diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2021-09-14 16:09:24 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2021-09-14 16:09:24 +0200 |
commit | 0e4c6391cd2278c910fbe01f1184d7fc93ba1993 (patch) | |
tree | e199a4cf7354bb85486c0cefe87d42f44278df76 /config-model/src/main/java/com/yahoo | |
parent | e43172a0f87fffc7ac12c49a7f6111123d42a0b6 (diff) |
Use a single cached object to make logic simpler and only update on object.
Diffstat (limited to 'config-model/src/main/java/com/yahoo')
-rw-r--r-- | config-model/src/main/java/com/yahoo/searchdefinition/MapEvaluationTypeContext.java | 9 | ||||
-rw-r--r-- | config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java | 30 |
2 files changed, 23 insertions, 16 deletions
diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/MapEvaluationTypeContext.java b/config-model/src/main/java/com/yahoo/searchdefinition/MapEvaluationTypeContext.java index 01d4042573c..65f8b8fbe2a 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/MapEvaluationTypeContext.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/MapEvaluationTypeContext.java @@ -1,6 +1,7 @@ // Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchdefinition; +import com.google.common.collect.ImmutableMap; import com.yahoo.searchdefinition.expressiontransforms.OnnxModelTransformer; import com.yahoo.searchdefinition.expressiontransforms.TokenTransformer; import com.yahoo.searchlib.rankingexpression.ExpressionFunction; @@ -54,7 +55,7 @@ public class MapEvaluationTypeContext extends FunctionReferenceContext implement private final SortedSet<Reference> queryFeaturesNotDeclared; private boolean tensorsAreUsed; - MapEvaluationTypeContext(Collection<ExpressionFunction> functions, Map<Reference, TensorType> featureTypes) { + MapEvaluationTypeContext(ImmutableMap<String, ExpressionFunction> functions, Map<Reference, TensorType> featureTypes) { super(functions); this.parent = Optional.empty(); this.featureTypes.putAll(featureTypes); @@ -64,7 +65,7 @@ public class MapEvaluationTypeContext extends FunctionReferenceContext implement globallyResolvedTypes = new HashMap<>(); } - private MapEvaluationTypeContext(Map<String, ExpressionFunction> functions, + private MapEvaluationTypeContext(ImmutableMap<String, ExpressionFunction> functions, Map<String, String> bindings, Optional<MapEvaluationTypeContext> parent, Map<Reference, TensorType> featureTypes, @@ -333,10 +334,6 @@ public class MapEvaluationTypeContext extends FunctionReferenceContext implement return bindings; } - public Map<Reference, TensorType> featureTypes() { - return Collections.unmodifiableMap(featureTypes); - } - /** * Returns an unmodifiable view of the query features which was requested but for which we have no type info * (such that they default to TensorType.empty), shared between all instances of this diff --git a/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java b/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java index 939ee37d328..9d51d39f3d0 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java @@ -2,6 +2,7 @@ package com.yahoo.searchdefinition; import ai.vespa.rankingexpression.importer.configmodelview.ImportedMlModels; +import com.google.common.collect.ImmutableMap; import com.yahoo.config.application.api.ApplicationPackage; import com.yahoo.config.application.api.DeployLogger; import com.yahoo.search.query.profile.QueryProfileRegistry; @@ -23,7 +24,6 @@ import com.yahoo.searchlib.rankingexpression.evaluation.Value; import com.yahoo.searchlib.rankingexpression.rule.Arguments; import com.yahoo.searchlib.rankingexpression.rule.ReferenceNode; import com.yahoo.tensor.TensorType; -import com.yahoo.vespa.model.VespaModel; import java.io.File; import java.io.IOException; @@ -106,8 +106,7 @@ public class RankProfile implements Cloneable { private Map<String, RankingExpressionFunction> functions = new LinkedHashMap<>(); // This cache must be invalidated every time modifications are done to 'functions'. - private Map<String, RankingExpressionFunction> allFunctionsCached = null; - private List<ExpressionFunction> allExpressionFunctionCached = null; + private CachedFunctions allFunctionsCached = null; private Map<Reference, TensorType> inputFeatures = new LinkedHashMap<>(); @@ -130,6 +129,20 @@ public class RankProfile implements Cloneable { private final ApplicationPackage applicationPackage; private final DeployLogger deployLogger; + private static class CachedFunctions { + private final Map<String, RankingExpressionFunction> allRankingExpressionFunctions; + private final ImmutableMap<String, ExpressionFunction> allExpressionFunctions; + CachedFunctions(Map<String, RankingExpressionFunction> functions) { + allRankingExpressionFunctions = functions; + ImmutableMap.Builder<String,ExpressionFunction> mapBuilder = new ImmutableMap.Builder<>(); + for (var entry : functions.entrySet()) { + ExpressionFunction function = entry.getValue().function(); + mapBuilder.put(function.getName(), function); + } + allExpressionFunctions = mapBuilder.build(); + } + } + /** * Creates a new rank profile for a particular search definition * @@ -679,18 +692,15 @@ public class RankProfile implements Cloneable { /** Returns an unmodifiable snapshot of the functions in this */ public Map<String, RankingExpressionFunction> getFunctions() { updateCachedFunctions(); - return allFunctionsCached; + return allFunctionsCached.allRankingExpressionFunctions; } - private List<ExpressionFunction> getExpressionFunctions() { + private ImmutableMap<String, ExpressionFunction> getExpressionFunctions() { updateCachedFunctions(); - return allExpressionFunctionCached; + return allFunctionsCached.allExpressionFunctions; } private void updateCachedFunctions() { if (needToUpdateFunctionCache()) { - allFunctionsCached = gatherAllFunctions(); - allExpressionFunctionCached = allFunctionsCached.values().stream() - .map(RankingExpressionFunction::function) - .collect(Collectors.toList()); + allFunctionsCached = new CachedFunctions(gatherAllFunctions()); } } |