Caching and invalidation
Compiling an expression is the expensive part of evaluating a rule, so RulesEngine caches at several levels. Knowing what is cached and when it is refreshed explains most performance and "why did my change not apply" questions.
flowchart TB
exec["ExecuteAllRulesAsync"] --> L2
subgraph engine["Per RulesEngine instance"]
L1[("1. Workflow store<br/>key: WorkflowName")]
L2[("2. Compiled rules<br/>key: workflow + input names and types")]
L3[("3. Compiled global params<br/>same key as compiled rules")]
L4[("4. Expression delegate cache<br/>key: settings + parameters + expression")]
L5["5. ParsingConfig and type provider<br/>reused until CustomTypes changes"]
end
L2 -- "stale or missing" --> L1
L2 -- "needs compile" --> L4
L4 -- "miss" --> L5
L5 --> dlc["System.Linq.Dynamic.Core<br/>parse and compile"]
AddOrUpdate["AddOrUpdateWorkflow"] -. "new timestamp makes layer 2 stale" .-> L1
Remove["RemoveWorkflow / ClearWorkflows"] -. "evicts" .-> L2
Remove -. "evicts" .-> L3
Figure 1. Cache layers inside one engine instance. Nothing is shared between engine instances.
| # | What | Key | Refreshed or evicted by |
|---|---|---|---|
| 1 | The Workflow objects you registered, with a timestamp |
WorkflowName |
AddOrUpdateWorkflow replaces it; RemoveWorkflow and ClearWorkflows delete it. |
| 2 | Compiled rules (one delegate per rule) | Workflow name plus every input's name and full type name | Stale when older than the workflow's timestamp, so the next call recompiles. Removed with the workflow. |
| 3 | The compiled delegate that evaluates global parameters | Same as layer 2 | Replaced or removed together with layer 2, so a workflow update that removes or breaks its global parameters never keeps running the old ones. |
| 4 | Compiled expression delegates | Settings fingerprint, return type, parameter names and types, expression text | First in, first out when the size limit is reached. Disabled when CacheConfig.SizeLimit is 0 or less. |
| 5 | The ParsingConfig and CustomTypeProvider used by the parser |
Reference of ReSettings.CustomTypes |
Rebuilt only when the CustomTypes array is replaced. |
Things worth knowing
- Changing a workflow. Use
AddOrUpdateWorkflow(or remove and add again). The next execution recompiles because the stored workflow is newer than its compiled rules. - A new input shape recompiles. The same workflow executed with a different input type, a different input name or an additional input has a different cache key and compiles again. Passing consistently shaped inputs keeps the hot path fast. See How inputs are typed.
- Removing a workflow is exact. Compiled entries are matched on the workflow name they were compiled from, so removing
Anever evictsA-B. - Size limit.
MemCacheConfig.SizeLimit(default 1000) is a count of entries per cache, not a size in bytes. Entries never expire by time. - Injected workflows.
WorkflowsToInjectis resolved when a workflow is compiled, into a new combined workflow. The stored workflow is never modified, and circular injections raiseInvalidOperationException. - Auto-registered input types. With
AutoRegisterInputTypethe engine adds input types toCustomTypesonly when it discovers new ones, so the parser's configuration is not rebuilt (and assemblies are not rescanned) on every registration. - Warm-up cost. The first execution of a large workflow pays the compile cost.
EnableParallelRuleCompilationcan shorten it for workflows with at least 32 enabled rules, and is ignored whenUseFastExpressionCompileris on.