[Mono-dev] Performance of compiled regular expressions?

Daniel Lo Nigro lists at dan.cx
Mon Dec 3 03:03:44 UTC 2012


Are you sure your site is running in release mode (debug="false" in your
web.config, and Release build config for assemblies)? Running in debug mode
will turn off lots of optimisations that may cause slowdown.

It might be worth trying out
Regex.CompileToAssembly<http://msdn.microsoft.com/en-us/library/9ek5zak6.aspx>to
precompile your regular expressions - This will create a separate .NET
assembly which has pre-optimised versions of the regular expressions.
However I've found this is only beneficial for very large regular
expressions, and regular expressions that are used a lot. So it might not
help too much (but it doesn't hurt to try it to see if it makes a
difference or not). As far as I know, Mono doesn't
support Regex.CompileToAssembly, but you should be able to generate the
assembly on Microsoft .NET, and then use the generated assembly with Mono.
I haven't tried this myself though.

Out of curiosity, how are you profiling the code?


On Mon, Dec 3, 2012 at 1:20 PM, Jordan Earls <earlz at lastyearswishes.com>wrote:

> So, I recently did a major update to my ASP.Net website running on
> Mono (runs on .Net as well, but I work on it and host it with Mono).
> Long story short, my websites performance sunk big time. It now takes
> 110ms for a response that once took less than 20ms.
>
> I traced down the big difference to using regular expressions as a
> part of my routing and a few other things. (I have a custom MVC
> framework: https://bitbucket.org/earlz/barelymvc).
>
> Here are some(maybe all) of the regular expressions being used
> (they're really simple):
>
> @"[^a-zA-Z0-9]\ "
> @"[^a-zA-Z0-9\-]"
> "^[0-9a-zA-Z]+$"
> @"^[-+]?[0-9]*\.?[0-9]+$"
>  "^[-+]?[0-9]*$"
> "^[0-9A-Fa-f]+$"
>
> All of these regular expressions are compiled once at application
> startup and cached. (with RegexOptions.Compiled). They are used with
> with IsMatch(input) or Replace(input, replacement);
>
> When profiling my application however, the number one most time
> consuming method was `System.Text.RegularExpressions.Interpreter:Eval
> (System.Text.RegularExpressions.Interpreter/Mode,int&,int)` (my
> database functions, which are also not really fast didn't even compare
> to the amount of time this function consumed)
>
> Is there anything I can do to mitigate this problem? I'm using the
> latest stable version of Mono (Arch Linux) on a 64-bit platform. Is it
> true also that regular expression should now be on par with .Net's
> implementation? From what I understand, mono compiles them to IL
> correct? Is there anything to watch for in regular expressions that
> could give me performance problems like this?
>
> Thanks,
> Jordan
> _______________________________________________
> Mono-devel-list mailing list
> Mono-devel-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-devel-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-devel-list/attachments/20121203/f3d78193/attachment.html>


More information about the Mono-devel-list mailing list