diff options
author | Arne Juul <arnej@verizonmedia.com> | 2021-02-22 13:25:19 +0000 |
---|---|---|
committer | Arne Juul <arnej@verizonmedia.com> | 2021-02-22 13:25:21 +0000 |
commit | f3b69ab71ac7dde197f25ffd67d5851648ebafbc (patch) | |
tree | e91c9dee83d7dcf20a7f1be8c67b0d326ccb4e0f | |
parent | 6a81c9802ce151c9a7ec47234a7f34bd7c1c6a56 (diff) |
move getting tensor into the attribute code
* avoids taking an extra copy of the tensor
in the DirectTensorAttribute case
5 files changed, 39 insertions, 29 deletions
diff --git a/searchcore/src/vespa/searchcore/proton/common/attribute_updater.cpp b/searchcore/src/vespa/searchcore/proton/common/attribute_updater.cpp index 8fb165cb0b8..4d12fdfcf5b 100644 --- a/searchcore/src/vespa/searchcore/proton/common/attribute_updater.cpp +++ b/searchcore/src/vespa/searchcore/proton/common/attribute_updater.cpp @@ -205,23 +205,6 @@ AttributeUpdater::handleUpdate(PredicateAttribute &vec, uint32_t lid, const Valu } } -namespace { - -void -applyTensorUpdate(TensorAttribute &vec, uint32_t lid, const document::TensorUpdate &update, - bool create_empty_if_non_existing) -{ - auto oldTensor = vec.getTensor(lid); - if (!oldTensor && create_empty_if_non_existing) { - oldTensor = vec.getEmptyTensor(); - } - if (oldTensor) { - vec.update_tensor(lid, update, *oldTensor); - } -} - -} - template <> void AttributeUpdater::handleUpdate(TensorAttribute &vec, uint32_t lid, const ValueUpdate &upd) @@ -236,11 +219,11 @@ AttributeUpdater::handleUpdate(TensorAttribute &vec, uint32_t lid, const ValueUp updateValue(vec, lid, assign.getValue()); } } else if (op == ValueUpdate::TensorModifyUpdate) { - applyTensorUpdate(vec, lid, static_cast<const TensorModifyUpdate &>(upd), false); + vec.update_tensor(lid, static_cast<const TensorModifyUpdate &>(upd), false); } else if (op == ValueUpdate::TensorAddUpdate) { - applyTensorUpdate(vec, lid, static_cast<const TensorAddUpdate &>(upd), true); + vec.update_tensor(lid, static_cast<const TensorAddUpdate &>(upd), true); } else if (op == ValueUpdate::TensorRemoveUpdate) { - applyTensorUpdate(vec, lid, static_cast<const TensorRemoveUpdate &>(upd), false); + vec.update_tensor(lid, static_cast<const TensorRemoveUpdate &>(upd), false); } else if (op == ValueUpdate::Clear) { vec.clearDoc(lid); } else { diff --git a/searchlib/src/vespa/searchlib/tensor/direct_tensor_attribute.cpp b/searchlib/src/vespa/searchlib/tensor/direct_tensor_attribute.cpp index f4010857c76..c89f83defea 100644 --- a/searchlib/src/vespa/searchlib/tensor/direct_tensor_attribute.cpp +++ b/searchlib/src/vespa/searchlib/tensor/direct_tensor_attribute.cpp @@ -79,11 +79,27 @@ DirectTensorAttribute::setTensor(DocId lid, const vespalib::eval::Value &tensor) void DirectTensorAttribute::update_tensor(DocId docId, const document::TensorUpdate &update, - const vespalib::eval::Value &old_tensor) + bool create_if_non_existing) { - auto new_value = update.apply_to(old_tensor, FastValueBuilderFactory::get()); - if (new_value) { - set_tensor(docId, std::move(new_value)); + EntryRef ref; + if (docId < getCommittedDocIdLimit()) { + ref = _refVector[docId]; + } + if (ref.valid()) { + auto ptr = _direct_store.get_tensor(ref); + if (ptr) { + auto new_value = update.apply_to(*ptr, FastValueBuilderFactory::get()); + if (new_value) { + set_tensor(docId, std::move(new_value)); + } + return; + } + } + if (create_if_non_existing) { + auto new_value = update.apply_to(*_emptyTensor, FastValueBuilderFactory::get()); + if (new_value) { + set_tensor(docId, std::move(new_value)); + } } } diff --git a/searchlib/src/vespa/searchlib/tensor/direct_tensor_attribute.h b/searchlib/src/vespa/searchlib/tensor/direct_tensor_attribute.h index a87526342ef..dfa5ff0b3ce 100644 --- a/searchlib/src/vespa/searchlib/tensor/direct_tensor_attribute.h +++ b/searchlib/src/vespa/searchlib/tensor/direct_tensor_attribute.h @@ -19,7 +19,7 @@ public: virtual void setTensor(DocId docId, const vespalib::eval::Value &tensor) override; void update_tensor(DocId docId, const document::TensorUpdate &update, - const vespalib::eval::Value &old_tensor) override; + bool create_empty_if_non_existing) override; virtual std::unique_ptr<vespalib::eval::Value> getTensor(DocId docId) const override; virtual bool onLoad() override; virtual std::unique_ptr<AttributeSaver> onInitSave(vespalib::stringref fileName) override; diff --git a/searchlib/src/vespa/searchlib/tensor/tensor_attribute.cpp b/searchlib/src/vespa/searchlib/tensor/tensor_attribute.cpp index f5de1de640f..042ecba0901 100644 --- a/searchlib/src/vespa/searchlib/tensor/tensor_attribute.cpp +++ b/searchlib/src/vespa/searchlib/tensor/tensor_attribute.cpp @@ -254,10 +254,21 @@ TensorAttribute::getRefCopy() const void TensorAttribute::update_tensor(DocId docId, const document::TensorUpdate &update, - const vespalib::eval::Value &old_tensor) + bool create_empty_if_non_existing) { - auto new_value = update.apply_to(old_tensor, FastValueBuilderFactory::get()); - setTensor(docId, *new_value); + const vespalib::eval::Value * old_v = nullptr; + auto old_tensor = getTensor(docId); + if (old_tensor) { + old_v = old_tensor.get(); + } else if (create_empty_if_non_existing) { + old_v = _emptyTensor.get(); + } else { + return; + } + auto new_value = update.apply_to(*old_v, FastValueBuilderFactory::get()); + if (new_value) { + setTensor(docId, *new_value); + } } std::unique_ptr<PrepareResult> diff --git a/searchlib/src/vespa/searchlib/tensor/tensor_attribute.h b/searchlib/src/vespa/searchlib/tensor/tensor_attribute.h index adb9e7bca8c..9d92e226139 100644 --- a/searchlib/src/vespa/searchlib/tensor/tensor_attribute.h +++ b/searchlib/src/vespa/searchlib/tensor/tensor_attribute.h @@ -61,7 +61,7 @@ public: virtual void setTensor(DocId docId, const vespalib::eval::Value &tensor) = 0; virtual void update_tensor(DocId docId, const document::TensorUpdate &update, - const vespalib::eval::Value &oldTensor); + bool create_empty_if_non_existing); /** * Performs the prepare step in a two-phase operation to set a tensor for a document. * |