首页 > 解决方案 > NRules rulesRepo.Compile() 慢

问题描述

几周前,我开始与 NRules 合作。我很喜欢。但是我遇到的问题是性能......我有大约 1500 条规则(可能是很多?)动态构建,我的 rulesRepo.Compile() 需要将近 6 秒。有人有同样的问题吗?

标签: nrules

解决方案


目的是让规则在应用程序生命周期内只编译一次(通常在应用程序启动时)。所以,性能成本是你应该只支付一次的。

另外,考虑一个自定义表达式编译器(https://github.com/NRules/NRules/wiki/Expression-Compiler),您可以在其中尝试连接https://github.com/dadhi/FastExpressionCompiler以加快编译速度

using FastExpressionCompiler;

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

并使用创建的表达式编译器进行规则编译:

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

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

推荐阅读