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

Reduce allocations in TypeSymbolExtensions.IsAtLeastAsVisibleAs #75401

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions src/Compilers/CSharp/Portable/Symbols/TypeSymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ namespace Microsoft.CodeAnalysis.CSharp.Symbols
{
internal static partial class TypeSymbolExtensions
{
private sealed class VisitTypeData
{
public Symbol? Symbol;
public CompoundUseSiteInfo<AssemblySymbol> UseSiteInfo;
}

private static readonly ObjectPool<VisitTypeData> s_visitTypeDataPool
= new ObjectPool<VisitTypeData>(() => new VisitTypeData(), size: 4);

public static bool ImplementsInterface(this TypeSymbol subType, TypeSymbol superInterface, ref CompoundUseSiteInfo<AssemblySymbol> useSiteInfo)
{
foreach (NamedTypeSymbol @interface in subType.AllInterfacesWithDefinitionUseSiteDiagnostics(ref useSiteInfo))
Expand Down Expand Up @@ -663,11 +672,26 @@ public static SpecialType GetSpecialTypeSafe(this TypeSymbol? type)

public static bool IsAtLeastAsVisibleAs(this TypeSymbol type, Symbol sym, ref CompoundUseSiteInfo<AssemblySymbol> useSiteInfo)
{
CompoundUseSiteInfo<AssemblySymbol> localUseSiteInfo = useSiteInfo;
var result = type.VisitType((type1, symbol, unused) => IsTypeLessVisibleThan(type1, symbol, ref localUseSiteInfo), sym,
canDigThroughNullable: true); // System.Nullable is public
useSiteInfo = localUseSiteInfo;
return result is null;
var visitTypeData = s_visitTypeDataPool.Allocate();

try
{
visitTypeData.UseSiteInfo = useSiteInfo;
visitTypeData.Symbol = sym;

var result = type.VisitType(static (type1, arg, unused) => IsTypeLessVisibleThan(type1, arg.Symbol!, ref arg.UseSiteInfo),
arg: visitTypeData,
canDigThroughNullable: true); // System.Nullable is public

useSiteInfo = visitTypeData.UseSiteInfo;
return result is null;
}
finally
{
visitTypeData.UseSiteInfo = default;
visitTypeData.Symbol = null;
s_visitTypeDataPool.Free(visitTypeData);
}
}

private static bool IsTypeLessVisibleThan(TypeSymbol type, Symbol sym, ref CompoundUseSiteInfo<AssemblySymbol> useSiteInfo)
Expand Down
Loading