Regex slow on Windows Server 2008

Posted by pjmyburg on Stack Overflow See other posts from Stack Overflow or by pjmyburg
Published on 2009-09-29T08:06:01Z Indexed on 2010/04/17 8:23 UTC
Read the original article Hit count: 429

Filed under:
|
|

Hi

I have a situation where my regular expressions compile extremely slowly on Windows Server 2008. I wrote a small console application to highlight this issue. The app generates its own input and builds up a Regex from words in an XML file. I built a release version of this app and ran it both on my personal laptop (running XP) and the Windows 2008 server. The regular expression took 0.21 seconds to compile on my laptop, but 23 seconds to compile on the server.

Any ideas what could be causing this? The problem is only on first use of the Regex (when it is first compiled - thereafter it is fine)

I have also found another problem - when using \s+ in the regular expression on the same Windows 2008 server, the memory balloons (uses 4GB+) and the compilation of the Regex never finishes.

Is there a known issue with Regex and 64 bit .net? Is there a fix/patch available for this? I cannot really find any info on the net, but I have found a few articles about this same issues in Framework 2.0 - surely this has been fixed by now?

More info: The server is running the 64 bit version of the .net framework (3.5 SP1) and on my laptop I have Visual Studio 2008 and the 3.5 framework installed. The regular expression is of the following pattern: ^word$|^word$|^word$ and is constructed with the following flags: RegexOptions.IgnoreCase | RegexOptions.Compiled

Edit: Here is a code snippet:

StringBuilder regexString = new StringBuilder();
if (!String.IsNullOrEmpty(fileLocation))
{
    XmlTextReader textReader = new XmlTextReader(fileLocation);
    textReader.Read();
    while (textReader.Read())
    {
        textReader.MoveToElement();
        if (textReader.Name == "word")
        {
            regexString.Append("^" + textReader.GetAttribute(0) + "$|");
        }
    }
    ProfanityFilter = new Regex(regexString.ToString(0, regexString.Length - 1), RegexOptions.IgnoreCase | RegexOptions.Compiled);
}

DateTime time = DateTime.Now;
Console.WriteLine("\nIsProfane:\n" + ProfanityFilter.IsMatch("test"));
Console.WriteLine("\nTime: " + (DateTime.Now - time).TotalSeconds);
Console.ReadKey();

This results in a time of 0.21 seconds on my laptop and 23 seconds on the 2008 server. The XML file consists of 168 words in the following format:

<word text="test" />

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET