NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Compilation and Reuse

By default, Regex compiles a regular expression to a sequence of internal regular expression byte codes (these are high-level codes that are different from intermediate language (IL)). When a regular expression is executed, the byte codes are interpreted.

If a Regex object is constructed with the c option, it compiles the regular expression to explicit IL code instead of high-level Regex byte codes. This allows the NGWS runtime Just-in-Time (JIT) engine to convert the expression to native machine code for higher performance.

However, generated IL cannot be unloaded. The only way to unload code is to unload an entire application domain (that is, unload all of your application’s code.). Effectively, once a regular expression is compiled with the c option, the resources used by the compiled expression are never released, even if the Regex object itself is released and garbage-collected.

Because of this limitation, you must be careful to limit the number of different regular expressions you compile with the c option. If an application must use a large or unbounded number of regular expressions, each expression should be interpreted, not compiled. On the other hand, if a small number of regular expressions are used repeatedly, they should be compiled with c for higher performance.

Regex maintains an internal hashtable of compiled regular expressions. Once a regular expression is compiled with c, the code will be reused whenever the same regular expression is subsequently requested.