Class 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 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 is ParameterExpression, 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

        private static final java.util.regex.Pattern PREFIX_PATTERN

        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.
        parent - parent optimizer
    • Method Detail

      • tryOptimizeMethodCall

        protected Expression tryOptimizeMethodCall​(MethodCallExpression methodCallExpression)
        Optimized method call, possibly converting it to final static field.
        methodCallExpression - method call to optimize
        optimized expression
      • learnFinalStaticDeclarations

        protected void learnFinalStaticDeclarations​(java.util.List<MemberDeclaration> memberDeclarations)
        Processes the list of declarations and learns final static ones as effectively constant.
        learnFinalStaticDeclarations in class ClassDeclarationFinder
        memberDeclarations - list of declarations to search finals from
      • createField

        protected Expression createField​(Expression expression)
        Creates final static field to hold the given expression. The method might reuse existing declarations if appropriate.
        expression - expression to store in final field
        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.
        expression - input expression
        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 or ParameterExpression). The method verifies parent chain since the expression might be defined in enclosing class.
        isConstant in class ClassDeclarationFinder
        expression - expression to test
        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).
        method - method to test
        true when the method is deterministic
      • isConstructorDeterministic

        protected boolean isConstructorDeterministic​(NewExpression newExpression)
        Checks if new instance creation can be reused. For instance new BigInteger("42") is effectively final and can be reused.
        newExpression - method to test
        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)
        klass - class to test
        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 to fieldsByName are verified. The method verifies parent chain.
        hasField in class ClassDeclarationFinder
        name - name of the variable to test
        true if the name is used by one of static final fields