Add ConditionalWeakTable.GetOrAdd API
ConditionalWeakTable currently provides APIs for public TValue GetValue(TKey key, CreateValueCallback createValueCallback) and public void AddOrUpdate(TKey key, TValue value), however it doesn't have any easy and efficient way to do GetOrAdd.
Rationale and Usage
Given a Key it will return the associated Value if the Key already exists in the table, otherwise it will add an entry with the Key, Value to the table.
As @stephentoub pointed out today we can achieve this with cwt.GetValue(key, _ => value);. However, that is inefficient as it creates a delegate and closure.
For an example, we have used the internal implementation here to add items to the delegate marshalling table which is implemented using ConditionalWeakTable.
Proposed API
public sealed class ConditionalWeakTable<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
where TKey : class
where TValue : class
{
// Existing
public ConditionalWeakTable();
~ConditionalWeakTable();
public bool TryGetValue(TKey key, out TValue value);
public void Add(TKey key, TValue value);
public void AddOrUpdate(TKey key, TValue value);
public bool Remove(TKey key);
public void Clear();
public TValue GetValue(TKey key, CreateValueCallback createValueCallback);
public TValue GetOrCreateValue(TKey key);
public delegate TValue CreateValueCallback(TKey key);
//Proposed
public TValue GetValue(TKey key, TValue value);
}
Implementation
I have checked-in an internal implementation of the API here.
cc @jkotas
@shrah can you please format similar to https://github.com/dotnet/corefx/issues/271 .. this makes review easier
Seems reasonable. It can be done today with:
cwt.GetValue(key, _ => value);
but that's inefficient, creating a delegate and a closure just to pass the value in.
@danmosemsft Done.
API review: We are fine with the addition, although this collection was never meant to be general-purpose.
We recommend to make the API name proper overload of GetValue, consistent with this class:
public sealed class ConditionalWeakTable<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
where TKey : class
where TValue : class
{
//Existing:
public TValue GetValue(TKey key, CreateValueCallback createValueCallback);
//Proposed
public TValue GetValue(TKey key, TValue value);
}
The proposed name before was consistent with ConcurrentDictionary. This class is already special and inconsistent with other collections, so let's make it self-consistent.
Updated top post name to GetValue
This should be fairly straightforward and simple (the only complication is adding API - which we have prior examples of).
GetValue(TKey key, TValue value) is really confusing name for this API, I think. It does not suggest that the value will be added to the collection. It feels more like GetValueOrDefault.
What would you propose? Adding:
public TValue GetOrAdd(TKey key, Func<TKey, TAgr> valueFactory);
public TValue GetOrAdd(TKey key, TValue value);
Similar to ConcurrentDictionary:
Let's discuss this option in next API review meeting again (when we have more folks in the room).
Yes, I think public TValue GetOrAdd(TKey key, TValue value) would be the right name.
I do not think that adding TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory) that would duplicate the existing TValue GetValue(TKey key, CreateValueCallback createValueCallback) is worth it.
In a perfect world, we'd have these APIs:
public TValue GetOrAdd(TKey key, Func<TKey, TArg> valueFactory);
public TValue GetOrAdd(TKey key, TValue value);
Since expressed public TValue GetOrAdd(TKey key, Func<TKey, TArg> valueFactory); as TValue GetValue(TKey key, CreateValueCallback createValueCallback) we believe the proposed APIs makes the most sense:
public TValue GetValue(TKey key, CreateValueCallback createValueCallback);
public TValue GetValue(TKey key, TValue value);
It's up-for-grabs again: This should be fairly straightforward and simple (the only complication is adding API - which we have prior examples of).
I can look at this. It'd be my first contribution so I might need a little hand holding
Great! Just sent you invite - let me know when you accept, we can then assign it to you.
Here are some docs (under construction): https://github.com/dotnet/corefx/wiki/Contributions#expose-api-across-coreclrcorefx
@joperezr @AlexGhiondea can help as well.
Alright, looks like I'm in!
Thanks for the pointers. I'll shout if I get stuck.
BTW: If you want to help with updating the docs (wiki is writable without PR process), that would be great!
@jlanng Note that it is implemented here: https://github.com/dotnet/corert/blob/4d6760f3ca6ca17eb31c441bc5d8f5c1e309386c/src/System.Private.CoreLib/src/System/Runtime/CompilerServices/ConditionalWeakTable.cs#L431
So this just needs flipping to public, copying the implementation for CoreCLR repo, and exposing it with tests in CoreFX.
Since expressed
public TValue GetOrAdd(TKey key, Func<TKey, TArg> valueFactory);asTValue GetValue(TKey key, CreateValueCallback createValueCallback)we believe the proposed APIs makes the most sense
@terrajobst I am wondering whether it would be better to not add the API at all. Sure you need to create delegate with the existing API; and cache it if you care about perf that is no different from many other methods that take delegate. But the damage from that may be less than damage from an API with poor consistent name.
I have rewritten the existing case in CoreRT using the existing API w/ CreateValueCallback: https://github.com/dotnet/corert/pull/3491. Surprisingly, it is fewer lines than with the new API.
I do not think that this API is good addition.