Skip to content

Commit

Permalink
chore(perf): Avoid coping argument when invoking with methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
nstdio committed May 1, 2024
1 parent 2978a98 commit 0bf773b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/main/java/cz/jirutka/rsql/parser/ast/ComparisonNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,19 @@ public final class ComparisonNode extends AbstractNode {
* @throws IllegalArgumentException If one of the conditions specified above it not met.
*/
public ComparisonNode(ComparisonOperator operator, String selector, List<String> arguments) {
this(operator, selector, new ArrayList<>(arguments), true);
}

ComparisonNode(ComparisonOperator operator, String selector, List<String> arguments,
@SuppressWarnings("unused") boolean trusted) {
Assert.notNull(operator, "operator must not be null");
Assert.notBlank(selector, "selector must not be blank");
Assert.notNull(arguments, "arguments must not be null");
validate(operator, arguments.size());

this.operator = operator;
this.selector = selector;
this.arguments = new ArrayList<>(arguments);
this.arguments = arguments;
}

public <R, A> R accept(RSQLVisitor<R, A> visitor, A param) {
Expand All @@ -77,7 +82,7 @@ public ComparisonOperator getOperator() {
* @return a copy of this node with the specified operator.
*/
public ComparisonNode withOperator(ComparisonOperator newOperator) {
return new ComparisonNode(newOperator, selector, arguments);
return new ComparisonNode(newOperator, selector, arguments, true);
}

public String getSelector() {
Expand All @@ -91,7 +96,7 @@ public String getSelector() {
* @return a copy of this node with the specified selector.
*/
public ComparisonNode withSelector(String newSelector) {
return new ComparisonNode(operator, newSelector, arguments);
return new ComparisonNode(operator, newSelector, arguments, true);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ class ComparisonNodeTest extends Specification {
node.getArguments() == ['thriller', 'sci-fi']
}

def 'should not copy list when not needed'() {
given:
def args = ['thriller', 'sci-fi']
def node = new ComparisonNode(IN, 'genres', args)
def argsField = ComparisonNode.getDeclaredField('arguments')
argsField.setAccessible(true)
def rawArguments = argsField.get(node)

when:
def actual1 = node.withSelector('gs')

then:
argsField.get(actual1) is(rawArguments)

when:
def actual2 = actual1.withOperator(NOT_IN)

then:
argsField.get(actual2) is(rawArguments)
}

def 'should create proper toString representation'() {
expect:
node.toString() == expected
Expand Down

0 comments on commit 0bf773b

Please sign in to comment.