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
-
public class DeterministicCodeOptimizer extends ClassDeclarationFinder
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
Fields Modifier and Type Field Description protected java.util.Map<Expression,java.lang.Boolean>
constants
The map contains known to be effectively-final expression.protected java.util.Map<Expression,ParameterExpression>
dedup
The map that de-duplicates expressions, so the same expressions may reuse the same final static fields.private static java.util.Set<java.lang.Class>
DETERMINISTIC_CLASSES
private static java.lang.String
FIELD_PREFIX
protected java.util.Map<java.lang.String,ParameterExpression>
fieldsByName
The map of all the added final static fields.private static java.util.regex.Pattern
NON_ASCII
private static java.util.regex.Pattern
PREFIX_PATTERN
-
Fields inherited from class org.apache.calcite.linq4j.tree.ClassDeclarationFinder
addedDeclarations, parent
-
-
Constructor Summary
Constructors Constructor Description DeterministicCodeOptimizer(ClassDeclarationFinder parent)
Creates a child optimizer.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected boolean
allMethodsDeterministic(java.lang.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 ParameterExpression
findDeclaredExpression(Expression expression)
Finds if there exists ready for reuse declaration for given expression.private <C> java.lang.reflect.Constructor<C>
getConstructor(java.lang.Class<C> klass)
protected DeterministicCodeOptimizer
goDeeper()
Creates child visitor.protected boolean
hasField(java.lang.String name)
Verifies if the variable name is already in use.protected java.lang.String
inventFieldName(Expression expression)
Generates field name to store given expression.protected boolean
isConstant(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(java.lang.reflect.Method method)
Checks if given method is deterministic (i.e.protected void
learnFinalStaticDeclarations(java.util.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,Expression
visit(BinaryExpression binaryExpression, Expression expression0, Expression expression1)
MemberDeclaration
visit(FieldDeclaration fieldDeclaration, Expression initializer)
Expression
visit(MemberExpression memberExpression, Expression expression)
Expression
visit(MethodCallExpression methodCallExpression, Expression targetExpression, java.util.List<Expression> expressions)
Expression
visit(TernaryExpression ternaryExpression, Expression expression0, Expression expression1, Expression expression2)
Expression
visit(TypeBinaryExpression typeBinaryExpression, Expression 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, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit, visit
-
-
-
-
Field Detail
-
constants
protected final java.util.Map<Expression,java.lang.Boolean> 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
protected final java.util.Map<Expression,ParameterExpression> dedup
The map that de-duplicates expressions, so the same expressions may reuse the same final static fields.
-
fieldsByName
protected final java.util.Map<java.lang.String,ParameterExpression> fieldsByName
The map of all the added final static fields. Allows to identify if the name is occupied or not.
-
NON_ASCII
private static final java.util.regex.Pattern NON_ASCII
-
FIELD_PREFIX
private static final java.lang.String FIELD_PREFIX
- See Also:
- Constant Field Values
-
PREFIX_PATTERN
private static final java.util.regex.Pattern PREFIX_PATTERN
-
DETERMINISTIC_CLASSES
private static final java.util.Set<java.lang.Class> DETERMINISTIC_CLASSES
-
-
Constructor Detail
-
DeterministicCodeOptimizer
public DeterministicCodeOptimizer(ClassDeclarationFinder parent)
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 Detail
-
tryOptimizeNewInstance
protected Expression tryOptimizeNewInstance(NewExpression newExpression)
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
public Expression visit(UnaryExpression unaryExpression, Expression expression)
-
visit
public Expression visit(TypeBinaryExpression typeBinaryExpression, Expression expression)
-
tryOptimizeMethodCall
protected Expression tryOptimizeMethodCall(MethodCallExpression methodCallExpression)
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, Expression targetExpression, java.util.List<Expression> expressions)
-
visit
public Expression visit(MemberExpression memberExpression, Expression expression)
-
visit
public MemberDeclaration visit(FieldDeclaration fieldDeclaration, Expression initializer)
-
learnFinalStaticDeclarations
protected void learnFinalStaticDeclarations(java.util.List<MemberDeclaration> memberDeclarations)
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
protected ParameterExpression findDeclaredExpression(Expression expression)
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
protected Expression createField(Expression expression)
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
protected java.lang.String inventFieldName(Expression expression)
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
protected boolean isConstant(Expression expression)
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
protected boolean isMethodDeterministic(java.lang.reflect.Method method)
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
protected boolean isConstructorDeterministic(NewExpression newExpression)
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
-
getConstructor
private <C> java.lang.reflect.Constructor<C> getConstructor(java.lang.Class<C> klass)
-
allMethodsDeterministic
protected boolean allMethodsDeterministic(java.lang.Class klass)
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
protected boolean hasField(java.lang.String name)
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
protected DeterministicCodeOptimizer 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
-
-