diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2021-06-30 11:07:47 +0000 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2021-06-30 11:07:47 +0000 |
commit | 8384e248d5ff8d9de184589cc8c21982f4eed718 (patch) | |
tree | bc36edaf9be892b65a9e287677558d4bef56c8b6 /searchsummary | |
parent | 49833295b149f33c97961a56a689b00efc76f24b (diff) |
Lookup the symbols outside of the loop.
Diffstat (limited to 'searchsummary')
-rw-r--r-- | searchsummary/src/vespa/searchsummary/docsummary/attributedfw.cpp | 92 |
1 files changed, 54 insertions, 38 deletions
diff --git a/searchsummary/src/vespa/searchsummary/docsummary/attributedfw.cpp b/searchsummary/src/vespa/searchsummary/docsummary/attributedfw.cpp index 65b262a85f2..b600a612bb5 100644 --- a/searchsummary/src/vespa/searchsummary/docsummary/attributedfw.cpp +++ b/searchsummary/src/vespa/searchsummary/docsummary/attributedfw.cpp @@ -25,6 +25,7 @@ using search::attribute::IAttributeVector; using vespalib::Memory; using vespalib::slime::Cursor; using vespalib::slime::Inserter; +using vespalib::slime::Symbol; using vespalib::eval::Value; namespace search::docsummary { @@ -39,7 +40,7 @@ AttrDFW::get_attribute(const GetDocsumsState& s) const { return *s.getAttribute(getIndex()); } -//----------------------------------------------------------------------------- +namespace { class SingleAttrDFW : public AttrDFW { @@ -161,46 +162,44 @@ public: }; void -insert_element(const std::vector<IAttributeVector::WeightedString>& elements, - size_t idx, bool is_weighted_set, Cursor& arr) +set(const vespalib::string & value, Symbol itemSymbol, Cursor & cursor) { - const vespalib::string& sv = elements[idx].getValue(); - Memory value(sv.c_str(), sv.size()); - if (is_weighted_set) { - Cursor& elem = arr.addObject(); - elem.setString("item", value); - elem.setLong("weight", elements[idx].getWeight()); - } else { - arr.addString(value); - } + cursor.setString(itemSymbol, value); } void -insert_element(const std::vector<IAttributeVector::WeightedInt>& elements, - size_t idx, bool is_weighted_set, Cursor& arr) +append(const IAttributeVector::WeightedString & element, Cursor& arr) { - if (is_weighted_set) { - Cursor& elem = arr.addObject(); - elem.setLong("item", elements[idx].getValue()); - elem.setLong("weight", elements[idx].getWeight()); - } else { - arr.addLong(elements[idx].getValue()); - } + arr.addString(element.getValue()); } void -insert_element(const std::vector<IAttributeVector::WeightedFloat>& elements, - size_t idx, bool is_weighted_set, Cursor& arr) +set(int64_t value, Symbol itemSymbol, Cursor & cursor) { - if (is_weighted_set) { - Cursor& elem = arr.addObject(); - elem.setDouble("item", elements[idx].getValue()); - elem.setLong("weight", elements[idx].getWeight()); - } else { - arr.addDouble(elements[idx].getValue()); - } + cursor.setLong(itemSymbol, value); } +void +append(const IAttributeVector::WeightedInt & element, Cursor& arr) +{ + arr.addLong(element.getValue()); +} + +void +set(double value, Symbol itemSymbol, Cursor & cursor) +{ + cursor.setDouble(itemSymbol, value); +} + +void +append(const IAttributeVector::WeightedFloat & element, Cursor& arr) +{ + arr.addDouble(element.getValue()); +} + +Memory ITEM("item"); +Memory WEIGHT("weight"); + template <typename DataType> void MultiAttrDFW<DataType>::insertField(uint32_t docid, GetDocsumsState* state, ResType, Inserter& target) @@ -219,21 +218,38 @@ MultiAttrDFW<DataType>::insertField(uint32_t docid, GetDocsumsState* state, ResT const auto& matching_elems = state->get_matching_elements(*_matching_elems_fields) .get_matching_elements(docid, getAttributeName()); if (!matching_elems.empty() && matching_elems.back() < entries) { - for (uint32_t id_to_keep : matching_elems) { - insert_element(elements, id_to_keep, _is_weighted_set, arr); + if (_is_weighted_set) { + Symbol itemSymbol = arr.resolve(ITEM); + Symbol weightSymbol = arr.resolve(WEIGHT); + for (uint32_t id_to_keep : matching_elems) { + const DataType & element = elements[id_to_keep]; + Cursor& elemC = arr.addObject(); + set(element.getValue(), itemSymbol, elemC); + elemC.setLong(weightSymbol, element.getWeight()); + } + } else { + for (uint32_t id_to_keep : matching_elems) { + append(elements[id_to_keep], arr); + } } } } else { - for (uint32_t i = 0; i < entries; ++i) { - insert_element(elements, i, _is_weighted_set, arr); + if (_is_weighted_set) { + Symbol itemSymbol = arr.resolve(ITEM); + Symbol weightSymbol = arr.resolve(WEIGHT); + for (const auto & element : elements) { + Cursor& elemC = arr.addObject(); + set(element.getValue(), itemSymbol, elemC); + elemC.setLong(weightSymbol, element.getWeight()); + } + } else { + for (const auto & element : elements) { + append(element, arr); + } } } } -//----------------------------------------------------------------------------- - -namespace { - std::unique_ptr<IDocsumFieldWriter> create_multi_writer(const IAttributeVector& attr, bool filter_elements, |