NRules rulesRepo.Compile() slow

234 views Asked by At

I've started to work with NRules a couple weeks ago. I like it a lot. But the issue I am getting is performance... I have about 1500 rules (may be it's to many?) built dynamically, and my rulesRepo.Compile() takes almost 6 seconds. Is anybody had the same kind of issue?

1

There are 1 answers

0
Sergiy Nikolayev On

The intent is for rules to get compiled only once during the application lifetime (generally at application startup). So, that performance cost is something you are supposed to only pay once.

Also, consider a custom expression compiler (https://github.com/NRules/NRules/wiki/Expression-Compiler) where you can try to hook up https://github.com/dadhi/FastExpressionCompiler to speed up compilation

using FastExpressionCompiler;

public class FastExpressionCompiler : NRules.Extensibility.IExpressionCompiler
{
    public TDelegate Compile<TDelegate>(Expression<TDelegate> expression) where TDelegate : Delegate
    {
        return expression.CompileFast();
    }
}

And use the created expression compiler for rules compilation:

var repository = new RuleRepository();
//Load rules

var compiler = new RuleCompiler();
compiler.ExpressionCompiler = new FastExpressionCompiler();
var factory = compiler.Compile(repository.GetRuleSets());