diff options
author | Harald Musum <musum@verizonmedia.com> | 2022-12-02 01:11:20 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-02 01:11:20 +0100 |
commit | 4fa41e13f4baa0d8927e516c6db594b8f4ec8a3e (patch) | |
tree | da04fd3f1ed4275a341c6a1bda092e27e9f3c9d6 /searchlib | |
parent | ffdaafffd90a2a8cb1522c7e131f13fc718be3f7 (diff) | |
parent | 6a9681d7f3e42f29bd1d9de9fe9c271489b0c886 (diff) |
Merge pull request #25065 from vespa-engine/revert-25064-revert-25062-balder/gc-even-more-guava-usage
Revert "Revert "- Reduce usage of guava.""
Diffstat (limited to 'searchlib')
8 files changed, 44 insertions, 60 deletions
diff --git a/searchlib/abi-spec.json b/searchlib/abi-spec.json index 16c1b2c0e7d..5413907e967 100644 --- a/searchlib/abi-spec.json +++ b/searchlib/abi-spec.json @@ -277,6 +277,7 @@ "public java.util.List arguments()", "public com.yahoo.searchlib.rankingexpression.RankingExpression getBody()", "public java.util.Map argumentTypes()", + "public com.yahoo.tensor.TensorType getArgumentType(java.lang.String)", "public java.util.Optional returnType()", "public com.yahoo.searchlib.rankingexpression.ExpressionFunction withName(java.lang.String)", "public com.yahoo.searchlib.rankingexpression.ExpressionFunction withBody(com.yahoo.searchlib.rankingexpression.RankingExpression)", diff --git a/searchlib/src/main/java/com/yahoo/searchlib/ranking/features/fieldmatch/Field.java b/searchlib/src/main/java/com/yahoo/searchlib/ranking/features/fieldmatch/Field.java index 9492cebc608..3f711df4fdd 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/ranking/features/fieldmatch/Field.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/ranking/features/fieldmatch/Field.java @@ -1,8 +1,7 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.ranking.features.fieldmatch; -import com.google.common.collect.ImmutableList; - +import java.util.Arrays; import java.util.List; /** @@ -12,19 +11,17 @@ import java.util.List; */ public class Field { - private final ImmutableList<Term> terms; + private final List<Term> terms; /** Creates a field from a space-separated string */ public Field(String fieldString) { - ImmutableList.Builder<Term> list = new ImmutableList.Builder<>(); - for (String term : fieldString.split(" ")) - list.add(new Term(term)); - this.terms = list.build(); + terms = Arrays.stream(fieldString.split(" ")).map(Term::new).toList(); + } /** Creates a field from a list of terms */ public Field(List<Term> terms) { - this.terms = ImmutableList.copyOf(terms); + this.terms = List.copyOf(terms); } /** Returns an immutable list of the terms in this */ diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/ExpressionFunction.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/ExpressionFunction.java index eac87ff2f12..171151bfdf4 100755 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/ExpressionFunction.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/ExpressionFunction.java @@ -1,8 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.rankingexpression; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; import com.yahoo.searchlib.rankingexpression.rule.ConstantNode; import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode; import com.yahoo.searchlib.rankingexpression.rule.FunctionNode; @@ -36,10 +34,10 @@ import java.util.Optional; public class ExpressionFunction { private final String name; - private final ImmutableList<String> arguments; + private final List<String> arguments; /** Types of the inputs, if known. The keys here is any subset (including empty and identity) of the argument list */ - private final ImmutableMap<String, TensorType> argumentTypes; + private final Map<String, TensorType> argumentTypes; private final RankingExpression body; private final Optional<TensorType> returnType; @@ -62,17 +60,17 @@ public class ExpressionFunction { * @param body the ranking expression that defines this function */ public ExpressionFunction(String name, List<String> arguments, RankingExpression body) { - this(name, arguments, body, ImmutableMap.of(), Optional.empty()); + this(name, arguments, body, Map.of(), Optional.empty()); } public ExpressionFunction(String name, List<String> arguments, RankingExpression body, Map<String, TensorType> argumentTypes, Optional<TensorType> returnType) { this.name = Objects.requireNonNull(name, "name cannot be null"); - this.arguments = arguments==null ? ImmutableList.of() : ImmutableList.copyOf(arguments); + this.arguments = arguments==null ? List.of() : List.copyOf(arguments); this.body = Objects.requireNonNull(body, "body cannot be null"); if ( ! this.arguments.containsAll(argumentTypes.keySet())) throw new IllegalArgumentException("Argument type keys must be a subset of the argument keys"); - this.argumentTypes = ImmutableMap.copyOf(argumentTypes); + this.argumentTypes = Map.copyOf(argumentTypes); this.returnType = Objects.requireNonNull(returnType, "returnType cannot be null"); } @@ -85,6 +83,7 @@ public class ExpressionFunction { /** Returns the types of the arguments of this, if specified. The keys of this may be any subset of the arguments */ public Map<String, TensorType> argumentTypes() { return argumentTypes; } + public TensorType getArgumentType(String argumentName) { return argumentTypes.get(argumentName); } /** Returns the return type of this, or empty if not specified */ public Optional<TensorType> returnType() { return returnType; } diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/AbstractArrayContext.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/AbstractArrayContext.java index 340556b7e2d..4d6ac0104c7 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/AbstractArrayContext.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/evaluation/AbstractArrayContext.java @@ -1,11 +1,12 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.rankingexpression.evaluation; -import com.google.common.collect.ImmutableMap; +import com.yahoo.lang.MutableInteger; import com.yahoo.searchlib.rankingexpression.RankingExpression; import com.yahoo.searchlib.rankingexpression.rule.CompositeNode; import com.yahoo.searchlib.rankingexpression.rule.ExpressionNode; import com.yahoo.searchlib.rankingexpression.rule.ReferenceNode; +import com.yahoo.stream.CustomCollectors; import java.util.BitSet; import java.util.LinkedHashSet; @@ -116,7 +117,7 @@ public abstract class AbstractArrayContext extends Context implements Cloneable, private static class IndexedBindings implements Cloneable { /** The mapping from variable name to index */ - private final ImmutableMap<String, Integer> nameToIndex; + private final Map<String, Integer> nameToIndex; /** The current values set, pre-converted to doubles */ private double[] doubleValues; @@ -125,7 +126,7 @@ public abstract class AbstractArrayContext extends Context implements Cloneable, private BitSet setValues; /** Value to return if value is missing. */ - private double missingValue; + private final double missingValue; public IndexedBindings(RankingExpression expression, Value missingValue) { Set<String> bindTargets = new LinkedHashSet<>(); @@ -138,11 +139,8 @@ public abstract class AbstractArrayContext extends Context implements Cloneable, doubleValues[i] = this.missingValue; } - int i = 0; - ImmutableMap.Builder<String, Integer> nameToIndexBuilder = new ImmutableMap.Builder<>(); - for (String variable : bindTargets) - nameToIndexBuilder.put(variable,i++); - nameToIndex = nameToIndexBuilder.build(); + MutableInteger index = new MutableInteger(0); + nameToIndex = bindTargets.stream().collect(CustomCollectors.toLinkedMap(name -> name, name -> index.next())); } private void extractBindTargets(ExpressionNode node, Set<String> bindTargets) { @@ -152,8 +150,7 @@ public abstract class AbstractArrayContext extends Context implements Cloneable, ": Array lookup is not supported with features having arguments)"); bindTargets.add(node.toString()); } - else if (node instanceof CompositeNode) { - CompositeNode cNode = (CompositeNode)node; + else if (node instanceof CompositeNode cNode) { for (ExpressionNode child : cNode.children()) extractBindTargets(child, bindTargets); } diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Arguments.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Arguments.java index e770e6ac038..b716b693011 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Arguments.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/Arguments.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.rankingexpression.rule; -import com.google.common.collect.ImmutableList; import com.yahoo.searchlib.rankingexpression.evaluation.Context; import com.yahoo.searchlib.rankingexpression.evaluation.Value; @@ -18,27 +17,23 @@ public final class Arguments implements Serializable { public static final Arguments EMPTY = new Arguments(); - private final ImmutableList<ExpressionNode> expressions; + private final List<ExpressionNode> expressions; public Arguments() { - this(ImmutableList.of()); + this(List.of()); } public Arguments(ExpressionNode singleArgument) { - this(ImmutableList.of(singleArgument)); + this(List.of(singleArgument)); } public Arguments(List<? extends ExpressionNode> expressions) { if (expressions == null) { - this.expressions = ImmutableList.of(); + this.expressions = List.of(); return; } - // Build in a roundabout way because java generics and lists - ImmutableList.Builder<ExpressionNode> b = ImmutableList.builder(); - for (ExpressionNode node : expressions) - b.add(node); - this.expressions = b.build(); + this.expressions = List.copyOf(expressions); } /** Returns an unmodifiable list of the expressions in this, never null */ diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/LambdaFunctionNode.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/LambdaFunctionNode.java index 97e9a74f9c8..9c0c0e46804 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/LambdaFunctionNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/LambdaFunctionNode.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.rankingexpression.rule; -import com.google.common.collect.ImmutableList; import com.yahoo.searchlib.rankingexpression.Reference; import com.yahoo.searchlib.rankingexpression.evaluation.Context; import com.yahoo.searchlib.rankingexpression.evaluation.MapContext; @@ -27,7 +26,7 @@ import java.util.stream.Collectors; */ public class LambdaFunctionNode extends CompositeNode { - private final ImmutableList<String> arguments; + private final List<String> arguments; private final ExpressionNode functionExpression; public LambdaFunctionNode(List<String> arguments, ExpressionNode functionExpression) { @@ -37,7 +36,7 @@ public class LambdaFunctionNode extends CompositeNode { .filter(f -> ! arguments.contains(f)) .collect(Collectors.joining(", "))); } - this.arguments = ImmutableList.copyOf(arguments); + this.arguments = List.copyOf(arguments); this.functionExpression = functionExpression; } @@ -106,15 +105,12 @@ public class LambdaFunctionNode extends CompositeNode { } private Optional<DoubleBinaryOperator> getDirectEvaluator() { - if ( ! (functionExpression instanceof OperationNode)) { + if ( ! (functionExpression instanceof OperationNode node)) { return Optional.empty(); } - OperationNode node = (OperationNode) functionExpression; - if ( ! (node.children().get(0) instanceof ReferenceNode) || ! (node.children().get(1) instanceof ReferenceNode)) { + if ( ! (node.children().get(0) instanceof ReferenceNode lhs) || ! (node.children().get(1) instanceof ReferenceNode rhs)) { return Optional.empty(); } - var lhs = (ReferenceNode) node.children().get(0); - var rhs = (ReferenceNode) node.children().get(1); if (! lhs.getName().equals(arguments.get(0)) || ! rhs.getName().equals(arguments.get(1))) { return Optional.empty(); } @@ -122,17 +118,17 @@ public class LambdaFunctionNode extends CompositeNode { return Optional.empty(); } Operator operator = node.operators().get(0); - switch (operator) { - case or: return asFunctionExpression((left, right) -> ((left != 0.0) || (right != 0.0)) ? 1.0 : 0.0); - case and: return asFunctionExpression((left, right) -> ((left != 0.0) && (right != 0.0)) ? 1.0 : 0.0); - case plus: return asFunctionExpression((left, right) -> left + right); - case minus: return asFunctionExpression((left, right) -> left - right); - case multiply: return asFunctionExpression((left, right) -> left * right); - case divide: return asFunctionExpression((left, right) -> left / right); - case modulo: return asFunctionExpression((left, right) -> left % right); - case power: return asFunctionExpression(Math::pow); - } - return Optional.empty(); + return switch (operator) { + case or -> asFunctionExpression((left, right) -> ((left != 0.0) || (right != 0.0)) ? 1.0 : 0.0); + case and -> asFunctionExpression((left, right) -> ((left != 0.0) && (right != 0.0)) ? 1.0 : 0.0); + case plus -> asFunctionExpression((left, right) -> left + right); + case minus -> asFunctionExpression((left, right) -> left - right); + case multiply -> asFunctionExpression((left, right) -> left * right); + case divide -> asFunctionExpression((left, right) -> left / right); + case modulo -> asFunctionExpression((left, right) -> left % right); + case power -> asFunctionExpression(Math::pow); + default -> Optional.empty(); + }; } private Optional<DoubleBinaryOperator> asFunctionExpression(DoubleBinaryOperator operator) { diff --git a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/SetMembershipNode.java b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/SetMembershipNode.java index 5da2fbfe624..6b87f75d884 100644 --- a/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/SetMembershipNode.java +++ b/searchlib/src/main/java/com/yahoo/searchlib/rankingexpression/rule/SetMembershipNode.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.rankingexpression.rule; -import com.google.common.collect.ImmutableList; import com.yahoo.searchlib.rankingexpression.Reference; import com.yahoo.searchlib.rankingexpression.evaluation.BooleanValue; import com.yahoo.searchlib.rankingexpression.evaluation.Context; @@ -30,7 +29,7 @@ public class SetMembershipNode extends BooleanNode { public SetMembershipNode(ExpressionNode testValue, List<ExpressionNode> setValues) { this.testValue = testValue; - this.setValues = ImmutableList.copyOf(setValues); + this.setValues = List.copyOf(setValues); } /** The value to check for membership in the set */ diff --git a/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/test/FieldMatchMetricsTestCase.java b/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/test/FieldMatchMetricsTestCase.java index f4a003868f8..41a6cd69878 100644 --- a/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/test/FieldMatchMetricsTestCase.java +++ b/searchlib/src/test/java/com/yahoo/searchlib/ranking/features/fieldmatch/test/FieldMatchMetricsTestCase.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.searchlib.ranking.features.fieldmatch.test; -import com.google.common.collect.ImmutableList; import com.yahoo.searchlib.ranking.features.fieldmatch.Field; import com.yahoo.searchlib.ranking.features.fieldmatch.FieldMatchMetrics; import com.yahoo.searchlib.ranking.features.fieldmatch.FieldMatchMetricsComputer; @@ -10,6 +9,7 @@ import com.yahoo.searchlib.ranking.features.fieldmatch.QueryTerm; import com.yahoo.searchlib.ranking.features.fieldmatch.Query; import org.junit.Test; +import java.util.ArrayList; import java.util.List; import static org.junit.Assert.assertEquals; @@ -776,9 +776,9 @@ public class FieldMatchMetricsTestCase { } private Field toField(String fieldString) { - if (fieldString.length() == 0) return new Field(ImmutableList.of()); + if (fieldString.length() == 0) return new Field(List.of()); - ImmutableList.Builder<Field.Term> terms = new ImmutableList.Builder<>(); + List<Field.Term> terms = new ArrayList<>(); for (String termString : fieldString.split(" ")) { String[] colonSplit = termString.split(":"); if (colonSplit.length > 1) @@ -786,7 +786,7 @@ public class FieldMatchMetricsTestCase { else terms.add(new Field.Term(colonSplit[0])); } - return new Field(terms.build()); + return new Field(List.copyOf(terms)); } } |