Rule chaining in RulesEngine suffered from exponential performance degradation as reported in #471#706
Rule chaining in RulesEngine suffered from exponential performance degradation as reported in #471#706bhattumang7 wants to merge 2 commits into
Conversation
Refactor resultList population to avoid duplication and improve performance.
|
you should be making these changes in my branch which is the only maintained fork |
Rule chaining in RulesEngine suffered from exponential performance degradation as reported in microsoft#471 microsoft#706
|
@bhattumang7 bhattumang7 Overlap with merged #734. The first root cause you identified ("each chained rule required full recompilation instead of leveraging the existing cache") is now fixed on The unique part of your PR is still valuable. Your second root cause — exponential O(n²) result-list copying in // EvaluateRuleAction.cs — current code rebuilds the full result list every link in the chain:
resultList = new List<RuleResultTree>(output?.Results ?? new List<RuleResultTree>());
resultList.AddRange(innerResult.Results);
Could you rebase against current main and reduce the PR to just the EvaluateRuleAction result-aggregation change? That would be a focused win that complements what already landed. Happy to re-benchmark once you do.
Also: please add an xUnit test that builds a 5+ deep chain and asserts the result list count is linear in chain depth (not quadratic). The benchmark in your PR description is great context but doesn't ride the CI. |
|
Latest 6.0.1-preview.1 is addressing all these issues. |
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