Skip to content
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

Implement 'ConditionalWeakTable<TKey,TValue>.GetOrAdd' APIs #111204

Merged

Conversation

Sergio0694
Copy link
Contributor

Closes #89002

This PR implements the new APIs for ConditionalWeakTable<TKey, TValue> and hides some legacy ones:

namespace System.Runtime.CompilerServices;

public sealed partial class ConditionalWeakTable<TKey, TValue>
{
+   public TValue GetOrAdd(TKey key, TValue value);
+   public TValue GetOrAdd(TKey key, Func<TKey, TValue> valueFactory);
+   public TValue GetOrAdd<TArg>(TKey key, Func<TKey, TArg, TValue> valueFactory, TArg factoryArgument)
+       where TArg : allows ref struct;
    
+   [EditorBrowsable(EditorBrowsableState.Never)]
    public TValue GetValue(TKey key, CreateValueCallback createValueCallback);
  
+   [EditorBrowsable(EditorBrowsableState.Never)]
    public TValue GetOrCreateValue(TKey key);

+   [EditorBrowsable(EditorBrowsableState.Never)]
    public delegate TValue CreateValueCallback(TKey key);
}

Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

1 similar comment
Copy link

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jan 8, 2025
@AaronRobinsonMSFT AaronRobinsonMSFT added this to the 10.0.0 milestone Jan 8, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 2 out of 3 changed files in this pull request and generated 1 comment.

Files not reviewed (1)
  • src/libraries/System.Runtime/ref/System.Runtime.cs: Evaluated as low risk

@jkotas
Copy link
Member

jkotas commented Jan 8, 2025

Go over Corelib and maybe other core assemblies and switch them to use the new API where appropriate? There are not that many candidates.

@Sergio0694
Copy link
Contributor Author

I've gone over uses I could find of the now hidden methods and replaced them. Particularly happy with how ComWrappers on Native AOT looks now, it could drop a couple of extra lookups entirely. @jkoritzinsky can you help take a look? 🙂

@Sergio0694
Copy link
Contributor Author

From rt-sz, after swapping the value typle to a local type (ebbd878):

Project Delta before Delta after
winrt-component-full-windows 3072 1024
winrt-component-minimal-windows 5632 1024

Saved 2 KB and 4.5 KB. I'll take it 😄

@jkoritzinsky
Copy link
Member

Okay that's a lot more savings than I was expecting. Yeah using a different type is worth it.

@Sergio0694
Copy link
Contributor Author

Yeah that's quite a bit, and it must all just be extra value tuple cruft that's unnecessarily kept, given that using a custom type still makes you pay for the generic instantiation. Unrelated to this PR, but @MichalStrehovsky this makes me wonder whether perhaps there could be something general that ILC could do in cases like these to minimize the size impact? 🤔

@MichalStrehovsky
Copy link
Member

Yeah that's quite a bit, and it must all just be extra value tuple cruft that's unnecessarily kept, given that using a custom type still makes you pay for the generic instantiation. Unrelated to this PR, but @MichalStrehovsky this makes me wonder whether perhaps there could be something general that ILC could do in cases like these to minimize the size impact? 🤔

I think this is the same thing as here: #111544 (comment)

We're using ValueTuple in a signature of a delegate. The compiler considers Invoke method on delegate implicitly reflected on. Reflected on methods need extra data structures for parameter types so that we can invoke them. ValueTuple is therefore a target of reflection/MakeGenericType.

@Sergio0694
Copy link
Contributor Author

"The compiler considers Invoke method on delegate implicitly reflected on."

Wait, I thought we (you) had fixed that already a while back? I remember you made some change and we saw a whole lot of savings in CsWinRT thanks to that. IIRC you said delegate targets would only be considered reflection visible if someone were doing .Method on a delegate somewhere, which I assume should not be the case in any of those samples. Am I mixing things up? 😅

@MichalStrehovsky
Copy link
Member

Wait, I thought we (you) had fixed that already a while back? I remember you made some change and we saw a whole lot of savings in CsWinRT thanks to that. IIRC you said delegate targets would only be considered reflection visible if someone were doing .Method on a delegate somewhere, which I assume should not be the case in any of those samples. Am I mixing things up? 😅

The thing I fixed is "do not indiscriminately mark targets of delegates as reflected on" (we were doing that because we didn't know if anyone accessed Delegate.Method property - we now track that).

The thing that is considered reflected on in the case discussed here is the Invoke method of the delegate itself, not the method the delegate points to.

Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@eiriktsarpalis eiriktsarpalis merged commit e66d834 into dotnet:main Jan 29, 2025
142 checks passed
@Sergio0694 Sergio0694 deleted the user/sergiopedri/cwt-getoradd-apis branch January 29, 2025 16:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[API Proposal]: ConditionalWeakTable<TKey,TValue>.GetOrAdd
7 participants