-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
[API Proposal]: ConditionalWeakTable<TKey,TValue>.Remove overload #111925
Comments
Tagging subscribers to this area: @dotnet/area-system-runtime-compilerservices |
@eiriktsarpalis small follow up to #89002, if you will. Would you be able to help get this to API review? 😄 |
This would also match the existing patterns established by other collections like |
Was just thinking broader beyond this; in the future, should |
Note that |
Note that the existing |
Makes sense to me, updated! 🙂 |
namespace System.Runtime.CompilerServices;
public sealed partial class ConditionalWeakTable<TKey, TValue>
{
public bool Remove(TKey key, [MaybeNullWhen(false)] out TValue value);
} |
Background and motivation
ConditionalWeakTable<TKey, TValue>
has aRemove
method that internally also retrieves the removed item, if present, but does not surface it. There is no existing method on it that allows removing an item while also getting the removed item, if present. This makes it unnecessarily expensive to opportunistically remove items while also doing something with them (eg. removing them from some other data structure).Eg. we hit this in the Windows Community Toolkit (CommunityToolkit/Windows#569):
It would be nice to just be able to do this with a single API, which would be simpler to read and also more efficient:
The new API would also make the whole operation atomic, which is also nice to have and might be necessary in some scenarios.
Implementing the new API is trivial, as
ConditionalWeakTable<TKey, TValue>
already has the necessary logic, just not exposed.API Proposal
namespace System.Runtime.CompilerServices; public sealed class ConditionalWeakTable<TKey, TValue> { public bool Remove(TKey key); + public bool Remove(TKey key, [MaybeNullWhen(false)] out TValue value); }
API Usage
Updating the example above to use the new API:
Alternative Designs
Keep using
TryGetValue
+Remove
, and waste an extra lookup. Also the whole thing is not atomic. And it's clunkier.Risks
None, it's just a new convenience API with a well known signature also used on lots of other similar data structures.
The text was updated successfully, but these errors were encountered: