Rule chaining in RulesEngine suffered from exponential performance degradation as reported in #471 #706
      
        
          +46
        
        
          −4
        
        
          
        
      
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Fix Performance Issue with Rule Chaining (#471)
Summary
This PR addresses the significant performance degradation in rule chaining reported in issue #471. The fix provides 8-11x performance improvements for chained rule execution by resolving two critical performance bottlenecks.
Problem Description
Rule chaining in RulesEngine suffered from exponential performance degradation as reported in #471:
The performance degraded exponentially with each additional rule in the chain, making rule chaining impractical for complex scenarios.
Root Cause Analysis
1. Inefficient Rule Compilation Caching
ExecuteActionWorkflowAsyncwas calling the individualCompileRulemethod which bypassed the compiled rules cache used byExecuteAllRulesAsync. This meant:2. Exponential Result Tree Copying
In
EvaluateRuleAction.ExecuteAndReturnResultAsync, each chained rule was copying ALL previous results:Solution
1. Implement Proper Rule Compilation Caching
File:
src/RulesEngine/RulesEngine.csExecuteActionWorkflowAsyncto use newGetCompiledRulemethodGetCompiledRuleleverages the same caching mechanism asExecuteAllRulesAsync2. Optimize Result Tree Aggregation
File:
src/RulesEngine/Actions/EvaluateRuleAction.csExecuteAndReturnResultAsyncto avoid exponential copyingTesting
Performance Validation
Created comprehensive performance tests (
PerformanceTest/Program.cs) that reproduce the original issue scenarios:Regression Testing
All existing unit tests pass, ensuring no functionality regression:
Impact
This fix transforms rule chaining from an impractical feature with exponential performance degradation into a viable solution for complex rule scenarios. Users can now:
Related Issues
Type of Change