From 2a7ebcb8f74d63d1de11c1ccb85b7bcc3429b258 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Fri, 3 Sep 2021 14:37:14 +0200 Subject: In order to avoid the quadratic cost of creating a new functions map detect when the underlying map has changed, and then you take a new snapshot. --- .../main/java/com/yahoo/searchdefinition/RankProfile.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) 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 b9e241a06e4..b6dde58fff0 100644 --- a/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java +++ b/config-model/src/main/java/com/yahoo/searchdefinition/RankProfile.java @@ -623,6 +623,12 @@ public class RankProfile implements Cloneable { return Collections.unmodifiableMap(allFunctions); } + private int getNumFunctions() { + if (getInherited() != null) + return functions.size() + getInherited().getNumFunctions(); + return functions.size(); + } + public int getKeepRankCount() { if (keepRankCount >= 0) return keepRankCount; if (getInherited() != null) return getInherited().getKeepRankCount(); @@ -776,11 +782,17 @@ public class RankProfile implements Cloneable { // Compile all functions. Why iterate in such a complicated way? // Because some functions (imported models adding generated macros) may add other functions during compiling. // A straightforward iteration will either miss those functions, or may cause a ConcurrentModificationException - while (null != (entry = findUncompiledFunction(functions.get(), compiledFunctions.keySet()))) { + Map currentFunctions = functions.get(); + int currentCountOfAllFunctions = getNumFunctions(); + while (null != (entry = findUncompiledFunction(currentFunctions, compiledFunctions.keySet()))) { RankingExpressionFunction rankingExpressionFunction = entry.getValue(); RankingExpressionFunction compiled = compile(rankingExpressionFunction, queryProfiles, featureTypes, importedModels, getConstants(), inlineFunctions, expressionTransforms); compiledFunctions.put(entry.getKey(), compiled); + if (getNumFunctions() > currentCountOfAllFunctions) { + currentCountOfAllFunctions = getNumFunctions(); + currentFunctions = functions.get(); + } } return compiledFunctions; } -- cgit v1.2.3