From da9db258815a32cd689fb372a56dc9df07e21742 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Tue, 4 Jun 2019 17:48:53 +0000 Subject: Only lookup once, not once per thread. --- .../vespa/searchlib/features/attributefeature.cpp | 64 ++++++++++++---------- .../vespa/searchlib/features/attributefeature.h | 2 + 2 files changed, 38 insertions(+), 28 deletions(-) (limited to 'searchlib') diff --git a/searchlib/src/vespa/searchlib/features/attributefeature.cpp b/searchlib/src/vespa/searchlib/features/attributefeature.cpp index 56d02ce6d4e..e1926c74975 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 bool equals(X lhs, Y rhs) { return lhs == rhs; @@ -92,10 +93,8 @@ considerUndefined(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 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 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::execute(uint32_t docId) typename T::LoadedValueType v = _attribute.getFast(docId); // value outputs().set_number(0, __builtin_expect(attribute::isUndefined(v), false) - ? attribute::getUndefined() + ? attribute::getUndefined() : util::getAsFeature(v)); outputs().set_number(1, 0.0f); // weight outputs().set_number(2, 0.0f); // contains @@ -267,7 +266,7 @@ WeightedSetAttributeExecutor::execute(uint32_t docId) AttributeBlueprint::AttributeBlueprint() : - search::fef::Blueprint("attribute"), + fef::Blueprint("attribute"), _attrName(), _extra(), _tensorType(ValueType::double_type()) @@ -277,14 +276,14 @@ AttributeBlueprint::AttributeBlueprint() : 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 +314,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(); } #define CREATE_AND_RETURN_IF_SINGLE_NUMERIC(a, T) \ - if (dynamic_cast *>(a) != NULL) { \ + if (dynamic_cast *>(a) != nullptr) { \ return stash.create>>(*static_cast *>(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 +374,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 +384,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,14 +412,23 @@ 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 &, vespalib::Stash &stash) const { - const IAttributeVector *attribute = env.getAttributeContext().getAttribute(_attrName); if (_tensorType.is_tensor()) { - return createTensorAttributeExecutor(attribute, _attrName, _tensorType, stash); + return createTensorAttributeExecutor(_attribute, _attrName, _tensorType, stash); } else { - return createAttributeExecutor(attribute, _attrName, _extra, stash); + return createAttributeExecutor(_attribute, _attrName, _extra, stash); } } 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; }; } -- cgit v1.2.3 From af7ac97c71e6c026f76a3b1d5e4f44af218571d5 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Wed, 5 Jun 2019 06:30:16 +0000 Subject: Do not depend on prepareSharedState to be called first. --- searchlib/src/vespa/searchlib/features/attributefeature.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'searchlib') diff --git a/searchlib/src/vespa/searchlib/features/attributefeature.cpp b/searchlib/src/vespa/searchlib/features/attributefeature.cpp index e1926c74975..baebf6a8877 100644 --- a/searchlib/src/vespa/searchlib/features/attributefeature.cpp +++ b/searchlib/src/vespa/searchlib/features/attributefeature.cpp @@ -423,12 +423,13 @@ AttributeBlueprint::prepareSharedState(const fef::IQueryEnvironment & env, fef:: } fef::FeatureExecutor & -AttributeBlueprint::createExecutor(const fef::IQueryEnvironment &, vespalib::Stash &stash) const +AttributeBlueprint::createExecutor(const fef::IQueryEnvironment & env, vespalib::Stash &stash) const { + const IAttributeVector * attribute = (_attribute != nullptr) ? _attribute : env.getAttributeContext().getAttribute(_attrName); if (_tensorType.is_tensor()) { - return createTensorAttributeExecutor(_attribute, _attrName, _tensorType, stash); + return createTensorAttributeExecutor(attribute, _attrName, _tensorType, stash); } else { - return createAttributeExecutor(_attribute, _attrName, _extra, stash); + return createAttributeExecutor(attribute, _attrName, _extra, stash); } } -- cgit v1.2.3 From 697438d79300cf93964bcb4a9d7ff5357a7128e0 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Wed, 5 Jun 2019 08:50:32 +0000 Subject: Initialize pointer to nullptr --- searchlib/src/vespa/searchlib/features/attributefeature.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'searchlib') diff --git a/searchlib/src/vespa/searchlib/features/attributefeature.cpp b/searchlib/src/vespa/searchlib/features/attributefeature.cpp index baebf6a8877..394bff7d622 100644 --- a/searchlib/src/vespa/searchlib/features/attributefeature.cpp +++ b/searchlib/src/vespa/searchlib/features/attributefeature.cpp @@ -269,7 +269,8 @@ AttributeBlueprint::AttributeBlueprint() : fef::Blueprint("attribute"), _attrName(), _extra(), - _tensorType(ValueType::double_type()) + _tensorType(ValueType::double_type()), + _attribute(nullptr) { } -- cgit v1.2.3