Class AdaptiveAstOptimizer

java.lang.Object
com.helix.core.bytecode.AdaptiveAstOptimizer

public class AdaptiveAstOptimizer extends Object
Adaptive AST Optimizer dynamically reordering commutative boolean clauses based on live cost-to-failure ratios (C_i / F_i) or pluggable ReorderingPolicy SPI, and executing zero-downtime bytecode hot-swapping into tiered rule caches.

In short-circuiting conjunctive queries (AND chains), evaluating clauses with the lowest C_i / F_i ratio first minimizes the expected total evaluation cost by maximizing the probability of early short-circuiting prior to evaluating expensive operations such as ML inferences.

  • Constructor Details

  • Method Details

    • getReorderingPolicy

      public ReorderingPolicy getReorderingPolicy()
    • optimize

      public ExpressionNode optimize(ExpressionNode astRoot)
      Optimizes the AST root for an unnamed or ad-hoc rule.
      Parameters:
      astRoot - input AST root
      Returns:
      optimized AST root
    • optimize

      public ExpressionNode optimize(String ruleName, ExpressionNode astRoot)
      Recursively optimizes the AST expression tree by reordering commutative AND chains in ascending order of their cost-to-failure ratios.
      Parameters:
      ruleName - name of the rule
      astRoot - input AST root
      Returns:
      reordered and optimized AST root
    • computeRatio

      public double computeRatio(String ruleName, ExpressionNode node)
      Computes the cost-to-failure ratio (C_i / F_i) for a given clause node.
      Parameters:
      ruleName - rule name
      node - clause node
      Returns:
      ratio value (lower values indicate faster, higher-failure clauses that should evaluate first)
    • isReorderBeneficial

      public boolean isReorderBeneficial(ExpressionNode originalAst, ExpressionNode optimizedAst)
      Determines whether reordering produced a different, structurally beneficial AST tree.
      Parameters:
      originalAst - original AST
      optimizedAst - optimized AST
      Returns:
      true if the AST order was changed
    • optimizeAndHotSwap

      public OptimizationResult optimizeAndHotSwap(Rule rule, ExpressionNode astRoot, TieredRuleCache cache) throws Exception
      Dynamically optimizes an uncompiled AST, recompiles the optimized bytecode, and atomically hot-swaps it into the tiered cache with zero downtime.
      Parameters:
      rule - rule definition metadata
      astRoot - original parsed AST root
      cache - active tiered rule cache
      Returns:
      optimization result metadata
      Throws:
      Exception - if bytecode compilation or hot-swapping fails
    • optimizeAndHotSwap

      public OptimizationResult optimizeAndHotSwap(Rule rule, ExpressionNode astRoot, TieredRuleCache cache, AsmBytecodeGenerator generator, boolean force) throws Exception
      Dynamically optimizes an uncompiled AST, recompiles the optimized bytecode, and atomically hot-swaps it into the tiered cache with zero downtime.
      Parameters:
      rule - rule definition metadata
      astRoot - original parsed AST root
      cache - active tiered rule cache
      generator - bytecode generator to use for compilation
      force - whether to recompile and hot-swap even if clause order did not change
      Returns:
      optimization result metadata
      Throws:
      Exception - if bytecode compilation or hot-swapping fails