Ayende @ Rahien

Hi!
My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:

ayende@ayende.com

+972 52-548-6969

, @ Q c

Posts: 18 | Comments: 82

filter by tags archive

UberProf performance improvements, nothing helps if you are stupid

time to read 2 min | 274 words

The following change took a while to figure out, but it was a huge performance benefit (think, 5 orders of magnitude). The code started as:

private readonly Regex startOfParametersSection = 
            new Regex(@"(;\s*)[@:?]p0 =", RegexOptions.Compiled);

And the optimization is:

private static readonly Regex startOfParametersSection = 
            new Regex(@"(;\s*)[@:?]p0 =", RegexOptions.Compiled);

The story behind this is interesting, this piece of code (and a few others like it) used to be in a class that has a singleton lifestyle. At some point, it was refactored into a command class that is created often, which obviously had… drastic effect on the system performance.


Comments

meo
meo

Funny bug. It seems this object was polluting address space of application while being compiled again and again into new assembly and hooking up till app ends.

Frans Bouma

still odd though, as creating a regex will always pull a cached instance into itself which does the actual work. You have a little bit of overhead from looking up the instance and the key construction (check the ctor) but it's not recompiled over and over again. So that it has drastic performance improvements is a little unexpected.

James Newton-King

Creating a new Regex object with the compiled option does cause it to be compiled over and over, destroying performance. I've been caught out by it myself.

The solution is to either reuse the same object or use one of the static methods on Regex, which do look up to see whether a compiled instance of the regex already exists.

firefly

MS actually recommend to use the static methods on Regex in most cased since it'll be cached automatically. I've always found it more intuitive to do the caching myself hence turn it into a static field.

Ayende Rahien

Firefly,

The problem is that you only get ~15 LRU cache.

That doesn't work for something like the profiler, who does a LOT of regexes

Peli

Have you tought about not using Regexes at all?

Ayende Rahien

I have to do a lot of text processing.

Can you think of anything better suited than regexes to do that?

From both performance and maintainability points of view?

Kelly Stuard

"Nothing helps if you are stupid" -- love it; my QOTD!

Chris Kemp

Definitely QOTD, and quite fitting, given the emails that I've just been handling today.

Comment preview

Comments have been closed on this topic.

FUTURE POSTS

  1. The insidious cost of allocations - 3 hours from now
  2. Buffer allocation strategies: A possible solution - 3 days from now
  3. Buffer allocation strategies: Explaining the solution - 4 days from now
  4. Buffer allocation strategies: Bad usage patterns - 5 days from now
  5. The useless text book algorithms - 6 days from now

And 1 more posts are pending...

There are posts all the way to Sep 11, 2015

RECENT SERIES

  1. Find the bug (5):
    20 Apr 2011 - Why do I get a Null Reference Exception?
  2. Production postmortem (10):
    03 Sep 2015 - The industry at large
  3. What is new in RavenDB 3.5 (7):
    12 Aug 2015 - Monitoring support
  4. Career planning (6):
    24 Jul 2015 - The immortal choices aren't
View all series

Syndication

Main feed Feed Stats
Comments feed   Comments Feed Stats