Package org.apache.calcite.linq4j.tree
Class DeterministicCodeOptimizer
java.lang.Object
org.apache.calcite.linq4j.tree.Shuttle
org.apache.calcite.linq4j.tree.ClassDeclarationFinder
org.apache.calcite.linq4j.tree.DeterministicCodeOptimizer
Factors out deterministic expressions to final static fields.
Instances of this class should not be reused, so new visitor should be
created for optimizing a new expression tree.
-
Field Summary
Modifier and TypeFieldDescriptionprotected final IdentityHashMap<Expression,
Boolean> The map contains known to be effectively-final expression.protected final Map<Expression,
ParameterExpression> The map that de-duplicates expressions, so the same expressions may reuse the same final static fields.protected final Map<String,
ParameterExpression> The map of all the added final static fields.Fields inherited from class org.apache.calcite.linq4j.tree.ClassDeclarationFinder
addedDeclarations, parent
-
Constructor Summary
ConstructorDescriptionCreates a child optimizer. -
Method Summary
Modifier and TypeMethodDescriptionprotected boolean
allMethodsDeterministic
(Class klass) Checks if all the methods in given class are deterministic (i.e.protected Expression
createField
(Expression expression) Creates final static field to hold the given expression.protected @Nullable ParameterExpression
findDeclaredExpression
(Expression expression) Finds if there exists ready for reuse declaration for given expression.protected DeterministicCodeOptimizer
goDeeper()
Creates child visitor.protected boolean
Verifies if the variable name is already in use.protected String
inventFieldName
(Expression expression) Generates field name to store given expression.protected boolean
isConstant
(@Nullable Expression expression) Verifies if the expression is effectively constant.protected boolean
isConstructorDeterministic
(NewExpression newExpression) Checks if new instance creation can be reused.protected boolean
isMethodDeterministic
(Method method) Checks if given method is deterministic (i.e.protected void
learnFinalStaticDeclarations
(List<MemberDeclaration> memberDeclarations) Processes the list of declarations and learns final static ones as effectively constant.protected Expression
tryOptimizeMethodCall
(MethodCallExpression methodCallExpression) Optimized method call, possibly converting it to final static field.protected Expression
tryOptimizeNewInstance
(NewExpression newExpression) Optimizesnew Type()
constructs.visit
(BinaryExpression binaryExpression, Expression expression0, Expression expression1) visit
(FieldDeclaration fieldDeclaration, @Nullable Expression initializer) visit
(MemberExpression memberExpression, @Nullable Expression expression) visit
(MethodCallExpression methodCallExpression, @Nullable Expression targetExpression, List<Expression> expressions) visit
(TernaryExpression ternaryExpression, Expression expression0, Expression expression1, Expression expression2) visit
(TypeBinaryExpression typeBinaryExpression, Expression expression) visit
(UnaryExpression unaryExpression, Expression expression) Methods inherited from class org.apache.calcite.linq4j.tree.ClassDeclarationFinder
create, create, create, isConstant, optimizeDeclarations, preVisit, preVisit, visit, visit
Methods inherited from class org.apache.calcite.linq4j.tree.Shuttle
preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, preVisit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit
-
Field Details
-
constants
The map contains known to be effectively-final expression. The map uses identity equality. Typically the key isParameterExpression
, however there might be non-factored to final field expression that is known to be constant. For instance, cast expression will not be factored to a field, but we still need to track its constant status. -
dedup
The map that de-duplicates expressions, so the same expressions may reuse the same final static fields. -
fieldsByName
The map of all the added final static fields. Allows to identify if the name is occupied or not.
-
-
Constructor Details
-
DeterministicCodeOptimizer
Creates a child optimizer. Typically a child is created for each class declaration, so each optimizer collects fields for exactly one class.- Parameters:
parent
- parent optimizer
-
-
Method Details
-
tryOptimizeNewInstance
Optimizesnew Type()
constructs.- Overrides:
tryOptimizeNewInstance
in classClassDeclarationFinder
- Parameters:
newExpression
- expression to optimize- Returns:
- optimized expression
-
visit
public Expression visit(BinaryExpression binaryExpression, Expression expression0, Expression expression1) -
visit
public Expression visit(TernaryExpression ternaryExpression, Expression expression0, Expression expression1, Expression expression2) -
visit
-
visit
-
tryOptimizeMethodCall
Optimized method call, possibly converting it to final static field.- Parameters:
methodCallExpression
- method call to optimize- Returns:
- optimized expression
-
visit
public Expression visit(MethodCallExpression methodCallExpression, @Nullable Expression targetExpression, List<Expression> expressions) -
visit
-
visit
-
learnFinalStaticDeclarations
Processes the list of declarations and learns final static ones as effectively constant.- Overrides:
learnFinalStaticDeclarations
in classClassDeclarationFinder
- Parameters:
memberDeclarations
- list of declarations to search finals from
-
findDeclaredExpression
Finds if there exists ready for reuse declaration for given expression.- Overrides:
findDeclaredExpression
in classClassDeclarationFinder
- Parameters:
expression
- input expression- Returns:
- parameter of the already existing declaration, or null
-
createField
Creates final static field to hold the given expression. The method might reuse existing declarations if appropriate.- Parameters:
expression
- expression to store in final field- Returns:
- expression for the given input expression
-
inventFieldName
Generates field name to store given expression. The expression is converted to string and all the non-ascii/numeric characters are replaced with underscores and"_$L4J$C$"
suffix is added to avoid conflicts with other variables. When multiple variables are mangled to the same name, counter is used to avoid conflicts.- Parameters:
expression
- input expression- Returns:
- unique name to store given expression
-
isConstant
Verifies if the expression is effectively constant. It is assumed the expression is simple (e.g.ConstantExpression
orParameterExpression
). The method verifies parent chain since the expression might be defined in enclosing class.- Overrides:
isConstant
in classClassDeclarationFinder
- Parameters:
expression
- expression to test- Returns:
- true when the expression is known to be constant
-
isMethodDeterministic
Checks if given method is deterministic (i.e. returns the same output given the same inputs).- Parameters:
method
- method to test- Returns:
- true when the method is deterministic
-
isConstructorDeterministic
Checks if new instance creation can be reused. For instancenew BigInteger("42")
is effectively final and can be reused.- Parameters:
newExpression
- method to test- Returns:
- true when the method is deterministic
-
allMethodsDeterministic
Checks if all the methods in given class are deterministic (i.e. return the same value given the same inputs)- Parameters:
klass
- class to test- Returns:
- true when all the methods including constructors are deterministic
-
hasField
Verifies if the variable name is already in use. Only the variables that are explicitly added tofieldsByName
are verified. The method verifies parent chain.- Overrides:
hasField
in classClassDeclarationFinder
- Parameters:
name
- name of the variable to test- Returns:
- true if the name is used by one of static final fields
-
goDeeper
Creates child visitor. It is used to traverse nested class declarations.- Overrides:
goDeeper
in classClassDeclarationFinder
- Returns:
- new Visitor that is used to optimize class declarations
-