Since both of them have the right missing method, I tried Microsoft ConcurrentDictionary and C5 from the University of Copenhagen http://www.itu.dk/research/c5/ , and I can say that, at least with my use case, it was too slow (I mean 5x - 10x slower) compared to Dictionary. I think that C5 sorts keys and values ββall the time, and the Concurrent Dictionary is "too worried" about the calling thread. I am not here to discuss why these two incarnations of the Dictionary are slow. My algorithm looked for and replaced some entries, while the first keys were deleted and new keys would be added (some kind of queue) ... The only thing left to do is change the original .Net mscorelib Dictionary. I downloaded the source code from Microsoft and included the Dictionary class in my source code. For compilation I also need to drag only the HashHelpers and ThrowHelper classes . It remains only to comment on some lines (for example, [DebuggerTypeProxy(typeof(Mscorlib_DictionaryDebugView<,>))] and some resource selections). Obviously, I had to add the missing method to the copied class. Also, do not try to compile the Microsoft source code, which you will do in a few hours, I was lucky to achieve this.
public bool Remove(TKey key, out TValue value) { if (key == null) { ThrowHelper.ThrowArgumentNullException(ExceptionArgument.key); } if (buckets != null) { int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF; int bucket = hashCode % buckets.Length; int last = -1; for (int i = buckets[bucket]; i >= 0; last = i, i = entries[i].next) { if (entries[i].hashCode == hashCode && comparer.Equals(entries[i].key, key)) { if (last < 0) { buckets[bucket] = entries[i].next; } else { entries[last].next = entries[i].next; } entries[i].hashCode = -1; entries[i].next = freeList; entries[i].key = default(TKey); value = entries[i].value; entries[i].value = default(TValue); freeList = i; freeCount++; version++; return true; } } } value = default(TValue); return false; }
Finally, I changed the namespace to System.Collection.Generic.My In my algorithm, I had only two lines, where I got the value, than deleted it in the next line .. replaced it with the new method and got a steady performance increase of 7% - ten%. Hope this helps in this use case and in any other cases where reusing the dictionary from scratch is just not what you need to do.
source share