From 4b5ebd34139f4c3f9a690e2cdb7f81f2aba97180 Mon Sep 17 00:00:00 2001 From: Olly Betts Date: Fri, 22 Dec 2017 16:41:48 +1300 Subject: [PATCH] Honey: Avoid temporary std::string when compressing tag --- xapian-core/backends/honey/honey_table.cc | 18 ++++++++++-------- xapian-core/backends/honey/honey_table.h | 11 ++++++++++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/xapian-core/backends/honey/honey_table.cc b/xapian-core/backends/honey/honey_table.cc index 82184967e..24a67b271 100644 --- a/xapian-core/backends/honey/honey_table.cc +++ b/xapian-core/backends/honey/honey_table.cc @@ -39,15 +39,17 @@ HoneyTable::open(int flags_, const RootInfo& root_info, honey_revision_number_t) } void -HoneyTable::add(const std::string& key, const std::string& val, bool compressed) +HoneyTable::add(const std::string& key, + const char* val, + size_t val_size, + bool compressed) { - if (!compressed && compress_min > 0 && val.size() > compress_min) { - size_t compressed_size = val.size(); + if (!compressed && compress_min > 0 && val_size > compress_min) { + size_t compressed_size = val_size; CompressionStream comp_stream; // FIXME: reuse - const char* p = comp_stream.compress(val.data(), &compressed_size); + const char* p = comp_stream.compress(val, &compressed_size); if (p) { - // FIXME: avoid temporary string. - add(key, string(p, compressed_size), true); + add(key, p, compressed_size, true); return; } } @@ -77,12 +79,12 @@ HoneyTable::add(const std::string& key, const std::string& val, bool compressed) // Encode "compressed?" flag in bottom bit. // FIXME: Don't do this if a table is uncompressed? That saves a byte // for each item where the extra bit pushes the length up by a byte. - size_t val_size_enc = (val.size() << 1) | compressed; + size_t val_size_enc = (val_size << 1) | compressed; std::string val_len; pack_uint(val_len, val_size_enc); // FIXME: pass together so we can potentially writev() both? fh.write(val_len.data(), val_len.size()); - fh.write(val.data(), val.size()); + fh.write(val, val_size); last_key = key; } diff --git a/xapian-core/backends/honey/honey_table.h b/xapian-core/backends/honey/honey_table.h index 8f3e62f8f..176e87ecc 100644 --- a/xapian-core/backends/honey/honey_table.h +++ b/xapian-core/backends/honey/honey_table.h @@ -381,7 +381,16 @@ class HoneyTable { const std::string& get_path() const { return path; } - void add(const std::string& key, const std::string& val, bool compressed = false); + void add(const std::string& key, + const char* val, + size_t val_size, + bool compressed = false); + + void add(const std::string& key, + const std::string& val, + bool compressed = false) { + add(key, val.data(), val.size(), compressed); + } void flush_db() { root = index.write(fh); -- 2.11.4.GIT