18

I am wondering how a piece of locked code can slow down my code even though the code is never executed. Here is an example below:

public void Test_PerformanceUnit()
{
    Stopwatch sw = new Stopwatch();
    sw.Start();
    Random r = new Random();
    for (int i = 0; i < 10000; i++)
    {
        testRand(r);
    }
    sw.Stop();
    Console.WriteLine(sw.ElapsedTicks);
}

public object testRand(Random r)
{
    if (r.Next(1) > 10)
    {
        lock(this) {
            return null;
        }
    }
    return r;
}

This code runs in ~1300ms on my machine. If we remove the lock block (but keep its body), we get 750ms. Almost the double, even though the code is never run!

Of course this code does nothing. I noticed it while adding some lazy initialization in a class where the code checks if the object is initialized and if not initializes it. The problem is that the initialization is locked and slows down everything even after the first call.

My questions are:

  1. Why is this happening?
  2. How to avoid the slowdown
16
  • Unless you intend on using lock intensively - I wouldn't really worry about it.
    – James
    Commented May 6, 2013 at 10:45
  • 4
    I get similar results, but a tick is 100 nano-seconds. Both runs should take ~0ms (i.e. if you print sw.ElapseMilliseconds.) This "slowdown" (of ~0.00006s) is likely due to the fact that lock includes a try/finally block which is probably being setup when the method is called. Try putting the contents of testRand in the loop itself; you'll see almost no slowdown at that point.
    – dlev
    Commented May 6, 2013 at 10:51
  • 1
    Similar topic here. Findings should be pretty much identical. Commented May 6, 2013 at 11:01
  • 1
    I've tracked this to a try{}finally{} statement. If you use it (even empty, with no embedded code) the same slowdown occurs. A lock statement implements a try{}finally{}. Commented May 6, 2013 at 11:07
  • 1
    ANother question with some enlightening answers: stackoverflow.com/questions/6029804/… Commented May 6, 2013 at 11:09

1 Answer 1

10

About why it's happening, it has been discussed in the comments : it's due to the initialization of the try ... finally generated by the lock.


And to avoid this slowdown, you can extract the locking feature to a new method, so that the locking mechanism will only be initialized if the method is actually called.

I tried it with this simple code :

public object testRand(Random r)
{
    if (r.Next(1) > 10)
    {
        return LockingFeature();
    }
    return r;
}

private object LockingFeature()
{
    lock (_lock)
    {
        return null;
    }
}

And here are my times (in ticks) :

your code, no lock   : ~500
your code, with lock : ~1200
my code              : ~500

EDIT : My test code (running a bit slower than the code with no locks) was actually on static methods, it appears that when the code is ran "inside" an object, the timings are the same. I fixed the timings according to that.

2
  • Thanks for the answer, this was exactly what I was looking for. In my test, your solution ran faster than the lock inline but slower than just having a return null. I defined the method LockingFeature as virtual to avoid code inlining and I got 100% of my performance back.
    – pieroxy
    Commented May 6, 2013 at 13:09
  • @pieroxy - Another thing about your first test is that the version of testRand() with the lock takes longer to JIT as well. So you can take that out of the equation by making just one call to testRand() before the Stopwatch begins (as a way of warming up the JIT compiler, so to speak). This narrows the gap significantly. Still, Zonko's code is a pretty slick way of dealing with this. Commented May 6, 2013 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.