Regex internal caching or what?

H

Henrik Dahl

Hello!

In my application I have a need for using a regular expression now and then.
Often the same regular expression must be used multiple times. For
performance reasons I use the RegexOptions.Compiled when I instantiate it.
It must be obvious that it takes some time to instantiate such an object.

My question is, does the Regex instantiation somehow deal with some caching
internally so instantiating a Regex object multiple times having the same
expression as the first argument for the constructor is quite efficient or
do I need to create my own caching functionality?


Best regards,

Henrik Dahl
 
K

Kevin Spencer

You can compile Regular Expression sinto an assembly if you like.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
 
S

Samuel R. Neff

Interestingly, Regex has the funcionality to do internal caching, but
it apparently is not exposed to the caller.

When you use one of the static methods like Regex.Match and
Regex.Split it will use a private constructor Regex(pattern, options,
cache) and specify true for cache. However, the public constructors
all force false for cache.

So you should have your own caching mechanism if testing demonstrates
that caching would be beneficial in your application.

HTH,

Sam
 
J

Jeffrey Tan[MSFT]

Hi Henrik,

The caching behavior of RegEx got changed from .NET 1.1 to .NET 2.0. There
are basically 2 changes:
#1, The 2.0 Regex class no longer has an unbounded cache size.
#2, The 2.0 Regex class no longer caches parsed regular expressions created
by Regex instance methods, it only caches regular expressions created by
Regex static methods, such as Regex.Match, Regex.Split.

Please refer to the blog entry below for more details:
"Regex Class Caching Changes between .NET Framework 1.1 and .NET Framework
2.0 [Josh Free]"
http://blogs.msdn.com/bclteam/archive/2006/10/19/regex-class-caching-changes
-between-net-framework-1-1-and-net-framework-2-0-josh-free.aspx

Regarding the semantics and performance issue for RegexOptions.Compiled,
please refer to the articles below:
"Regular Expression performance [David Gutierrez]"
http://blogs.msdn.com/bclteam/archive/2004/11/12/256783.aspx
"Regular Expression Compilation" section of link below:
http://msdn.microsoft.com/msdnmag/issues/06/01/CLRInsideOut/#S7

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
H

Henrik Dahl

Jeffrey Tan,

Please allow me to express my recognition of your very precise and
constructive answer.


Best regards,

Henrik Dahl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top