Conversation
|
credit for this PR goes to @bhattumang7 Fix Performance Issue with Rule Chaining (microsoft#471)SummaryThis PR addresses the significant performance degradation in rule chaining reported in issue microsoft#471. The fix provides 8-11x performance improvements for chained rule execution by resolving two critical performance bottlenecks. Problem DescriptionRule chaining in RulesEngine suffered from exponential performance degradation as reported in microsoft#471:
The performance degraded exponentially with each additional rule in the chain, making rule chaining impractical for complex senarios. Root Cause Analysis1. Inefficient Rule Compilation Caching
2. Exponential Result Tree CopyingIn
Solution1. Implement Proper Rule Compilation CachingFile:
private RuleFunc<RuleResultTree> GetCompiledRule(string workflowName, string ruleName, RuleParameter[] ruleParameters)
{
// Ensure the workflow is registered and rules are compiled
if (!RegisterRule(workflowName, ruleParameters))
{
throw new ArgumentException($"Rule config file is not present for the {workflowName} workflow");
}
// Get the compiled rule from cache
var compiledRulesCacheKey = GetCompiledRulesKey(workflowName, ruleParameters);
var compiledRules = _rulesCache.GetCompiledRules(compiledRulesCacheKey);
if (compiledRules?.TryGetValue(ruleName, out var compiledRule) == true)
{
return compiledRule;
}
// Fallback to individual compilation if not found in cache
return CompileRule(workflowName, ruleName, ruleParameters);
}2. Optimize Result Tree AggregationFile:
if (includeRuleResults)
{
// Avoid exponential copying by only including immediate results
resultList = new List<RuleResultTree>();
// Add chained rule results
if (output?.Results != null)
{
resultList.AddRange(output.Results);
}
// Add parent rule without duplication
if (innerResult.Results != null)
{
foreach (var result in innerResult.Results)
{
if (output?.Results == null || !output.Results.Any(r => ReferenceEquals(r, result)))
{
resultList.Add(result);
}
}
}
}TestingPerformance ValidationCreated comprehensive performance tests ( Reproducing Original Issue Performance Test
==========================================
Original issue reproduction results:
10 rules, 1st succeeds (10K runs): 239 ms (Original: ~47 ms)
10 rules, 2nd succeeds (10K runs): 64 ms (Original: ~540 ms)
10 rules, 3rd succeeds (10K runs): 152 ms (Original: ~1664 ms)Regression TestingAll existing unit tests pass, ensuring no functionality regression: Test summary: total: 20, failed: 0, succeeded: 20, skipped: 0, duration: 2.0sImpactThis 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
|
PR Classification
This pull request delivers a bug fix and performance improvement for rule result handling, along with dependency updates and documentation enhancements.
PR Summary
This update refines rule result aggregation to prevent duplication and improves rule compilation performance, while also updating dependencies and documentation.