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
Problems with @wsanville's own solution, partly mentioned before:
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 deadlocksString.Intern()
is slowTo 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 msInternLocked()
: 1245 msInternConcurrent()
: 458 msInternConcurrentSafe()
: 453 msThe 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.
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());
}
}