Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to check the number of cached regex?

Tags:

c#

.net

regex

Regex.CacheSize Property Gets or sets the maximum number of entries in the current static cache of compiled regular expressions.

The Regex class maintains an internal cache of compiled regular expressions used in >static method calls. If the value specified in a set operation is less than the current >cache size, cache entries are discarded until the cache size is equal to the specified >value.

By default, the cache holds 15 compiled static regular expressions. Your application >typically will not have to modify the size of the cache. Use the CacheSize property only >when you want to turn off caching or when you have an unusually large cache.

So I'd like to have insight into the current number of expressions in the cache. Anyone know if/how that is possible?

Idea being that I reuse < 15 of them now so don't want to fiddle with the CacheSize, but would like to be able to check the actual cache usage at some point to either log if I am hitting the max (as regex usage expands) or dynamically adjust CacheSize.

Alternatively, any comments as to the overhead of simply increasing the CacheSize to some arbitrarily large number?

like image 234
Karl Kieninger Avatar asked Oct 18 '12 14:10

Karl Kieninger


1 Answers

Decompilation (of mscorlib 4.0) reveals that the cache is an internal linked list of CachedCodeEntry, so you're not going to get at it without reflection.

The overheads of increasing the maximum cache size would be:

  1. the memory cost of storing the cached entries; the usage of the maximum is simply in logic like this on Regex creation:

    • are we caching, in general?
      • if so, cache this regex
      • have we now exceeded the maximum cache size?
        • if so, remove the last cache entry


2. the increased cost to traverse the cache looking for a match

So long as your numbers aren't absurd, you should be OK cranking it up.

Here's the reflection code you'd need to retrieve the current cache size:

    public static int RegexCacheSize()
    {
        var fi = typeof(Regex).GetField("livecode", BindingFlags.Static 
                                                  | BindingFlags.NonPublic);
        var coll = (ICollection)(fi.GetValue(null));

        return coll.Count;
    }

We use the cast to ICollection to avoid the complication of having to cast to a generic list on an internal type.

like image 173
AakashM Avatar answered Sep 30 '22 14:09

AakashM