.net - Extend C# Dictionary for Atomic AddOrUpdate -
previously asked question on atomic addorupdate on c# dictionary. answer got extend c# dictionary
implementation, found quite rational.
i extended dictionary
implementation suggested, however, performance surprisingly bad!! tried minimize tweaks on c# implementation trace cause. minimum reach was: created addorupdate
function, has similar signature add
except returns bool
if dictionary contained key
, it's value updated given value
, or false if otherwise. on source code made following changes:
public bool addorupdate(tkey key, tvalue value) { return insert(key, value); }
and
private bool insert(tkey key, tvalue value) { if (buckets == null) initialize(0); int hashcode = comparer.gethashcode(key) & 0x7fffffff; int targetbucket = hashcode % buckets.length; (int = buckets[targetbucket]; >= 0; = entries[i].next) { if (entries[i].hashcode == hashcode && comparer.equals(entries[i].key, key)) { entries[i].value = value; version++; return true; // on original code, returns void } } int index; if (freecount > 0) { index = freelist; freelist = entries[index].next; freecount--; } else { if (count == entries.length) { resize(); targetbucket = hashcode % buckets.length; } index = count; count++; } entries[index].hashcode = hashcode; entries[index].next = buckets[targetbucket]; entries[index].key = key; entries[index].value = value; buckets[targetbucket] = index; version++; return false; // on original code, not return }
i profiled cpu performance on code, here few snapshots (note: lambdas
dictionary of modified type):
comparison: code without atomic addorupdate taking 2min, not finish! while occupies more 10gb of ram , takes ever!!
am missing point ?
i guess has you've removed setting of iequalitycomparer
.
but instead of adapting insert method i'd suggest call original insert
add flag set false
addorupdate
method. because insert
behaves add or update method would.
Comments
Post a Comment