diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2019-06-05 11:55:09 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-06-05 11:55:09 +0300 |
commit | 149a42066caeba2add7aaec11b5afbf66c6b2b26 (patch) | |
tree | 8a35a8bd524d3ac703f9a7bf7b1d1ab1f1896ecb /searchlib | |
parent | 92a7375c69250986014bf972f3efa50e850bc966 (diff) | |
parent | 697438d79300cf93964bcb4a9d7ff5357a7128e0 (diff) |
Merge pull request #9680 from vespa-engine/balder/lookup-attribute-once
Only lookup once, not once per thread.
Diffstat (limited to 'searchlib')
-rw-r--r-- | searchlib/src/vespa/searchlib/features/attributefeature.cpp | 64 | ||||
-rw-r--r-- | searchlib/src/vespa/searchlib/features/attributefeature.h | 2 |
2 files changed, 39 insertions, 27 deletions
diff --git a/searchlib/src/vespa/searchlib/features/attributefeature.cpp b/searchlib/src/vespa/searchlib/features/attributefeature.cpp index 56d02ce6d4e..394bff7d622 100644 --- a/searchlib/src/vespa/searchlib/features/attributefeature.cpp +++ b/searchlib/src/vespa/searchlib/features/attributefeature.cpp @@ -36,6 +36,7 @@ using search::fef::FeatureType; using namespace search::fef::indexproperties; namespace { + template <typename X, typename Y> bool equals(X lhs, Y rhs) { return lhs == rhs; @@ -92,10 +93,8 @@ considerUndefined<ConstCharPtr>(ConstCharPtr value, BasicType::Type ) return search::features::util::getAsFeature(value); } - } - namespace search::features { /** @@ -117,7 +116,7 @@ public: class CountOnlyAttributeExecutor : public fef::FeatureExecutor { private: - const attribute::IAttributeVector & _attribute; + const IAttributeVector & _attribute; public: /** @@ -125,7 +124,7 @@ public: * * @param attribute The attribute vector to use. */ - CountOnlyAttributeExecutor(const attribute::IAttributeVector & attribute) : _attribute(attribute) { } + CountOnlyAttributeExecutor(const IAttributeVector & attribute) : _attribute(attribute) { } void execute(uint32_t docId) override; }; /** @@ -134,8 +133,8 @@ public: template <typename T> class AttributeExecutor : public fef::FeatureExecutor { private: - const attribute::IAttributeVector * _attribute; - attribute::BasicType::Type _attrType; + const IAttributeVector * _attribute; + BasicType::Type _attrType; uint32_t _idx; T _buffer; // used when fetching values from the attribute feature_t _defaultCount; @@ -147,7 +146,7 @@ public: * @param attribute The attribute vector to use. * @param idx The index used for an array attribute. */ - AttributeExecutor(const search::attribute::IAttributeVector * attribute, uint32_t idx); + AttributeExecutor(const IAttributeVector * attribute, uint32_t idx); void execute(uint32_t docId) override; }; @@ -158,8 +157,8 @@ public: template <typename BT, typename T> class WeightedSetAttributeExecutor : public fef::FeatureExecutor { private: - const attribute::IAttributeVector * _attribute; - attribute::BasicType::Type _attrType; + const IAttributeVector * _attribute; + BasicType::Type _attrType; BT _buffer; // used when fetching values and weights from the attribute T _key; // the key to find a weight for bool _useKey; @@ -172,7 +171,7 @@ public: * @param key The key to find a corresponding weight for. * @param useKey Whether we should consider the key. */ - WeightedSetAttributeExecutor(const search::attribute::IAttributeVector * attribute, T key, bool useKey); + WeightedSetAttributeExecutor(const IAttributeVector * attribute, T key, bool useKey); void execute(uint32_t docId) override; }; @@ -183,7 +182,7 @@ SingleAttributeExecutor<T>::execute(uint32_t docId) typename T::LoadedValueType v = _attribute.getFast(docId); // value outputs().set_number(0, __builtin_expect(attribute::isUndefined(v), false) - ? attribute::getUndefined<search::feature_t>() + ? attribute::getUndefined<feature_t>() : util::getAsFeature(v)); outputs().set_number(1, 0.0f); // weight outputs().set_number(2, 0.0f); // contains @@ -267,24 +266,25 @@ WeightedSetAttributeExecutor<BT, T>::execute(uint32_t docId) AttributeBlueprint::AttributeBlueprint() : - search::fef::Blueprint("attribute"), + fef::Blueprint("attribute"), _attrName(), _extra(), - _tensorType(ValueType::double_type()) + _tensorType(ValueType::double_type()), + _attribute(nullptr) { } AttributeBlueprint::~AttributeBlueprint() = default; void -AttributeBlueprint::visitDumpFeatures(const search::fef::IIndexEnvironment &, - search::fef::IDumpFeatureVisitor &) const +AttributeBlueprint::visitDumpFeatures(const fef::IIndexEnvironment &, + fef::IDumpFeatureVisitor &) const { } bool -AttributeBlueprint::setup(const search::fef::IIndexEnvironment & env, - const search::fef::ParameterList & params) +AttributeBlueprint::setup(const fef::IIndexEnvironment & env, + const fef::ParameterList & params) { // params[0] = attribute name // params[1] = index (array attribute) or key (weighted set attribute) @@ -315,20 +315,20 @@ AttributeBlueprint::setup(const search::fef::IIndexEnvironment & env, return !_tensorType.is_error(); } -search::fef::Blueprint::UP +fef::Blueprint::UP AttributeBlueprint::createInstance() const { - return search::fef::Blueprint::UP(new AttributeBlueprint()); + return std::make_unique<AttributeBlueprint>(); } #define CREATE_AND_RETURN_IF_SINGLE_NUMERIC(a, T) \ - if (dynamic_cast<const SingleValueNumericAttribute<T> *>(a) != NULL) { \ + if (dynamic_cast<const SingleValueNumericAttribute<T> *>(a) != nullptr) { \ return stash.create<SingleAttributeExecutor<SingleValueNumericAttribute<T>>>(*static_cast<const SingleValueNumericAttribute<T> *>(a)); \ } namespace { -search::fef::FeatureExecutor & +fef::FeatureExecutor & createAttributeExecutor(const IAttributeVector *attribute, const vespalib::string &attrName, const vespalib::string &extraParam, vespalib::Stash &stash) { if (attribute == NULL) { @@ -375,7 +375,7 @@ createAttributeExecutor(const IAttributeVector *attribute, const vespalib::strin } } -search::fef::FeatureExecutor & +fef::FeatureExecutor & createTensorAttributeExecutor(const IAttributeVector *attribute, const vespalib::string &attrName, const ValueType &tensorType, vespalib::Stash &stash) @@ -385,8 +385,8 @@ createTensorAttributeExecutor(const IAttributeVector *attribute, const vespalib: " Returning empty tensor.", attrName.c_str()); return ConstantTensorExecutor::createEmpty(tensorType, stash); } - if (attribute->getCollectionType() != search::attribute::CollectionType::SINGLE || - attribute->getBasicType() != search::attribute::BasicType::TENSOR) { + if (attribute->getCollectionType() != CollectionType::SINGLE || + attribute->getBasicType() != BasicType::TENSOR) { LOG(warning, "The attribute vector '%s' is NOT of type tensor." " Returning empty tensor.", attribute->getName().c_str()); return ConstantTensorExecutor::createEmpty(tensorType, stash); @@ -413,10 +413,20 @@ createTensorAttributeExecutor(const IAttributeVector *attribute, const vespalib: } -search::fef::FeatureExecutor & -AttributeBlueprint::createExecutor(const search::fef::IQueryEnvironment &env, vespalib::Stash &stash) const +void +AttributeBlueprint::prepareSharedState(const fef::IQueryEnvironment & env, fef::IObjectStore &) const +{ + //Ensure that we remember it the whole query + const IAttributeVector * attribute = env.getAttributeContext().getAttribute(_attrName); + if (_attribute == nullptr) { + _attribute = attribute; + } +} + +fef::FeatureExecutor & +AttributeBlueprint::createExecutor(const fef::IQueryEnvironment & env, vespalib::Stash &stash) const { - const IAttributeVector *attribute = env.getAttributeContext().getAttribute(_attrName); + const IAttributeVector * attribute = (_attribute != nullptr) ? _attribute : env.getAttributeContext().getAttribute(_attrName); if (_tensorType.is_tensor()) { return createTensorAttributeExecutor(attribute, _attrName, _tensorType, stash); } else { diff --git a/searchlib/src/vespa/searchlib/features/attributefeature.h b/searchlib/src/vespa/searchlib/features/attributefeature.h index c115c4acaea..ad6a44f73bd 100644 --- a/searchlib/src/vespa/searchlib/features/attributefeature.h +++ b/searchlib/src/vespa/searchlib/features/attributefeature.h @@ -18,6 +18,7 @@ private: vespalib::string _attrName; // the name of the attribute vector vespalib::string _extra; // the index or key vespalib::eval::ValueType _tensorType; + mutable const search::attribute::IAttributeVector * _attribute; public: AttributeBlueprint(); @@ -28,6 +29,7 @@ public: fef::FeatureExecutor &createExecutor(const fef::IQueryEnvironment &env, vespalib::Stash &stash) const override; fef::ParameterDescriptions getDescriptions() const override; bool setup(const fef::IIndexEnvironment & env, const fef::ParameterList & params) override; + void prepareSharedState(const fef::IQueryEnvironment & queryEnv, fef::IObjectStore & objectStore) const override; }; } |