From 7e8209d06e1b67c3d1f5c05acc8c3f0a19234380 Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Wed, 25 Jan 2023 16:48:26 +0100 Subject: Replace synchronized Stack with Deque in feed and query path. --- .../document/annotation/RecursiveNodeIterator.java | 9 ++-- .../yahoo/document/select/rule/ArithmeticNode.java | 62 ++++++++-------------- .../com/yahoo/document/select/rule/LogicNode.java | 56 +++++++++---------- 3 files changed, 51 insertions(+), 76 deletions(-) (limited to 'document/src/main/java') diff --git a/document/src/main/java/com/yahoo/document/annotation/RecursiveNodeIterator.java b/document/src/main/java/com/yahoo/document/annotation/RecursiveNodeIterator.java index 775ce41d303..faae78ff8ee 100644 --- a/document/src/main/java/com/yahoo/document/annotation/RecursiveNodeIterator.java +++ b/document/src/main/java/com/yahoo/document/annotation/RecursiveNodeIterator.java @@ -1,9 +1,10 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.document.annotation; +import java.util.ArrayDeque; +import java.util.Deque; import java.util.ListIterator; import java.util.NoSuchElementException; -import java.util.Stack; /** * ListIterator implementation which performs a depth-first traversal of SpanNodes. @@ -11,11 +12,11 @@ import java.util.Stack; * @author Einar M R Rosenvinge */ class RecursiveNodeIterator implements ListIterator { - protected Stack> stack = new Stack>(); + protected Deque> stack = new ArrayDeque<>(); protected ListIterator iteratorFromLastCallToNext = null; RecursiveNodeIterator(ListIterator it) { - stack.push(new PeekableListIterator(it)); + stack.push(new PeekableListIterator<>(it)); } protected RecursiveNodeIterator() { @@ -38,7 +39,7 @@ class RecursiveNodeIterator implements ListIterator { if (!iterator.traversed) { //we set the traversed flag on our way down iterator.traversed = true; - stack.push(new PeekableListIterator(node.childIterator())); + stack.push(new PeekableListIterator<>(node.childIterator())); return hasNext(); } diff --git a/document/src/main/java/com/yahoo/document/select/rule/ArithmeticNode.java b/document/src/main/java/com/yahoo/document/select/rule/ArithmeticNode.java index b160293440e..76de41fc39e 100644 --- a/document/src/main/java/com/yahoo/document/select/rule/ArithmeticNode.java +++ b/document/src/main/java/com/yahoo/document/select/rule/ArithmeticNode.java @@ -8,9 +8,10 @@ import com.yahoo.document.select.Context; import com.yahoo.document.select.Result; import com.yahoo.document.select.Visitor; +import java.util.ArrayDeque; +import java.util.Deque; import java.util.List; import java.util.ArrayList; -import java.util.Stack; /** * @author Simon Thoresen Hult @@ -47,7 +48,7 @@ public class ArithmeticNode implements ExpressionNode { @Override public Object evaluate(Context context) { StringBuilder ret = null; - Stack buf = new Stack<>(); + Deque buf = new ArrayDeque<>(); for (int i = 0; i < items.size(); ++i) { NodeItem item = items.get(i); Object val = item.node.evaluate(context); @@ -56,8 +57,7 @@ public class ArithmeticNode implements ExpressionNode { throw new IllegalArgumentException("Can not perform arithmetic on null value (referencing missing field?)"); } - if (val instanceof AttributeNode.VariableValueList) { - AttributeNode.VariableValueList value = (AttributeNode.VariableValueList)val; + if (val instanceof AttributeNode.VariableValueList value) { if (value.size() == 0) { throw new IllegalArgumentException("Can not perform arithmetic on missing field: " + item.node.toString()); @@ -102,27 +102,16 @@ public class ArithmeticNode implements ExpressionNode { return buf.pop().value; } - private void popOffTheTop(Stack buf) { + private void popOffTheTop(Deque buf) { ValueItem rhs = buf.pop(); ValueItem lhs = buf.pop(); switch (rhs.operator) { - case ADD: - lhs.value = lhs.value.doubleValue() + rhs.value.doubleValue(); - break; - case SUB: - lhs.value = lhs.value.doubleValue() - rhs.value.doubleValue(); - break; - case DIV: - lhs.value = lhs.value.doubleValue() / rhs.value.doubleValue(); - break; - case MUL: - lhs.value = lhs.value.doubleValue() * rhs.value.doubleValue(); - break; - case MOD: - lhs.value = lhs.value.longValue() % rhs.value.longValue(); - break; - default: - throw new IllegalStateException("Arithmetic operator " + rhs.operator + " not supported."); + case ADD -> lhs.value = lhs.value.doubleValue() + rhs.value.doubleValue(); + case SUB -> lhs.value = lhs.value.doubleValue() - rhs.value.doubleValue(); + case DIV -> lhs.value = lhs.value.doubleValue() / rhs.value.doubleValue(); + case MUL -> lhs.value = lhs.value.doubleValue() * rhs.value.doubleValue(); + case MOD -> lhs.value = lhs.value.longValue() % rhs.value.longValue(); + default -> throw new IllegalStateException("Arithmetic operator " + rhs.operator + " not supported."); } buf.push(lhs); } @@ -140,22 +129,15 @@ public class ArithmeticNode implements ExpressionNode { } public String operatorToString(int operator) { - switch (operator) { - case NOP: - return null; - case ADD: - return "+"; - case SUB: - return "-"; - case MOD: - return "%"; - case DIV: - return "/"; - case MUL: - return "*"; - default: - throw new IllegalStateException("Arithmetic operator " + operator + " not supported."); - } + return switch (operator) { + case NOP -> null; + case ADD -> "+"; + case SUB -> "-"; + case MOD -> "%"; + case DIV -> "/"; + case MUL -> "*"; + default -> throw new IllegalStateException("Arithmetic operator " + operator + " not supported."); + }; } private int stringToOperator(String operator) { @@ -192,8 +174,8 @@ public class ArithmeticNode implements ExpressionNode { } public static class NodeItem { - private int operator; - private ExpressionNode node; + private final int operator; + private final ExpressionNode node; NodeItem(int operator, ExpressionNode node) { this.operator = operator; diff --git a/document/src/main/java/com/yahoo/document/select/rule/LogicNode.java b/document/src/main/java/com/yahoo/document/select/rule/LogicNode.java index 5cfcef2a5e5..f97ebb20c28 100644 --- a/document/src/main/java/com/yahoo/document/select/rule/LogicNode.java +++ b/document/src/main/java/com/yahoo/document/select/rule/LogicNode.java @@ -7,9 +7,10 @@ import com.yahoo.document.select.Context; import com.yahoo.document.select.ResultList; import com.yahoo.document.select.Visitor; +import java.util.ArrayDeque; import java.util.ArrayList; +import java.util.Deque; import java.util.List; -import java.util.Stack; /** * This class defines a logical expression of nodes. This implementation uses a stack to evaluate its content as to @@ -56,7 +57,7 @@ public class LogicNode implements ExpressionNode { @Override public BucketSet getBucketSet(BucketIdFactory factory) { - Stack buf = new Stack<>(); + Deque buf = new ArrayDeque<>(); for (NodeItem item : items) { if (!buf.isEmpty()) { while (buf.peek().operator > item.operator) { @@ -76,11 +77,11 @@ public class LogicNode implements ExpressionNode { * * @param buf The stack of bucket items. */ - private void combineBuckets(Stack buf) { + private void combineBuckets(Deque buf) { BucketItem rhs = buf.pop(); BucketItem lhs = buf.pop(); switch (rhs.operator) { - case AND: + case AND -> { if (lhs.buckets == null) { lhs.buckets = rhs.buckets; } else if (rhs.buckets == null) { @@ -88,8 +89,8 @@ public class LogicNode implements ExpressionNode { } else { lhs.buckets = lhs.buckets.intersection(rhs.buckets); } - break; - case OR: + } + case OR -> { if (lhs.buckets == null) { // empty } else if (rhs.buckets == null) { @@ -97,16 +98,15 @@ public class LogicNode implements ExpressionNode { } else { lhs.buckets = lhs.buckets.union(rhs.buckets); } - break; - default: - throw new IllegalStateException("Arithmetic operator " + rhs.operator + " not supported."); + } + default -> throw new IllegalStateException("Arithmetic operator " + rhs.operator + " not supported."); } buf.push(lhs); } @Override public Object evaluate(Context context) { - Stack buf = new Stack<>(); + Deque buf = new ArrayDeque<>(); for (NodeItem item : items) { if ( buf.size() > 1) { while ((buf.peek().getOperator() >= item.operator)) { @@ -126,7 +126,7 @@ public class LogicNode implements ExpressionNode { * * @param buf The stack of values. */ - private void combineValues(Stack buf) { + private void combineValues(Deque buf) { ValueItem rhs = buf.pop(); ValueItem lhs = buf.pop(); buf.push(new LazyCombinedItem(lhs, rhs)); @@ -156,16 +156,12 @@ public class LogicNode implements ExpressionNode { * @return The string representation. */ private String operatorToString(int operator) { - switch (operator) { - case NOP: - return null; - case OR: - return "or"; - case AND: - return "and"; - default: - throw new IllegalStateException("Logical operator " + operator + " not supported."); - } + return switch (operator) { + case NOP -> null; + case OR -> "or"; + case AND -> "and"; + default -> throw new IllegalStateException("Logical operator " + operator + " not supported."); + }; } /** @@ -231,14 +227,10 @@ public class LogicNode implements ExpressionNode { public ResultList getResult() { if (lazyResult == null) { switch (rhs.getOperator()) { - case AND: - lazyResult = lhs.getResult().combineAND(rhs); - break; - case OR: - lazyResult = lhs.getResult().combineOR(rhs); - break; - default: - throw new IllegalStateException("Logical operator " + rhs.getOperator() + " not supported."); + case AND -> lazyResult = lhs.getResult().combineAND(rhs); + case OR -> lazyResult = lhs.getResult().combineOR(rhs); + default -> + throw new IllegalStateException("Logical operator " + rhs.getOperator() + " not supported."); } } return lazyResult; @@ -249,7 +241,7 @@ public class LogicNode implements ExpressionNode { * Private class to store bucket sets in a stack. */ private static final class BucketItem { - private int operator; + final private int operator; private BucketSet buckets; BucketItem(int operator, BucketSet buckets) { @@ -262,8 +254,8 @@ public class LogicNode implements ExpressionNode { * Private class to store expression nodes in a stack. */ public static final class NodeItem { - private int operator; - private ExpressionNode node; + final private int operator; + final private ExpressionNode node; NodeItem(int operator, ExpressionNode node) { this.operator = operator; -- cgit v1.2.3