forked from jirutka/rsql-parser
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Use trusted constructors when creating nodes from parser.
This commit provides a few memory optimizations that relies on fact that we trust the internal code that invokes `NodesFactory.createLogicalNode` and `NodesFactory.createComparisonNode`.
- Loading branch information
Showing
7 changed files
with
200 additions
and
5 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
src/main/java/cz/jirutka/rsql/parser/NodesFactoryAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package cz.jirutka.rsql.parser; | ||
|
||
import static java.util.logging.Level.WARNING; | ||
|
||
import cz.jirutka.rsql.parser.ast.ComparisonNode; | ||
import cz.jirutka.rsql.parser.ast.LogicalNode; | ||
import cz.jirutka.rsql.parser.ast.LogicalOperator; | ||
import cz.jirutka.rsql.parser.ast.Node; | ||
import cz.jirutka.rsql.parser.ast.NodesFactory; | ||
import java.lang.invoke.MethodHandle; | ||
import java.lang.invoke.MethodHandles; | ||
import java.lang.invoke.MethodHandles.Lookup; | ||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
import java.util.logging.Logger; | ||
|
||
final class NodesFactoryAccess { | ||
|
||
private NodesFactoryAccess() { | ||
} | ||
|
||
private static final MethodHandle LOGICAL_NODE_MH; | ||
private static final MethodHandle COMP_NODE_MH; | ||
|
||
static { | ||
Lookup lookup = MethodHandles.lookup(); | ||
|
||
LOGICAL_NODE_MH = methodHandle(lookup, "logicalNodeTrusted", LogicalOperator.class, | ||
List.class); | ||
COMP_NODE_MH = methodHandle(lookup, "comparisonNodeTrusted", String.class, String.class, | ||
List.class); | ||
} | ||
|
||
private static MethodHandle methodHandle(Lookup lookup, String name, Class<?>... parameterTypes) { | ||
Method m = null; | ||
MethodHandle mh = null; | ||
|
||
try { | ||
m = NodesFactory.class.getDeclaredMethod(name, parameterTypes); | ||
m.setAccessible(true); | ||
|
||
mh = lookup.unreflect(m); | ||
} catch (Throwable e) { | ||
logger().log(WARNING, "Unable to initialize MethodHandle for {0}", new Object[]{name, e}); | ||
} finally { | ||
if (m != null) { | ||
m.setAccessible(false); | ||
} | ||
} | ||
|
||
return mh; | ||
} | ||
|
||
static LogicalNode create(NodesFactory factory, LogicalOperator operator, List<Node> children) { | ||
if (LOGICAL_NODE_MH == null) { | ||
return factory.createLogicalNode(operator, children); | ||
} else { | ||
try { | ||
return (LogicalNode) LOGICAL_NODE_MH.invoke(factory, operator, children); | ||
} catch (RuntimeException e) { | ||
throw e; | ||
} catch (Throwable e) { | ||
logger().log(WARNING, "The logicalNodeTrusted unexpectedly thrown exception", e); | ||
|
||
return factory.createLogicalNode(operator, children); | ||
} | ||
} | ||
} | ||
|
||
static ComparisonNode create(NodesFactory factory, String operatorToken, String selector, List<String> arguments) | ||
throws UnknownOperatorException { | ||
if (COMP_NODE_MH == null) { | ||
return factory.createComparisonNode(operatorToken, selector, arguments); | ||
} else { | ||
try { | ||
return (ComparisonNode) COMP_NODE_MH.invoke(factory, operatorToken, selector, arguments); | ||
} catch (RuntimeException | UnknownOperatorException e) { | ||
throw e; | ||
} catch (Throwable e) { | ||
logger().log(WARNING, "The comparisonNodeTrusted unexpectedly thrown exception", e); | ||
|
||
return factory.createComparisonNode(operatorToken, selector, arguments); | ||
} | ||
} | ||
} | ||
|
||
private static Logger logger() { | ||
return Logger.getLogger(NodesFactoryAccess.class.getName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
src/test/groovy/cz/jirutka/rsql/parser/NodesFactoryAccessSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package cz.jirutka.rsql.parser | ||
|
||
import cz.jirutka.rsql.parser.ast.ComparisonNode | ||
import cz.jirutka.rsql.parser.ast.LogicalOperator | ||
import cz.jirutka.rsql.parser.ast.NodesFactory | ||
import spock.lang.Specification | ||
|
||
import static cz.jirutka.rsql.parser.ast.RSQLOperators.* | ||
|
||
class NodesFactoryAccessSpec extends Specification { | ||
|
||
def 'Should use trusted constructors for creating logical node'(LogicalOperator op) { | ||
given: | ||
def factory = new NodesFactory(defaultOperators()) | ||
def children = [ | ||
new ComparisonNode(EQUAL, 'a1', ['b1']), | ||
new ComparisonNode(NOT_EQUAL, 'a2', ['b2']) | ||
] | ||
|
||
when: | ||
def actual = NodesFactoryAccess.create(factory, op, children) | ||
|
||
then: | ||
actual.children.size() == children.size() | ||
|
||
and: 'original is not modified by modifying copy' | ||
def childrenCopy = actual.children | ||
childrenCopy.removeLast() | ||
childrenCopy.size() + 1 == children.size() | ||
|
||
and: 'but when modifying original it must change in the LogicalNode' | ||
children.removeLast() | ||
|
||
actual.children.size() == children.size() | ||
|
||
where: | ||
op << LogicalOperator.values() | ||
} | ||
|
||
def 'Should use trusted constructors for creating comparison node'() { | ||
given: | ||
def factory = new NodesFactory(defaultOperators()) | ||
def arguments = ['x', 'y', 'z'] | ||
|
||
when: | ||
def actual = NodesFactoryAccess.create(factory, '=in=', 'a1', arguments) | ||
|
||
then: | ||
actual.arguments.size() == arguments.size() | ||
|
||
and: 'original is not modified by modifying copy' | ||
def childrenCopy = actual.arguments | ||
childrenCopy.removeLast() | ||
childrenCopy.size() + 1 == arguments.size() | ||
|
||
and: 'but when modifying original it must change in the LogicalNode' | ||
arguments.removeLast() | ||
|
||
actual.arguments.size() == arguments.size() | ||
} | ||
} |