diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2019-05-09 10:27:31 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-05-09 10:27:31 +0200 |
commit | 3dce67e9be89f32a2645061d9a5082c03e95e2b7 (patch) | |
tree | f51c56cd7bc161af393f74b06e70abb66633b62d /searchlib | |
parent | 02e883daacb9aac085d18d848395cad568373a17 (diff) | |
parent | 67d2d9abe0de150f0610761c99e8946629805378 (diff) |
Merge pull request #9325 from vespa-engine/balder/remove-redundant-if
Avoid if in the inner loop
Diffstat (limited to 'searchlib')
4 files changed, 38 insertions, 32 deletions
diff --git a/searchlib/src/tests/features/prod_features.cpp b/searchlib/src/tests/features/prod_features.cpp index 44a253cb5f7..51a98bec3d9 100644 --- a/searchlib/src/tests/features/prod_features.cpp +++ b/searchlib/src/tests/features/prod_features.cpp @@ -1048,11 +1048,13 @@ Test::setupForDocumentTest(FtFeatureTest &ft, const vespalib::string & attrName, namespace { void -verifyCorrectDotProductExecutor(BlueprintFactory & factory, vespalib::stringref attrName, vespalib::stringref expected) { +verifyCorrectDotProductExecutor(BlueprintFactory & factory, vespalib::stringref attrName, + vespalib::stringref queryVector, vespalib::stringref expected) +{ ParameterList params = {{ParameterType::ATTRIBUTE, attrName}, {ParameterType::STRING, "vector"}}; FtFeatureTest ft(factory, "value(0)"); Test::setupForDotProductTest(ft); - ft.getQueryEnv().getProperties().add("dotProduct.vector", "(7:1)"); + ft.getQueryEnv().getProperties().add("dotProduct.vector", queryVector); DotProductBlueprint bp; DummyDependencyHandler deps(bp); EXPECT_TRUE(bp.setup(ft.getIndexEnv(), params)); @@ -1225,8 +1227,11 @@ Test::testDotProduct() assertDotProduct(17, "(0:1,3:4,50:97)", 1, "sint", "arrfloat"); // attribute override assertDotProduct(0, "(0:1,3:4,50:97)", 1, "sint", "arrfloat_non_existing"); // incorrect attribute override } - verifyCorrectDotProductExecutor(_factory, "wsstr", "search::features::dotproduct::wset::(anonymous namespace)::DotProductExecutorByEnum"); - verifyCorrectDotProductExecutor(_factory, "wsint", "search::features::dotproduct::wset::DotProductExecutor<search::MultiValueNumericAttribute<search::IntegerAttributeTemplate<int>, search::multivalue::WeightedValue<int> > >"); + verifyCorrectDotProductExecutor(_factory, "wsstr", "{a:1}", "search::features::dotproduct::wset::(anonymous namespace)::DotProductExecutorByEnum"); + verifyCorrectDotProductExecutor(_factory, "wsstr", "{unknown:1}", "search::features::SingleZeroValueExecutor"); + verifyCorrectDotProductExecutor(_factory, "wsint", "{1:1}", "search::features::dotproduct::wset::DotProductExecutor<search::MultiValueNumericAttribute<search::IntegerAttributeTemplate<int>, search::multivalue::WeightedValue<int> > >"); + verifyCorrectDotProductExecutor(_factory, "wsint", "{}", "search::features::SingleZeroValueExecutor"); + } void diff --git a/searchlib/src/vespa/searchlib/features/dotproductfeature.cpp b/searchlib/src/vespa/searchlib/features/dotproductfeature.cpp index 0589164b87f..fc71100dea6 100644 --- a/searchlib/src/vespa/searchlib/features/dotproductfeature.cpp +++ b/searchlib/src/vespa/searchlib/features/dotproductfeature.cpp @@ -65,13 +65,11 @@ void DotProductExecutorByCopy<Vector, Buffer>::execute(uint32_t docId) { feature_t val = 0; - if (!_queryVector.getDimMap().empty()) { - _buffer.fill(*_attribute, docId); - for (size_t i = 0; i < _buffer.size(); ++i) { - auto itr = _queryVector.getDimMap().find(_buffer[i].getValue()); - if (itr != _end) { - val += _buffer[i].getWeight() * itr->second; - } + _buffer.fill(*_attribute, docId); + for (size_t i = 0; i < _buffer.size(); ++i) { + auto itr = _queryVector.getDimMap().find(_buffer[i].getValue()); + if (itr != _end) { + val += _buffer[i].getWeight() * itr->second; } } outputs().set_number(0, val); @@ -94,14 +92,12 @@ DotProductExecutorBase<BaseType>::~DotProductExecutorBase() = default; template <typename BaseType> void DotProductExecutorBase<BaseType>::execute(uint32_t docId) { feature_t val = 0; - if (!_queryVector.getDimMap().empty()) { - const AT * values(nullptr); - uint32_t sz = getAttributeValues(docId, values); - for (size_t i = 0; i < sz; ++i) { - auto itr = _queryVector.getDimMap().find(values[i].value()); - if (itr != _end) { - val += values[i].weight() * itr->second; - } + const AT * values(nullptr); + uint32_t sz = getAttributeValues(docId, values); + for (size_t i = 0; i < sz; ++i) { + auto itr = _queryVector.getDimMap().find(values[i].value()); + if (itr != _end) { + val += values[i].weight() * itr->second; } } outputs().set_number(0, val); @@ -151,14 +147,12 @@ DotProductExecutorByEnum::~DotProductExecutorByEnum() = default; void DotProductExecutorByEnum::execute(uint32_t docId) { feature_t val = 0; - if (!_queryVector.getDimMap().empty()) { - const IWeightedIndexVector::WeightedIndex *values(nullptr); - uint32_t sz = _attribute->getEnumHandles(docId, values); - for (size_t i = 0; i < sz; ++i) { - auto itr = _queryVector.getDimMap().find(values[i].value().ref()); - if (itr != _end) { - val += values[i].weight() * itr->second; - } + const IWeightedIndexVector::WeightedIndex *values(nullptr); + uint32_t sz = _attribute->getEnumHandles(docId, values); + for (size_t i = 0; i < sz; ++i) { + auto itr = _queryVector.getDimMap().find(values[i].value().ref()); + if (itr != _end) { + val += values[i].weight() * itr->second; } } outputs().set_number(0, val); @@ -677,7 +671,9 @@ createForDirectIntegerWSet(const IAttributeVector * attribute, const Property & using namespace dotproduct::wset; IntegerVectorT<T> vector; WeightedSetParser::parse(prop.get(), vector); - return createForDirectWSetImpl<IntegerAttributeTemplate<T>>(attribute, std::move(vector), stash); + return vector.empty() + ? &stash.create<SingleZeroValueExecutor>() + : createForDirectWSetImpl<IntegerAttributeTemplate<T>>(attribute, std::move(vector), stash); } @@ -687,6 +683,9 @@ createTypedWsetExecutor(const IAttributeVector * attribute, const Property & pro if (attribute->hasEnum()) { EnumVector vector(attribute); WeightedSetParser::parse(prop.get(), vector); + if (vector.empty()) { + return &stash.create<SingleZeroValueExecutor>(); + } const IWeightedIndexVector * getEnumHandles = dynamic_cast<const IWeightedIndexVector *>(attribute); if (supportsGetEnumHandles(getEnumHandles)) { return &stash.create<DotProductExecutorByEnum>(getEnumHandles, std::move(vector)); @@ -696,6 +695,9 @@ createTypedWsetExecutor(const IAttributeVector * attribute, const Property & pro if (attribute->isStringType()) { StringVector vector; WeightedSetParser::parse(prop.get(), vector); + if (vector.empty()) { + return &stash.create<SingleZeroValueExecutor>(); + } return &stash.create<DotProductExecutorByCopy<StringVector, WeightedConstCharContent>>(attribute, std::move(vector)); } else if (attribute->isIntegerType()) { if (attribute->getBasicType() == BasicType::INT32) { diff --git a/searchlib/src/vespa/searchlib/features/dotproductfeature.h b/searchlib/src/vespa/searchlib/features/dotproductfeature.h index 38dcdd54929..e5c4073bd8f 100644 --- a/searchlib/src/vespa/searchlib/features/dotproductfeature.h +++ b/searchlib/src/vespa/searchlib/features/dotproductfeature.h @@ -61,6 +61,7 @@ public: const Vector & getVector() const { return _vector; } VectorBase & syncMap(); const HashMap & getDimMap() const { return _dimMap; } + bool empty() const { return _vector.empty(); } }; /** diff --git a/searchlib/src/vespa/searchlib/features/weighted_set_parser.cpp b/searchlib/src/vespa/searchlib/features/weighted_set_parser.cpp index 88208fdf76c..577ca746e14 100644 --- a/searchlib/src/vespa/searchlib/features/weighted_set_parser.cpp +++ b/searchlib/src/vespa/searchlib/features/weighted_set_parser.cpp @@ -5,8 +5,7 @@ #include <vespa/log/log.h> LOG_SETUP(".features.weighted_set_parser"); -namespace search { -namespace features { +namespace search::features { void WeightedSetParser::logWarning(const vespalib::string &msg) @@ -14,5 +13,4 @@ WeightedSetParser::logWarning(const vespalib::string &msg) LOG(warning, "%s", msg.c_str()); } -} // namespace features -} // namespace search +} |