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 Map<Expression,Boolean>constantsThe map contains known to be effectively-final expression.protected Map<Expression,ParameterExpression>dedupThe map that de-duplicates expressions, so the same expressions may reuse the same final static fields.private static Set<Class>DETERMINISTIC_CLASSESprivate static StringFIELD_PREFIXprotected Map<String,ParameterExpression>fieldsByNameThe map of all the added final static fields.private static PatternNON_ASCIIprivate static PatternPREFIX_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 booleanallMethodsDeterministic(Class klass)Checks if all the methods in given class are deterministic (i.e.protected ExpressioncreateField(Expression expression)Creates final static field to hold the given expression.protected ParameterExpressionfindDeclaredExpression(Expression expression)Finds if there exists ready for reuse declaration for given expression.private <C> Constructor<C>getConstructor(Class<C> klass)protected DeterministicCodeOptimizergoDeeper()Creates child visitor.protected booleanhasField(String name)Verifies if the variable name is already in use.protected StringinventFieldName(Expression expression)Generates field name to store given expression.protected booleanisConstant(Expression expression)Verifies if the expression is effectively constant.protected booleanisConstructorDeterministic(NewExpression newExpression)Checks if new instance creation can be reused.protected booleanisMethodDeterministic(Method method)Checks if given method is deterministic (i.e.protected voidlearnFinalStaticDeclarations(List<MemberDeclaration> memberDeclarations)Processes the list of declarations and learns final static ones as effectively constant.protected ExpressiontryOptimizeMethodCall(MethodCallExpression methodCallExpression)Optimized method call, possibly converting it to final static field.protected ExpressiontryOptimizeNewInstance(NewExpression newExpression)Optimizesnew Type()constructs,Expressionvisit(BinaryExpression binaryExpression, Expression expression0, Expression expression1)MemberDeclarationvisit(FieldDeclaration fieldDeclaration, Expression initializer)Expressionvisit(MemberExpression memberExpression, Expression expression)Expressionvisit(MethodCallExpression methodCallExpression, Expression targetExpression, List<Expression> expressions)Expressionvisit(TernaryExpression ternaryExpression, Expression expression0, Expression expression1, Expression expression2)Expressionvisit(TypeBinaryExpression typeBinaryExpression, Expression expression)Expressionvisit(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 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
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 Map<Expression,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 Map<Expression,ParameterExpression> dedup
The map that de-duplicates expressions, so the same expressions may reuse the same final static fields.
-
fieldsByName
protected final Map<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 Pattern NON_ASCII
-
FIELD_PREFIX
private static final String FIELD_PREFIX
- See Also:
- Constant Field Values
-
PREFIX_PATTERN
private static final Pattern PREFIX_PATTERN
-
-
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:
tryOptimizeNewInstancein 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, List<Expression> expressions)
-
visit
public Expression visit(MemberExpression memberExpression, Expression expression)
-
visit
public MemberDeclaration visit(FieldDeclaration fieldDeclaration, Expression initializer)
-
learnFinalStaticDeclarations
protected void learnFinalStaticDeclarations(List<MemberDeclaration> memberDeclarations)
Processes the list of declarations and learns final static ones as effectively constant.- Overrides:
learnFinalStaticDeclarationsin 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:
findDeclaredExpressionin 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 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.ConstantExpressionorParameterExpression). The method verifies parent chain since the expression might be defined in enclosing class.- Overrides:
isConstantin classClassDeclarationFinder- Parameters:
expression- expression to test- Returns:
- true when the expression is known to be constant
-
isMethodDeterministic
protected boolean isMethodDeterministic(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> Constructor<C> getConstructor(Class<C> klass)
-
allMethodsDeterministic
protected boolean allMethodsDeterministic(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(String name)
Verifies if the variable name is already in use. Only the variables that are explicitly added tofieldsByNameare verified. The method verifies parent chain.- Overrides:
hasFieldin 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:
goDeeperin classClassDeclarationFinder- Returns:
- new Visitor that is used to optimize class declarations
-
-