Locking on an interned string?

前端 未结 6 1264
栀梦
栀梦 2021-01-01 16:43

Update: It is acceptable if this method is not thread safe, but I\'m interested in learning how I would make it thread safe. Also, I do not want to lock on

6条回答
  •  温柔的废话
    2021-01-01 17:15

    Problems with @wsanville's own solution, partly mentioned before:

    1. other parts of your code base might lock on the same interned string instances for different purposes, causing only performance issues, if lucky, and deadlocks if unlucky (potentially only in the future, as the code base grows, being extended by coders unaware of your String.Intern locking pattern) - note that this includes locks on the same interned string even if they are in different AppDomains, potentially leading to cross-AppDomain deadlocks
    2. it's impossible for you to reclaim the interned memory in case you decided to do so
    3. String.Intern() is slow

    To address all these 3 issues, you could implement your own Intern() that you tie to your specific locking purpose, i.e. do not use it as a global, general-purpose string interner:

    private static readonly ConcurrentDictionary concSafe = 
        new ConcurrentDictionary();
    static string InternConcurrentSafe(string s)
    {
        return concSafe.GetOrAdd(s, String.Copy);
    }
    

    I called this method ...Safe(), because when interning I will not store the passed in String instance, as that might e.g. be an already interned String, making it subject to the problems mentioned in 1. above.

    To compare the performance of various ways of interning strings, I also tried the following 2 methods, as well as String.Intern.

    private static readonly ConcurrentDictionary conc = 
        new ConcurrentDictionary();
    static string InternConcurrent(string s)
    {
        return conc.GetOrAdd(s, s);
    }
    
    private static readonly Dictionary locked = 
        new Dictionary(5000);
    static string InternLocked(string s)
    {
        string interned;
        lock (locked)
            if (!locked.TryGetValue(s, out interned))
                interned = locked[s] = s;
        return interned;
    }
    

    Benchmark

    100 threads, each randomly selecting one of 5000 different strings (each containing 8 digits) 50000 times and then calling the respective intern method. All values after warming up sufficiently. This is Windows 7, 64bit, on a 4core i5.

    N.B. Warming up the above setup implies that after warming up, there won't be any writes to the respective interning dictionaries, but only reads. It's what I was interested in for the use case at hand, but different write/read ratios will probably affect the results.

    Results

    • String.Intern(): 2032 ms
    • InternLocked(): 1245 ms
    • InternConcurrent(): 458 ms
    • InternConcurrentSafe(): 453 ms

    The fact that InternConcurrentSafe is as fast as InternConcurrent makes sense in light of the fact that these figures are after warming up (see above N.B.), so there are in fact no or only a few invocations of String.Copy during the test.


    In order to properly encapsulate this, create a class like this:

    public class StringLocker
    {
        private readonly ConcurrentDictionary _locks =
            new ConcurrentDictionary();
    
        public string GetLockObject(string s)
        {
            return _locks.GetOrAdd(s, String.Copy);
        }
    }
    

    and after instantiating one StringLocker for every use case you might have, it is as easy as calling

    lock(myStringLocker.GetLockObject(s))
    {
        ...
    

    N.B.

    Thinking again, there's no need to return an object of type string if all you want to do is lock on it, so copying the characters is totally unnecessary, and the following would perform better than above class.

    public class StringLocker
    {
        private readonly ConcurrentDictionary _locks =
            new ConcurrentDictionary();
    
        public object GetLockObject(string s)
        {
            return _locks.GetOrAdd(s, k => new object());
        }
    }
    

提交回复
热议问题