Skip to content

Commit 43ce29e

Browse files
authored
Avoid creating identity to obtain public key (#78255)
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. See PR for images outlining before / after allocation patterns.
1 parent 6091e3c commit 43ce29e

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

src/Compilers/CSharp/Portable/Symbols/PublicModel/AssemblySymbol.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,15 @@ bool IAssemblySymbol.GivesAccessTo(IAssemblySymbol assemblyWantingAccess)
8181

8282
AssemblyIdentity identity = UnderlyingAssemblySymbol.Identity;
8383

84+
// Avoid using the identity to obtain the public key if possible to avoid the allocations associated
85+
// with identity creation
86+
ImmutableArray<byte> publicKey = (assemblyWantingAccess is AssemblySymbol assemblyWantingAccessAssemblySymbol)
87+
? assemblyWantingAccessAssemblySymbol.UnderlyingAssemblySymbol.PublicKey.NullToEmpty()
88+
: assemblyWantingAccess.Identity.PublicKey;
89+
8490
foreach (var key in myKeys)
8591
{
86-
IVTConclusion conclusion = identity.PerformIVTCheck(assemblyWantingAccess.Identity.PublicKey, key);
92+
IVTConclusion conclusion = identity.PerformIVTCheck(publicKey, key);
8793
Debug.Assert(conclusion != IVTConclusion.NoRelationshipClaimed);
8894
if (conclusion == IVTConclusion.Match || conclusion == IVTConclusion.OneSignedOneNot)
8995
{

src/Compilers/VisualBasic/Portable/Symbols/AssemblySymbol.vb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,8 +750,13 @@ Namespace Microsoft.CodeAnalysis.VisualBasic.Symbols
750750
Return True
751751
End If
752752

753+
' Avoid using the identity to obtain the public key if possible to avoid the allocations associated
754+
' with identity creation
755+
Dim assemblyWantingAccessAssemblySymbol As AssemblySymbol = TryCast(assemblyWantingAccess, AssemblySymbol)
756+
Dim publicKey = If(assemblyWantingAccessAssemblySymbol IsNot Nothing, assemblyWantingAccessAssemblySymbol.PublicKey.NullToEmpty(), assemblyWantingAccess.Identity.PublicKey)
757+
753758
For Each key In myKeys
754-
Dim conclusion As IVTConclusion = Me.Identity.PerformIVTCheck(assemblyWantingAccess.Identity.PublicKey, key)
759+
Dim conclusion As IVTConclusion = Me.Identity.PerformIVTCheck(publicKey, key)
755760
Debug.Assert(conclusion <> IVTConclusion.NoRelationshipClaimed)
756761
If conclusion = IVTConclusion.Match Then
757762
' Note that C# includes OrElse conclusion = IVTConclusion.OneSignedOneNot

0 commit comments

Comments
 (0)