Razor テンプレート言語の (非常に小さな) サブセットを解析する正規表現があります。最近、正規表現にさらにいくつかのルールを追加したため、実行が大幅に遅くなりました。私は疑問に思っています:遅いことが知られている特定の正規表現構造はありますか? 読みやすさを維持しながらパフォーマンスを向上させる、使用しているパターンの再構築はありますか? 注: このパフォーマンス ヒットはコンパイル後に発生することを確認しました。
パターンは次のとおりです。
new Regex(
@" (?<escape> \@\@ )"
+ @"| (?<comment> \@\* ( ([^\*]\@) | (\*[^\@]) | . )* \*\@ )"
+ @"| (?<using> \@using \s+ (?<namespace> [\w\.]+ ) (\s*;)? )"
// captures expressions of the form "foreach (var [var] in [expression]) { <text>"
/* ---> */ + @"| (?<foreach> \@foreach \s* \( \s* var \s+ (?<var> \w+ ) \s+ in \s+ (?<expressionValue> [\w\.]+ ) \s* \) \s* \{ \s* <text> )"
// captures expressions of the form "if ([expression]) { <text>"
/* ---> */ + @"| (?<if> \@if \s* \( \s* (?<expressionValue> [\w\.]+ ) \s* \) \s* \{ \s* <text> )"
// captures the close of a razor text block
+ @"| (?<endBlock> </text> \s* \} )"
// an expression of the form @([(int)] a.b.c)
+ @"| (?<parenAtExpression> \@\( \s* (?<castToInt> \(int\)\s* )? (?<expressionValue> [\w\.]+ ) \s* \) )"
+ @"| (?<atExpression> \@ (?<expressionValue> [\w\.]+ ) )"
/* ---> */ + @"| (?<literal> ([^\@<]+|[^\@]) )",
RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline | RegexOptions.ExplicitCapture | RegexOptions.Compiled);
/* ---> */ は、速度低下の原因となった新しい「ルール」を示します。