-
-
Notifications
You must be signed in to change notification settings - Fork 373
Compile Regex Optimization
ID: compile regex
Availability:
This optimization compiles the regular expressions that are used in an assembly
into static code. The code is generated using the facilities of the .NET
Framework that allow the Regex.CompileToAssembly
method to work.
The compiled expressions perform with the same performance gain as expressions
that are maked with RegexOptions.Compiled
, but without the slowdown when loading the expression.
-
compiled
: This parameter is a boolean value indicating if all regular expressions should be compiled, or only the ones with theRegexOptions.Compiled
flag.-
true
: Only regular expressions with theRegexOptions.Compiled
flag will be compiled to static code. -
false
: All regular expressions are subject of being compiled. (default)
-
-
i18nSafe
: This parameter is a boolean value indicating that only regular expressions are compiled that are compiled exactly the same way for every culture. The expression won't be compiled if it's flagged withRegexOptions.CaseInsensitive
or if it contains case-insenstive areas, unless it is also flagged withRegexOptions.CultureInvariant
.-
true
: Skip expressions that are unsafe to compile with regards to the culture. -
false
: Ignore possible issues with the culture and compile the expressions for the invariant culture. (default)
-
-
skipBroken
: This parameter is a boolean value indicating how to handle invalid regular expressions.-
true
: Warn about invalid regular expressions and leave them as is. -
false
: Cancel the obfuscation in case an invalid regular expression is encountered. (default)
-
<protection id="compile regex">
<argument name="compiled" value="false" />
<argument name="i18nSafe" value="true" />
<argument name="skipBroken" value="false" />
</protection>
This optimization needs to identify both the pattern and the options for the regular expression. This only works in case both are constant values. If either the pattern or the options are not constant, the call to the expression will not be optimized.
Enabling this optimization is in general a safe thing to do. It increases the size of the assembly, depending on the amount and the complexity of the expressions. The performance gain depends on the complexity of the expressions. In case the same expression is used multiple times in the assembly, the compiled code is reused.
If the increase in side is a concern it's useful to limit the compiling of the
regular expressions to the ones that are most critical for the performance.
This can be done either by setting the RegexOptions.Compiled
and setting
the compiled
parameter to true
or be enabling this optimization only
for specific methods and types.