Skip to content

Commit

Permalink
chore(perf): Avoid coping node when invoking with same selector.
Browse files Browse the repository at this point in the history
  • Loading branch information
nstdio committed May 1, 2024
1 parent 0bf773b commit 23d0a53
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/cz/jirutka/rsql/parser/ast/ComparisonNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ public String getSelector() {
* @return a copy of this node with the specified selector.
*/
public ComparisonNode withSelector(String newSelector) {
return new ComparisonNode(operator, newSelector, arguments, true);
return selector.equals(newSelector)
? this
: new ComparisonNode(operator, newSelector, arguments, true);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,17 @@ class ComparisonNodeTest extends Specification {
argsField.get(actual2) is(rawArguments)
}

def 'should not copy node when not needed'() {
given:
def node = new ComparisonNode(IN, 'genres', ['thriller', 'sci-fi'])

when:
def actual1 = node.withSelector(node.selector)

then:
actual1.is(node)
}

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

0 comments on commit 23d0a53

Please sign in to comment.