Skip to content

Conversation

@ToddGrun
Copy link
Contributor

@ToddGrun ToddGrun commented Apr 22, 2025

Reduce allocations in AssemblySymbol.GivesAccessTo due to getting the assemblyWantingAccess's Identity.

During the completion scenarios in the C# speedometer tests, about 3% of allocations in the VS process are used obtaining this identity just to get the PublicKey. However, in the case where assemblyWantingAccess is an AssemblySymbol, there is a way to obtain this without needing to obtain the full Identity.

*** Previous allocations ***
image

*** Allocations with this change ***
image

To demonstrate that these Identity calls weren't just calculated at a later point, below are the allocations due to calls to get_Identity before/after:

*** Previous allocations in get_Identity ***
image

*** Allocations in get_Identity with this change ***
image

Going to start in draft mode and will promote if a test insertion shows value.

Will update with performance justitications at that point.
@ghost ghost added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Apr 22, 2025
@ToddGrun ToddGrun changed the title *** WIP: Avoid creating identity to obtain public key Avoid creating identity to obtain public key Apr 23, 2025
@ToddGrun ToddGrun marked this pull request as ready for review April 23, 2025 14:27
@ToddGrun ToddGrun requested a review from a team as a code owner April 23, 2025 14:27
@ToddGrun
Copy link
Contributor Author

@dotnet/roslyn-compiler -- This is ready for review


// Avoid using the identity to obtain the public key if possible to avoid the allocations associated
// with identity creation
ImmutableArray<byte> publicKey = (assemblyWantingAccess is AssemblySymbol assemblyWantingAccessAssemblySymbol)
Copy link
Contributor

@AlekseyTs AlekseyTs Apr 23, 2025

Choose a reason for hiding this comment

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

publicKey

I think we should make a similar change for VB implementation as well #Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep, good catch. Done in commit 2.

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 1)

@ToddGrun
Copy link
Contributor Author

@AlekseyTs -- Question for you.

There remains about 50 MB (1%) of allocations from calls to get_Identity in the after image above where AssemblySymbolKey.Resolve is using the Identity to get the Name. I notice there is also a Name property on ISymbol. Is ISymbol.Name in that context the same as the IAssemblySymbol.Identity.Name value? It appears to be from the debugging I've done, and if so, I'd like to go ahead and change that too.

' Avoid using the identity to obtain the public key if possible to avoid the allocations associated
' with identity creation
Dim publicKey As ImmutableArray(Of Byte)
If TypeOf assemblyWantingAccess Is AssemblySymbol Then
Copy link
Contributor

@AlekseyTs AlekseyTs Apr 23, 2025

Choose a reason for hiding this comment

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

If TypeOf assemblyWantingAccess Is AssemblySymbol Then

I think doing the following instead would be more efficient:

Dim assemblyWantingAccessAssemblySymbol As AssemblySymbol = TryCast(assemblyWantingAccess, AssemblySymbol)
If assemblyWantingAccessAssemblySymbol IsNot Nothing Then

#Closed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Commit 3. TryCast is the VB call that I tend to forget about.

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 2)

@AlekseyTs
Copy link
Contributor

There remains about 50 MB (1%) of allocations from calls to get_Identity in the after image above where AssemblySymbolKey.Resolve is using the Identity to get the Name. I notice there is also a Name property on ISymbol. Is ISymbol.Name in that context the same as the IAssemblySymbol.Identity.Name value? It appears to be from the debugging I've done, and if so, I'd like to go ahead and change that too.

Honestly, I do not know the answer to this question. To be comfortable with a change like that, I think a more thorough investigation should be done (an examination of all possible ways the property and the assembly identity get their values, etc.). We would also like to place asserts in certain places enforcing the invariant (assuming we will prove there is an invariant today) going forward.

Copy link
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

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

LGTM (commit 3)

@ToddGrun
Copy link
Contributor Author

@dotnet/roslyn-compiler for 2nd review. Thanks!

@ToddGrun ToddGrun merged commit 43ce29e into dotnet:main Apr 24, 2025
24 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Apr 24, 2025
@RikkiGibson RikkiGibson modified the milestones: Next, 18.0 P1 Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants