Regex slow on Windows Server 2008

I have a situation where my regular expressions compile very quickly in Windows Server 2008. I wrote a small console application to highlight this problem. The application generates its own input and creates a regex from the words in the XML file. I created a version of this application and ran it both on my personal laptop (running XP) and on a Windows 2008 server. The regular expression was 0.21 seconds for compilation on my laptop, but 23 seconds for compilation on the server.

Any ideas what could be causing this? The problem is only when using Regex for the first time (when it is compiled first - after that, it's fine)

I also discovered another problem: when using \s+ in regular expression on the same Windows 2008 server, the memory balls (uses 4 GB +) and the Regex compilation never ends.

Is there a known issue with Regex and 64-bit .net? Is there a fix / fix for this? I can’t find any information on the net, but I found several articles about the same problems in Framework 2.0 - is this already fixed?

Additional information: The server runs a 64-bit version of the .net framework (3.5 SP1), and on my laptop I have Visual Studio 2008 and installed version 3.5. The regular expression has the following pattern: ^word$|^word$|^word$ and is created with the following flags: RegexOptions.IgnoreCase | RegexOptions.Compiled RegexOptions.IgnoreCase | RegexOptions.Compiled


Here is the 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 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" /> 
+5
source share
3 answers

I found a solution, considering not the right one, but perfect in my case. For some reason, if I omit the RegexOptions.Compiled flag, Regex runs much faster. I even managed to execute Regex on 100 long sentences in less than 65 milliseconds on a 2008 server.

This should be a bug in the .net lib, since the uncompressed version should be much slower than the compiled version. In any case, less than 1 millisecond per check is very acceptable to me :)

+4
source

You can precompile regular expressions using the Regex.CompileToAssembly method, and then you can deploy the compiled regular expressions to your server.

+4
source

I ran into the same problem. My application works fine on x86 computers, but on memory balls and hangs on x64. Removing the compilation flag did not help. I tried this today on .net 4.0 and the problem remains. If you have a reproduction, I suggest you indicate a mistake.

I think MSFT is aware of this, see bottom comment here

But let them decide if this is the same mistake. Please add a link to your registration here if you have a file, so I can add my comments to it.

+1
source

Source: https://habr.com/ru/post/919868/


All Articles