diff options
3 files changed, 29 insertions, 18 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()); } } diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/FunctionReferenceContext.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/FunctionReferenceContext.java index 6717bec0258..506884d15b4 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/FunctionReferenceContext.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/FunctionReferenceContext.java @@ -38,12 +38,16 @@ public class FunctionReferenceContext { /** Create a context for a single serialization task */ public FunctionReferenceContext(Map<String, ExpressionFunction> functions) { - this(functions.values()); + this(functions, null); } /** Create a context for a single serialization task */ public FunctionReferenceContext(Map<String, ExpressionFunction> functions, Map<String, String> bindings) { - this.functions = ImmutableMap.copyOf(functions); + this(ImmutableMap.copyOf(functions), bindings); + } + + protected FunctionReferenceContext(ImmutableMap<String, ExpressionFunction> functions, Map<String, String> bindings) { + this.functions = functions; if (bindings != null) this.bindings.putAll(bindings); } |