Restore compiled-expression cache in RuleExpressionParser (fixes #673)#727
Merged
YogeshPraj merged 1 commit intoMay 27, 2026
Merged
Conversation
…osoft#673) The static MemCache around Compile<T> was removed in v5.0.1 (microsoft#501) along with the AutoRegisterInputType feature. Removing it caused every call to Compile<T> / CompileRuleExpressionParameters to re-run the dynamic expression parser and re-emit IL, causing ~10x slowdowns reported in microsoft#673 and 100x-1900x slowdowns on the public parser and ExecuteActionWorkflowAsync paths that are not served by the workflow-level RulesCache. Reintroduce the cache as an instance field (not static, to avoid the latent cross-settings sharing bug the old static had). The key includes the expression text, ordered (paramName, paramType), return type, and a settings fingerprint covering IsExpressionCaseSensitive, UseFastExpressionCompiler, and CustomTypes. Scoped-params keys also include each value-expression's text so workflow mutations correctly invalidate. Honors the existing ReSettings.CacheConfig; SizeLimit == 0 disables. Benchmark (median of 5 runs, .NET 8, 30-rule workflow): Parser.Evaluate<bool> x 30k: 34,730 ms -> 33 ms (~1052x) Parser.Compile<bool> x 30k: 34,846 ms -> 18 ms (~1936x) ExecuteActionWorkflowAsync x 1000: 1,182 ms -> 3 ms ( ~394x) ExecuteAllRulesAsync x 1000 (warm): 11 ms -> 17 ms (within noise) Fresh engine x 50: 1,890 ms -> 1,867 ms (unchanged)
This was referenced May 27, 2026
Closed
sagarambilpure
approved these changes
May 27, 2026
This was referenced May 27, 2026
Merged
YogeshPraj
added a commit
that referenced
this pull request
May 27, 2026
Bump <Version> from 6.0.0 to 6.0.1-preview.1 and add a CHANGELOG entry covering the three PRs landed since 6.0.0: #727 (perf cache restore), #728 (action-exception propagation, list schema union, OutputExpression hint, global-param dedup, object-return diagnostics), and #729 (ExecuteActionWorkflowAsync FormatErrorMessages, ActionContext null guard, deep ErrorMessage interpolation, plus regression guards for #581, #590, #606, #608). Co-authored-by: Yogesh Prajapati <yogeshcprajapati@outlook.com>
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes #673. The static
MemCachearoundRuleExpressionParser.Compile<T>was removed in v5.0.1 (#501) as a secondary change inside a PR titled "Added option to disable auto type registry." Removing it meant every call toCompile<T>/CompileRuleExpressionParametersre-ran the dynamic expression parser and re-emitted IL viaCompileFast/Compile, causing the ~10x slowdown the issue reports.This PR restores the cache, but as an instance field (the original was
static, which silently shared compiled delegates acrossRulesEngineinstances with differentCustomTypes/IsExpressionCaseSensitive— a latent correctness bug we don't want to bring back).Why the cache was originally removed (best reconstruction)
PR #501's commit message ("removed caching from RuleExpressionParser"), the PR description, the CHANGELOG entry, and the linked issue (#500) all give no justification for the removal. #500 is a parsing bug ("array like an enum") whose actual fix is the
AutoRegisterInputTypeflag — completely unrelated to caching.The most plausible unstated rationale: the same PR introduced two new behavioral toggles (
AutoRegisterInputTypeandIsExpressionCaseSensitive). The originalstaticcache would have silently shared compiled delegates acrossRuleExpressionParserinstances with different settings, defeating those new toggles. Removing the cache "fixed" that latent bug by sledgehammer.This PR addresses that concern directly rather than by removal:
RuleExpressionParserinstances cannot share entries.IsExpressionCaseSensitive,UseFastExpressionCompiler, andCustomTypes. Even within one instance, settings-affecting inputs cannot collide.Compile_DifferentSettings_DoNotShareCachetest explicitly guards this property and would fail if the bug ever crept back.AutoRegisterInputTypeparsing fix from Added option to disable auto type registry #501 is left untouched, so v4 to v5 error: array like an enum #500 stays fixed.What changed
RuleExpressionParsernow owns an instanceMemCache.Compile<T>andCompileRuleExpressionParameterslook up against it before re-parsing.(paramName, paramType.AssemblyQualifiedName)IsExpressionCaseSensitive,UseFastExpressionCompiler,CustomTypesValueExpression.ToString()(so mutating a workflow'sLocalParams/GlobalParamscorrectly invalidates)ReSettings.CacheConfig.SizeLimit == 0opts out entirely.Why
RulesCachedoesn't already mask thisRulesCachekeys onworkflowName + paramTypesand caches compiledRuleFunc<RuleResultTree>. It works for repeatExecuteAllRulesAsynccalls on a warm engine. It does not cover:RuleExpressionParser.Evaluate<T>/Compile<T>(public API).ExecuteActionWorkflowAsync, which callsCompileRuleon every invocation (RulesEngine.cs#L131).Benchmark
.NET 8.0.27, Release build. Each scenario: 3 warmup + 5 measured runs; reported value is the median in ms.
RuleExpressionParser.Evaluate<bool>x 30,000 (same expression + params)RuleExpressionParser.Compile<bool>x 30,000 (same expression + params)RulesEngine.ExecuteActionWorkflowAsyncx 1,000 (30-rule workflow)RulesEngine.ExecuteAllRulesAsyncx 1,000, warm engine (30-rule workflow)RulesEngine+ExecuteAllRulesAsyncx 50, fresh engine each call (30 rules)Notes:
RulesCache. Within noise, negligible vs compile cost.RulesEngine; restoring a process-wide static cache is what caused the original latent bug.Test plan
RuleExpressionParserTests:Compile_SameExpressionAndParams_ReturnsCachedDelegate— verifies the cache returns the same delegate instance.Compile_DifferentSettings_DoNotShareCache— guards against the original static-field cross-settings bug.Compile_CacheDisabled_RecompilesEveryCall— verifiesCacheConfig.SizeLimit == 0opts out.RemoveWorkFlow_ShouldRemoveAllCompiledCache,ClearWorkFlow_ShouldRemoveAllCompiledCache,WorkflowUpdate_GlobalParam_ShouldReflect) still pass — confirms the scoped-params cache key correctly includes the value-expression text.