[Code Index] by Mike Marynowski

programming for work and fun

Fastest Possible Thread-Safe Lock-Free Lookup Dictionary (CopyOnWriteDictionary)

I have been writing a ton of code lately that involves permanently caching objects of various types: generated type schemas, compiled expression queries, reflection lookups for MethodInfo or PropertyInfo, etc. Usually these are updated infrequently in quick bursts, i.e. at application startup, when a new assembly loads, or when a new operation is invoked for the first time that makes use of items that haven't been cached yet. I always felt like the existing options were lacking in usability and/or performance and we could do much better - CopyOnWriteDictionary is the culmination of that pursuit and represents what I consider the ideal dictionary implementation for use cases like this. [More]