mirror of
https://github.com/djcb/mu.git
synced 2024-06-20 06:46:50 +02:00
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:
parent
7c85b61944
commit
1018f0f0a1
|
@ -38,7 +38,7 @@ const Xapian::Document&
|
|||
Document::xapian_document() const
|
||||
{
|
||||
if (dirty_sexp_) {
|
||||
xdoc_.set_data(sexp_.to_string());
|
||||
xdoc_.set_data(sexp().to_string());
|
||||
dirty_sexp_ = false;
|
||||
}
|
||||
return xdoc_;
|
||||
|
@ -47,7 +47,7 @@ Document::xapian_document() const
|
|||
template<typename SexpType> void
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
** under the terms of the GNU General Public License as published by the
|
||||
|
@ -43,19 +43,15 @@ class Document {
|
|||
public:
|
||||
/**
|
||||
* Construct a message for a new Xapian Document
|
||||
*
|
||||
*/
|
||||
Document() {}
|
||||
|
||||
/**
|
||||
* Construct a message document based on on existing Xapian document.
|
||||
* Construct a message document based on an existing Xapian document.
|
||||
*
|
||||
* @param doc
|
||||
*/
|
||||
Document(const Xapian::Document& doc): xdoc_{doc} {
|
||||
if (auto&& s{Sexp::parse(xdoc_.get_data())}; s)
|
||||
sexp_ = std::move(*s);
|
||||
}
|
||||
Document(const Xapian::Document& doc): xdoc_{doc} {}
|
||||
|
||||
/**
|
||||
* DTOR
|
||||
|
@ -153,7 +149,7 @@ public:
|
|||
*
|
||||
* @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
|
||||
|
@ -231,9 +227,16 @@ private:
|
|||
template<typename SexpType> void put_prop(const Field& field, 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_;
|
||||
Sexp sexp_;
|
||||
mutable Sexp cached_sexp_;
|
||||
mutable bool dirty_sexp_{}; /* xdoc's sexp is outdated */
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user