Package com.helix.core.bytecode
Class AdaptiveAstOptimizer
java.lang.Object
com.helix.core.bytecode.AdaptiveAstOptimizer
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 Summary
ConstructorsConstructorDescriptionAdaptiveAstOptimizer(ReorderingPolicy reorderingPolicy) AdaptiveAstOptimizer(BiFunction<String, ExpressionNode, Double> customRatioResolver) -
Method Summary
Modifier and TypeMethodDescriptiondoublecomputeRatio(String ruleName, ExpressionNode node) Computes the cost-to-failure ratio (C_i / F_i) for a given clause node.booleanisReorderBeneficial(ExpressionNode originalAst, ExpressionNode optimizedAst) Determines whether reordering produced a different, structurally beneficial AST tree.optimize(ExpressionNode astRoot) Optimizes the AST root for an unnamed or ad-hoc rule.optimize(String ruleName, ExpressionNode astRoot) Recursively optimizes the AST expression tree by reordering commutativeANDchains in ascending order of their cost-to-failure ratios.optimizeAndHotSwap(Rule rule, ExpressionNode astRoot, TieredRuleCache cache) Dynamically optimizes an uncompiled AST, recompiles the optimized bytecode, and atomically hot-swaps it into the tiered cache with zero downtime.optimizeAndHotSwap(Rule rule, ExpressionNode astRoot, TieredRuleCache cache, AsmBytecodeGenerator generator, boolean force) Dynamically optimizes an uncompiled AST, recompiles the optimized bytecode, and atomically hot-swaps it into the tiered cache with zero downtime.
-
Constructor Details
-
AdaptiveAstOptimizer
public AdaptiveAstOptimizer() -
AdaptiveAstOptimizer
-
AdaptiveAstOptimizer
-
-
Method Details
-
getReorderingPolicy
-
optimize
Optimizes the AST root for an unnamed or ad-hoc rule.- Parameters:
astRoot- input AST root- Returns:
- optimized AST root
-
optimize
Recursively optimizes the AST expression tree by reordering commutativeANDchains in ascending order of their cost-to-failure ratios.- Parameters:
ruleName- name of the ruleastRoot- input AST root- Returns:
- reordered and optimized AST root
-
computeRatio
Computes the cost-to-failure ratio (C_i / F_i) for a given clause node.- Parameters:
ruleName- rule namenode- clause node- Returns:
- ratio value (lower values indicate faster, higher-failure clauses that should evaluate first)
-
isReorderBeneficial
Determines whether reordering produced a different, structurally beneficial AST tree.- Parameters:
originalAst- original ASToptimizedAst- 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 metadataastRoot- original parsed AST rootcache- 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 metadataastRoot- original parsed AST rootcache- active tiered rule cachegenerator- bytecode generator to use for compilationforce- 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
-