text stringlengths 0 13.4k |
|---|
ConcurrentDictionary shines when it comes to instantly returning of existing keys from cache, mostly lock free, and |
contending on a granular level. But what if the object creation is really expensive, outweighing the cost of context |
switching, and some cache misses occur? |
If the same key is requested from multiple threads, one of the objects resulting from colliding operations will be |
eventually added to the collection, and the others will be thrown away, wasting the CPU resource to create the |
object and memory resource to store the object temporarily. Other resources could be wasted as well. This is really |
bad. |
GoalKicker.com – .NET Framework Notes for Professionals |
17 |
Solution |
We can combine ConcurrentDictionary<TKey, TValue> with Lazy<TValue>. The idea is that ConcurrentDictionary |
GetOrAdd method can only return the value which was actually added to the collection. The loosing Lazy objects |
could be wasted in this case too, but that's not much problem, as the Lazy object itself is relatively unexpensive. The |
Value property of the losing Lazy is never requested, because we are smart to only request the Value property of |
the one actually added to the collection - the one returned from the GetOrAdd method: |
public static class ConcurrentDictionaryExtensions |
{ |
public static TValue GetOrCreateLazy<TKey, TValue>( |
this ConcurrentDictionary<TKey, Lazy<TValue>> d, |
TKey key, |
Func<TKey, TValue> factory) |
{ |
return |
d.GetOrAdd( |
key, |
key1 => |
new Lazy<TValue>(() => factory(key1), |
LazyThreadSafetyMode.ExecutionAndPublication)).Value; |
} |
} |
Caching of XmlSerializer objects can be particularly expensive, and there is a lot of contention at the application |
startup too. And there is more to this: if those are custom serializers, there will be a memory leak too for the rest of |
the process lifecycle. The only benefit of the ConcurrentDictionary in this case is that for the rest of the process |
lifecycle there will be no locks, but application startup and memory usage would be inacceptable. This is a job for |
our ConcurrentDictionary, augmented with Lazy: |
private ConcurrentDictionary<Type, Lazy<XmlSerializer>> _serializers = |
new ConcurrentDictionary<Type, Lazy<XmlSerializer>>(); |
public XmlSerializer GetSerialier(Type t) |
{ |
return _serializers.GetOrCreateLazy(t, BuildSerializer); |
} |
private XmlSerializer BuildSerializer(Type t) |
{ |
throw new NotImplementedException("and this is a homework"); |
} |
GoalKicker.com – .NET Framework Notes for Professionals |
18 |
Chapter 5: Collections |
Section 5.1: Using collection initializers |
Some collection types can be initialized at the declaration time. For example, the following statement creates and |
initializes the numbers with some integers: |
List<int> numbers = new List<int>(){10, 9, 8, 7, 7, 6, 5, 10, 4, 3, 2, 1}; |
Internally, the C# compiler actually converts this initialization to a series of calls to the Add method. Consequently, |
you can use this syntax only for collections that actually support the Add method. |
The Stack<T> and Queue<T> classes do not support it. |
For complex collections such as the Dictionary<TKey, TValue> class, that take key/value pairs, you can specify |
each key/value pair as an anonymous type in the initializer list. |
Dictionary<int, string> employee = new Dictionary<int, string>() |
{{44, "John"}, {45, "Bob"}, {47, "James"}, {48, "Franklin"}}; |
The first item in each pair is the key, and the second is the value. |
Section 5.2: Stack |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.