Can I get rid of this read lock?

Posted by Pieter on Stack Overflow See other posts from Stack Overflow or by Pieter
Published on 2011-01-11T06:23:22Z Indexed on 2011/01/11 6:53 UTC
Read the original article Hit count: 254

Filed under:
|
|

I have the following helper class (simplified):

public static class Cache
{
    private static readonly object _syncRoot = new object();
    private static Dictionary<Type, string> _lookup = new Dictionary<Type, string>();

    public static void Add(Type type, string value)
    {
        lock (_syncRoot)
        {
            _lookup.Add(type, value);
        }
    }

    public static string Lookup(Type type)
    {
        string result;

        lock (_syncRoot)
        {
            _lookup.TryGetValue(type, out result);
        }

        return result;
    }
}

Add will be called roughly 10/100 times in the application and Lookup will be called by many threads, many of thousands of times. What I would like is to get rid of the read lock.

How do you normally get rid of the read lock in this situation?

I have the following ideas:

  1. Require that _lookup is stable before the application starts operation. The could be build up from an Attribute. This is done automatically through the static constructor the attribute is assigned to. Requiring the above would require me to go through all types that could have the attribute and calling RuntimeHelpers.RunClassConstructor which is an expensive operation;

  2. Move to COW semantics.

    public static void Add(Type type, string value)
    {
        lock (_syncRoot)
        {
            var lookup = new Dictionary<Type, string>(_lookup);
    
            lookup.Add(type, value);
    
            _lookup = lookup;
        }
    }
    

    (With the lock (_syncRoot) removed in the Lookup method.) The problem with this is that this uses an unnecessary amount of memory (which might not be a problem) and I would probably make _lookup volatile, but I'm not sure how this should be applied. (John Skeets' comment here gives me pause.)

  3. Using ReaderWriterLock. I believe this would make things worse since the region being locked is small.

Suggestions are very welcome.

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET