我一直在使用 C# 正则表达式,它被大量用作 Web 应用程序中自定义模板系统的一部分。表达式很复杂,我注意到使用 Regex.Compiled 选项带来的实际性能提升。然而,编译的初始成本在开发过程中是令人讨厌的,尤其是在迭代单元测试期间(这里提到了这个一般的权衡)。
我目前正在尝试的一种解决方案是惰性正则表达式编译。这个想法是,我可以通过在单独的线程中创建正则表达式的编译版本并在准备好时将其替换来获得两全其美的效果。
我的问题是:是否有任何理由说明这可能是一个坏主意?我之所以问,是因为我不确定跨线程分配诸如 jitting 和程序集加载之类的成本是否真的有效(尽管从我的基准测试中看来)。这是代码:
public class LazyCompiledRegex
{
private volatile Regex _regex;
public LazyCompiledRegex(string pattern, RegexOptions options)
{
if (options.HasFlag(RegexOptions.Compiled)) { throw new ArgumentException("Compiled should not be specified!"); }
this._regex = new Regex(pattern, options);
ThreadPool.QueueUserWorkItem(_ =>
{
var compiled = new Regex(pattern, options | RegexOptions.Compiled);
// obviously, the count will never be null. However the point here is just to force an evaluation
// of the compiled regex so that the cost of loading and jitting the assembly is incurred here rather
// than on the thread doing real work
if (Equals(null, compiled.Matches("some random string").Count)) { throw new Exception("Should never get here"); }
Interlocked.Exchange(ref this._regex, compiled);
});
}
public Regex Value { get { return this._regex; } }
}