runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Add ConditionalWeakTable.GetOrAdd API

Open shrah opened this issue 9 years ago • 21 comments

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.

shrah avatar Apr 18 '17 23:04 shrah

cc @jkotas

shrah avatar Apr 18 '17 23:04 shrah

@shrah can you please format similar to https://github.com/dotnet/corefx/issues/271 .. this makes review easier

danmoseley avatar Apr 19 '17 03:04 danmoseley

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.

stephentoub avatar Apr 19 '17 11:04 stephentoub

@danmosemsft Done.

shrah avatar Apr 19 '17 18:04 shrah

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.

karelz avatar Apr 25 '17 17:04 karelz

Updated top post name to GetValue

karelz avatar Apr 25 '17 17:04 karelz

This should be fairly straightforward and simple (the only complication is adding API - which we have prior examples of).

karelz avatar Apr 25 '17 17:04 karelz

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.

jkotas avatar Apr 25 '17 17:04 jkotas

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).

karelz avatar Apr 25 '17 18:04 karelz

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.

jkotas avatar Apr 25 '17 18:04 jkotas

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);

terrajobst avatar May 02 '17 17:05 terrajobst

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).

karelz avatar May 02 '17 18:05 karelz

I can look at this. It'd be my first contribution so I might need a little hand holding

jlanng avatar May 02 '17 19:05 jlanng

Great! Just sent you invite - let me know when you accept, we can then assign it to you.

karelz avatar May 02 '17 19:05 karelz

Here are some docs (under construction): https://github.com/dotnet/corefx/wiki/Contributions#expose-api-across-coreclrcorefx

@joperezr @AlexGhiondea can help as well.

karelz avatar May 02 '17 19:05 karelz

Alright, looks like I'm in!

Thanks for the pointers. I'll shout if I get stuck.

jlanng avatar May 02 '17 19:05 jlanng

BTW: If you want to help with updating the docs (wiki is writable without PR process), that would be great!

karelz avatar May 02 '17 20:05 karelz

@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.

jkotas avatar May 02 '17 20:05 jkotas

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

@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.

jkotas avatar May 02 '17 20:05 jkotas

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.

jkotas avatar May 03 '17 01:05 jkotas