From 01a516f0d37023534b1f9e8b61d3fc6470c44017 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Sat, 5 Aug 2023 11:08:11 +0300 Subject: [PATCH] server: tweak sexp generation --- lib/mu-server.cc | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/mu-server.cc b/lib/mu-server.cc index ff322e83..94c1c21b 100644 --- a/lib/mu-server.cc +++ b/lib/mu-server.cc @@ -174,24 +174,28 @@ append_metadata(std::string& str, const QueryMatch& qmatch) * A message here consists of a message s-expression with optionally a :docid * and/or :meta expression added. * - * Note, for speed reasons, we build is as a _string_, without any further - * parsing. + * We could parse the sexp and use the Sexp APIs to add some things... but... + * it's _much_ faster to directly work on the string representation: remove the + * final ')', add a few items, and add the ')' again. */ static std::string msg_sexp_str(const Message& msg, Store::Id docid, const Option qm) { - auto sexpstr{msg.document().sexp_str()}; - sexpstr.reserve(sexpstr.size () + (docid == 0 ? 0 : 16) + (qm ? 64 : 0)); + auto&& sexpstr{msg.document().sexp_str()}; - // remove the closing ( ... ) - sexpstr.erase(sexpstr.end() - 1); + if (docid != 0 || qm) { + sexpstr.reserve(sexpstr.size () + (docid == 0 ? 0 : 16) + (qm ? 64 : 0)); - if (docid != 0) - sexpstr += " :docid " + to_string(docid); - if (qm) - append_metadata(sexpstr, *qm); + // remove the closing ( ... ) + sexpstr.erase(sexpstr.end() - 1); - sexpstr += ')'; // ... end close it again. + if (docid != 0) + sexpstr += " :docid " + to_string(docid); + if (qm) + append_metadata(sexpstr, *qm); + + sexpstr += ')'; // ... end close it again. + } return sexpstr; }