-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend Dictionary<K, V> with a GetOrAdd method #32679
Comments
I wouldn't expect that kind of method to be in public API personally. It seems like you will update this value in the end (otherwise I don't see use case for that), so you will end up with dict[key] = modified_value, which will insert keyValue into the Dictionary. ConcurrentDictionary seems to be different animal in that case. As also you have AddOrUpdate which is equivalent to dict[key] = val in case of Dictionary. |
There is already a proposal for adding |
Or consider Dictionaryslim. The comment at the top of it explains the tradeoffs it offers. |
@DevDrake
In c++ the following is possible:
It would be great to have something similar in c# performance wise. |
It's true it has semantic differences eg it requires equation to be cheap. Which difference is blocking in your scenario? |
@danmosemsft As i understand the normal Dictionary<K,V> does store HashCodes for every entry. This makes comparisons inside a bucket a little bit faster (depending on the type). Compared to DictionarySlim<K,V> which skips GetHashCode() call for entries inside a bucket and instead calls .Equals() for every key. Correct me if I'm wrong. What speaks against adding a GetOrAdd() method to the normal Dictionary? I updated my examples in the initial post, i hope they are a little bit clearer now. It looks like DictionarySlim<TKey, TValue> is also missing some very useful interfaces like |
GetOrAdd() assumes the key is not a value type. Yes you're correct that the other requirement is that equating keys is not too expensive. That's a soft requirement though as in most cases there will be no collisions and both dictionaries will equate keys exactly once per lookup |
If you have feedback on it I'd be interested. It might make it into the platform proper if it's clearly useful enough. |
Here are some examples of why such a function would be useful.
|
@CharlesPublic does this add anything to #15059 ? If not I suggest we close this and continue there. |
I think this is duplicated -- let's continue there unless I'm wrong. |
One thing that always annoyed me about the System.Collections.Generic.Dictionary<TKey, TValue> class
is that we often use code like this:
Especially when implementing
algorithms
the double performance cost ofFindValue
is not great.Often times you would want to add an Entry if it does not exist or increment / edit it if it does.
InsertionBehavior.ReturnOnExisting
for TryInsertTryGetValue
to set the value when it does not existFunc<>
,new()
ordefault
ref entry
There are probably other good options.
The ConcurrentDictionary class already has a GetOrAdd() method, but it also uses a double lookup.
The
DictionarySlim.GetOrAddValueRef(TKey)
method looks great.What are the thoughts on this?
The text was updated successfully, but these errors were encountered: