Rule Evaluation Performance Tips

The Code Effects rules engine is extremely fast. In a single thread, the generic Evaluator<T> evaluates one million randomly filled objects against an evaluation type rule in about a tenth of a second, and its Filter method does it in 30 to 70 milliseconds. These figures come from a rule with two conditions, run in a plain test application on a modest virtual machine. Yours depend on your rules and your hardware, but the proportions between the tips below hold.

Even though performance is excellent, it still makes sense to optimize your evaluations where possible.

Evaluator Instances

The most expensive operation is compiling your rule(s) during the initialization of the Evaluator class. In our test, creating an Evaluator for a rule of two conditions took about 0.2 milliseconds once the process was warm, which is as much as thousands of evaluations of the same rule. The very first Evaluator in a process takes longer because of the .NET warm-up.

If at all possible, reuse Evaluator instances to run multiple data sets against the same rules. Each instance already holds the compiled rules, so caching and reusing one avoids unnecessary overhead. An Evaluator can be shared between threads: we ran 200,000 evaluations of one instance on parallel threads and every result was correct. Keep in mind that rules with setters or actions change the source object they are given, so each thread needs an object of its own.

Generic Evaluator

Prefer the generic Evaluator<T> over the Evaluator that takes a Type. The generic class works with a delegate typed for your source object, while the non-generic one invokes its delegate dynamically on every call. In our test, a million evaluations took about 90 milliseconds with Evaluator<T> and about 800 milliseconds with Evaluator.

Rulesets

If you need to evaluate multiple rules against the same data, you can combine them into a single ruleset by inserting them into an empty XML document and passing that ruleset as your RuleXML to the Evaluator constructor, so the Evaluator compiles them all once:

<?xml version="1.0" encoding="utf-8"?>
<codeeffects xmlns="https://codeeffects.com/schemas/rule/7">

    <rule id="ID1" ...>
        <!-- Rule #1 -->
    </rule>

    <rule id="ID2" ...>
        <!-- Rule #2 -->
    </rule>

    <rule id="ID3" ...>
        <!-- Rule #3 -->
    </rule>

    ...

</codeeffects>

Then choose the rule to evaluate by its ID or its index, or evaluate the whole ruleset by scope. The Evaluate method without any of them evaluates the first rule of the ruleset only:

var evaluator = new Evaluator<YourSourceType>(rulesetXml);

bool first = evaluator.Evaluate(source);          // The first rule only
bool second = evaluator.Evaluate(source, "ID2");  // The rule with this ID
bool third = evaluator.Evaluate(source, 2);       // The rule at this index

See Business Rule Evaluation for the evaluation scope and short-circuiting of a ruleset.

Collections of Source Objects

To select the objects of a collection that satisfy a rule, give the whole collection to the Filter method of the Evaluator instead of calling Evaluate for every object. It is faster: the evaluator applies its compiled rule to each item without a separate call per object. See Data Filtering.

Tracer

A tracer is called for every expression of the rule, and it makes evaluation several times slower even when the tracer does nothing. In our test, 100,000 evaluations took about three times longer with an empty tracer. Set it only when you debug or log, and not in the code path you want to be fast.

Null Checks

By default, the engine builds checks into the compiled code that stop a rule from throwing a NullReferenceException when it reads a member of an object that is null. If your source objects never have such members, you can set the PerformNullChecks property of EvaluationParameters to false and compile a leaner rule. Do it only when you are sure, because the rule throws as soon as it meets a null.

Ask on Stack Overflow - Yes, it's still active
Ask in GitHub Discussions

p102

l097 --

l102

p101

×