<- Back to Documentation Index
Performance
Benchmarks
Typical execution for 999 customers on .NET 8:
| Phase | Time | Per Customer | Notes |
|---|---|---|---|
| Validation | ~46ms | — | One-time, before compile |
| Compilation | ~812ms | — | One-time, cached per signature |
| Sequential Execute | ~2ms | 0.002ms | Best for few simple rules |
| Parallel Execute | ~3ms | 0.003ms | Best for many CPU-intensive rules |
| Parallel Async | ~5ms | 0.005ms | Best for async I/O rules |
Why It’s Fast
1. Roslyn Compilation
Expressions compile to IL once. No interpreter overhead.
2. Typed Delegates
// RulesEngine (slow)
var result = delegate.DynamicInvoke(args); // Boxing + reflection
// This engine (fast)
var result = _delegate(customer); // Direct call, zero overhead
3. Immutable Rules
Properties locked after Compile(). Zero locks, zero contention.
4. Parallel.For
Independent rules run on thread pool. No manual Task creation.
Tuning Tips
| Scenario | Recommendation |
|---|---|
| Few simple rules | Use Execute() — parallel overhead not worth it |
| Many complex rules | Use ExecuteParallel() |
| Rules hit database/HTTP | Use ExecuteParallelAsync() |
| One-time setup | Call Validate() + Compile() at startup |
| Hot path | Reuse compiled workflow, never recompile |
| Rapidly changing rules | Lower maxCompilesBeforeRecycle |
| Stable production rules | Raise maxCompilesBeforeRecycle |
Memory
- Compiled delegates: ~2KB per expression
- Rule objects: ~200 bytes each
- RuleResult: ~32 bytes per execution
Typical 100-rule workflow: ~50KB compiled, executes in microseconds.
One Parameter per Rule
A rule takes exactly one parameter, and it is invoked through a typed delegate directly — no DynamicInvoke, no object-array allocation per call. When a rule needs several values, wrap them in a struct or class and reference its members in the expression:
// One parameter: the wrapper holds every input
public record CustomerInput(int Age, bool IsActive, string Name);
var rule = new Rule { Expression = "input.Age >= 18 && input.IsActive" };
See Also
- Performance Tuning — Detailed tuning guide
- Benchmarks — BenchmarkDotNet project