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 Details

    • constants

      protected final IdentityHashMap<Expression,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 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.
  • Constructor Details

    • 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 Details

    • tryOptimizeNewInstance

      protected Expression tryOptimizeNewInstance(NewExpression newExpression)
      Optimizes new Type() constructs.
      Overrides:
      tryOptimizeNewInstance in class ClassDeclarationFinder
      Parameters:
      newExpression - expression to optimize
      Returns:
      optimized expression
    • visit

      public Expression visit(BinaryExpression binaryExpression, Expression expression0, Expression expression1)
      Overrides:
      visit in class Shuttle
    • visit

      public Expression visit(TernaryExpression ternaryExpression, Expression expression0, Expression expression1, Expression expression2)
      Overrides:
      visit in class Shuttle
    • visit

      public Expression visit(UnaryExpression unaryExpression, Expression expression)
      Overrides:
      visit in class Shuttle
    • visit

      public Expression visit(TypeBinaryExpression typeBinaryExpression, Expression expression)
      Overrides:
      visit in class Shuttle
    • 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, @Nullable Expression targetExpression, List<Expression> expressions)
      Overrides:
      visit in class Shuttle
    • visit

      public Expression visit(MemberExpression memberExpression, @Nullable Expression expression)
      Overrides:
      visit in class Shuttle
    • visit

      public MemberDeclaration visit(FieldDeclaration fieldDeclaration, @Nullable Expression initializer)
      Overrides:
      visit in class Shuttle
    • learnFinalStaticDeclarations

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

      protected @Nullable ParameterExpression findDeclaredExpression(Expression expression)
      Finds if there exists ready for reuse declaration for given expression.
      Overrides:
      findDeclaredExpression in class ClassDeclarationFinder
      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(@Nullable 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.
      Overrides:
      isConstant in class ClassDeclarationFinder
      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 instance new BigInteger("42") is effectively final and can be reused.
      Parameters:
      newExpression - method to test
      Returns:
      true when the method is deterministic
    • 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 to fieldsByName are verified. The method verifies parent chain.
      Overrides:
      hasField in class ClassDeclarationFinder
      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 class ClassDeclarationFinder
      Returns:
      new Visitor that is used to optimize class declarations