mu-document: Make sexp() lazy (optimization)
This makes queries where we don't need the sexp much faster; e.g. before: mu find "a" --include-related 47,51s user 2,68s system 99% cpu 50,651 total after: mu find "a" --include-related 7,12s user 1,97s system 87% cpu 10,363 total
This commit is contained in:
@ -38,7 +38,7 @@ const Xapian::Document&
|
|||||||
Document::xapian_document() const
|
Document::xapian_document() const
|
||||||
{
|
{
|
||||||
if (dirty_sexp_) {
|
if (dirty_sexp_) {
|
||||||
xdoc_.set_data(sexp_.to_string());
|
xdoc_.set_data(sexp().to_string());
|
||||||
dirty_sexp_ = false;
|
dirty_sexp_ = false;
|
||||||
}
|
}
|
||||||
return xdoc_;
|
return xdoc_;
|
||||||
@ -47,7 +47,7 @@ Document::xapian_document() const
|
|||||||
template<typename SexpType> void
|
template<typename SexpType> void
|
||||||
Document::put_prop(const std::string& pname, SexpType&& val)
|
Document::put_prop(const std::string& pname, SexpType&& val)
|
||||||
{
|
{
|
||||||
sexp_.put_props(pname, std::forward<SexpType>(val));
|
cached_sexp().put_props(pname, std::forward<SexpType>(val));
|
||||||
dirty_sexp_ = true;
|
dirty_sexp_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
/** Copyright (C) 2022 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
/** Copyright (C) 2022-2023 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
|
||||||
**
|
**
|
||||||
** This program is free software; you can redistribute it and/or modify it
|
** This program is free software; you can redistribute it and/or modify it
|
||||||
** under the terms of the GNU General Public License as published by the
|
** under the terms of the GNU General Public License as published by the
|
||||||
@ -43,19 +43,15 @@ class Document {
|
|||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* Construct a message for a new Xapian Document
|
* Construct a message for a new Xapian Document
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
Document() {}
|
Document() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct a message document based on on existing Xapian document.
|
* Construct a message document based on an existing Xapian document.
|
||||||
*
|
*
|
||||||
* @param doc
|
* @param doc
|
||||||
*/
|
*/
|
||||||
Document(const Xapian::Document& doc): xdoc_{doc} {
|
Document(const Xapian::Document& doc): xdoc_{doc} {}
|
||||||
if (auto&& s{Sexp::parse(xdoc_.get_data())}; s)
|
|
||||||
sexp_ = std::move(*s);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DTOR
|
* DTOR
|
||||||
@ -153,7 +149,7 @@ public:
|
|||||||
*
|
*
|
||||||
* @return the cached s-expression
|
* @return the cached s-expression
|
||||||
*/
|
*/
|
||||||
const Sexp& sexp() const { return sexp_; }
|
const Sexp& sexp() const { return cached_sexp(); }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generically adds an optional value, if set, to the document
|
* Generically adds an optional value, if set, to the document
|
||||||
@ -231,9 +227,16 @@ private:
|
|||||||
template<typename SexpType> void put_prop(const Field& field, SexpType&& val);
|
template<typename SexpType> void put_prop(const Field& field, SexpType&& val);
|
||||||
template<typename SexpType> void put_prop(const std::string& pname, SexpType&& val);
|
template<typename SexpType> void put_prop(const std::string& pname, SexpType&& val);
|
||||||
|
|
||||||
|
Sexp& cached_sexp() const {
|
||||||
|
if (cached_sexp_.empty())
|
||||||
|
if (auto&& s{Sexp::parse(xdoc_.get_data())}; s)
|
||||||
|
cached_sexp_ = std::move(*s);
|
||||||
|
return cached_sexp_;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
mutable Xapian::Document xdoc_;
|
mutable Xapian::Document xdoc_;
|
||||||
Sexp sexp_;
|
mutable Sexp cached_sexp_;
|
||||||
mutable bool dirty_sexp_{}; /* xdoc's sexp is outdated */
|
mutable bool dirty_sexp_{}; /* xdoc's sexp is outdated */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user