Skip to content

Commit bf37f11

Browse files
authored
Reduce allocations from calling ITypeSymbol.AllInterfaces (#79817)
* Reduce allocations from calling ITypeSymbol.AllInterfaces This method accounts for 2.0% of allocations during completion in the CompletionInCohosting Razor speedometer test. Razor uses this method from a couple disparate locations (mostly around detecting taghelpers and components). I've verified locally that about 2/3 of these allocations can be removed by having the InterfaceInfo cache the public symbol array information too.
1 parent 6669099 commit bf37f11

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace Microsoft.CodeAnalysis.CSharp.Symbols.PublicModel
1111
{
1212
internal abstract class TypeSymbol : NamespaceOrTypeSymbol, ISymbol, ITypeSymbol
1313
{
14+
private ImmutableArray<INamedTypeSymbol> _allInterfaces = default;
15+
1416
protected TypeSymbol(CodeAnalysis.NullableAnnotation nullableAnnotation)
1517
{
1618
NullableAnnotation = nullableAnnotation;
@@ -108,7 +110,12 @@ ImmutableArray<INamedTypeSymbol> ITypeSymbol.AllInterfaces
108110
{
109111
get
110112
{
111-
return UnderlyingTypeSymbol.AllInterfacesNoUseSiteDiagnostics.GetPublicSymbols();
113+
if (_allInterfaces.IsDefault)
114+
{
115+
ImmutableInterlocked.InterlockedInitialize(ref _allInterfaces, UnderlyingTypeSymbol.AllInterfacesNoUseSiteDiagnostics.GetPublicSymbols());
116+
}
117+
118+
return _allInterfaces;
112119
}
113120
}
114121

0 commit comments

Comments
 (0)