Performance
Evaluation is fast once rules are compiled. Most performance questions are about compiling (warm-up) and about not forcing recompiles. The measurements quoted here come from the change log; run the project in benchmark/ on your own rules to measure your workload.
Reuse the engine
Every cache lives inside one RulesEngine instance (see Caching and invalidation). Create the engine once, register the workflows once and reuse it for all calls. A new instance starts with empty caches and recompiles everything.
Keep input shapes consistent
Compiled rules are cached per workflow and per input names and types. Passing the same shape every time keeps every call on the fast path. These cause extra compiles:
- the same input name with different types (for example
intin one call andlongin another); - dynamic inputs (
ExpandoObject, dictionaries) whose properties differ between calls, including a property that isnullin one call and has a value in another; - adding or removing inputs.
Prefer typed classes for inputs when you can, and see How inputs are typed.
Reduce warm-up time
| Technique | When it helps |
|---|---|
EnableParallelRuleCompilation = true |
Workflows with many enabled rules (the engine uses it from 32 rules). Leave UseFastExpressionCompiler off, because the engine does not combine the two. |
EnableAssemblyScanning = false |
You do not use [DynamicLinqType]. Skips scanning every loaded assembly. |
Register types explicitly in CustomTypes |
Avoids relying on discovery. |
| Warm up at start-up | Execute each workflow once with representative inputs before serving traffic. |
Release 6.0.1 restored the compiled-expression cache and removed a per-expression assembly scan. The change log reports a 20,000-rule workflow warming up in 16.4 seconds instead of 113.8, and about 6.6 times faster registration for realistic large workflows.
Reduce work per call
- Use scoped parameters for shared work. A
GlobalParamis evaluated once per call, not once per rule, so move expensive sub-expressions into globals. - Use
Performancemode for large OR groups. Wrap rules in one parent withOperatorOrElseand setNestedRuleExecutionMode = Performance; evaluation stops at the first passing child. - Disable features you do not use. The bundled benchmark sets
EnableFormattedErrorMessage = falseandEnableScopedParams = false. Formatting anErrorMessagethat contains$(...)placeholders serializes the referenced inputs to JSON, so turn it off if you never read the formatted messages. - Disable rules you do not need with
"Enabled": falseinstead of deleting and re-adding them; disabled rules are not compiled or evaluated.
Cache size
MemCacheConfig.SizeLimit (default 1000) limits the number of entries in each cache. A workflow evaluated with many different input shapes, or a very large set of distinct expressions, can exceed it and cause recompiles. Raise it when you have more distinct compiled rules than that:
var settings = new ReSettings { CacheConfig = new MemCacheConfig { SizeLimit = 10000 } };
Setting SizeLimit to 0 or less disables the compiled-expression cache. Only do that to measure uncached behavior.
FastExpressionCompiler
UseFastExpressionCompiler = true compiles expressions with FastExpressionCompiler. It is off by default. Measure your own rules before enabling it, and note that it is not combined with parallel compilation.