Skip to content

Commit

Permalink
perf: Avoid using GetAllTypesAttributedWith
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Feb 24, 2023
1 parent 6627eb8 commit eed1949
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ internal class RoslynMetadataHelper
{
private readonly INamedTypeSymbol _nullableSymbol;
private readonly Func<string, ITypeSymbol> _findTypeByFullName;
private readonly Func<INamedTypeSymbol, INamedTypeSymbol[]> _getAllTypesAttributedWith;

public Compilation Compilation { get; }

Expand All @@ -27,7 +26,6 @@ public RoslynMetadataHelper(GeneratorExecutionContext context)
Compilation = context.Compilation;

_findTypeByFullName = Funcs.Create<string, ITypeSymbol>(SourceFindTypeByFullName).AsLockedMemoized();
_getAllTypesAttributedWith = Funcs.Create<INamedTypeSymbol, INamedTypeSymbol[]>(SourceGetAllTypesAttributedWith).AsLockedMemoized();
_nullableSymbol = Compilation.GetSpecialType(SpecialType.System_Nullable_T);
}

Expand All @@ -47,7 +45,7 @@ private ITypeSymbol SourceFindTypeByFullName(string fullName)

if (symbol == null)
{
// This type resolution is required because there is no way (yet) to get a type
// This type resolution is required because there is no way (yet) to get a type
// symbol from a string for types that are not "simple", like generic types or arrays.

// We then use a temporary documents that contains all the known
Expand Down Expand Up @@ -88,11 +86,5 @@ public ITypeSymbol GetTypeByFullName(string fullName)

return symbol;
}

public INamedTypeSymbol[] GetAllTypesAttributedWith(INamedTypeSymbol attributeClass)
=> _getAllTypesAttributedWith(attributeClass);

private INamedTypeSymbol[] SourceGetAllTypesAttributedWith(INamedTypeSymbol attributeClass)
=> Compilation.GlobalNamespace.GetNamespaceTypes().Where(t => t.FindAttribute(attributeClass) is { }).ToArray();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ internal partial class XamlFileGenerator
private readonly INamedTypeSymbol _styleSymbol;
private readonly INamedTypeSymbol _setterSymbol;
private readonly INamedTypeSymbol _colorSymbol;

private readonly List<INamedTypeSymbol> _xamlConversionTypes;
private readonly INamedTypeSymbol? _createFromStringAttributeSymbol;

private readonly bool _isWasm;

Expand Down Expand Up @@ -310,11 +309,11 @@ public XamlFileGenerator(
_dataBindingSymbol = (INamedTypeSymbol)_metadataHelper.GetTypeByFullName("Windows.UI.Xaml.Data.Binding");
_styleSymbol = (INamedTypeSymbol)_metadataHelper.GetTypeByFullName(XamlConstants.Types.Style);
_colorSymbol = (INamedTypeSymbol)_metadataHelper.GetTypeByFullName(XamlConstants.Types.Color);
_createFromStringAttributeSymbol = _metadataHelper.GetTypeByFullName(XamlConstants.Types.CreateFromStringAttribute) as INamedTypeSymbol;
_androidContentContextSymbol = _metadataHelper.FindTypeByFullName("Android.Content.Context") as INamedTypeSymbol;
_androidViewSymbol = _metadataHelper.FindTypeByFullName("Android.Views.View") as INamedTypeSymbol;
_iOSViewSymbol = _metadataHelper.FindTypeByFullName("UIKit.UIView") as INamedTypeSymbol;
_appKitViewSymbol = _metadataHelper.FindTypeByFullName("AppKit.NSView") as INamedTypeSymbol;
_xamlConversionTypes = _metadataHelper.GetAllTypesAttributedWith((INamedTypeSymbol)_metadataHelper.GetTypeByFullName(XamlConstants.Types.CreateFromStringAttribute)).ToList();
ShouldWriteErrorOnInvalidXaml = shouldWriteErrorOnInvalidXaml;

_isWasm = isWasm;
Expand Down Expand Up @@ -2282,7 +2281,7 @@ private bool IsCustomMarkupExtensionType(XamlType? xamlType) =>

private bool IsXamlTypeConverter(INamedTypeSymbol? symbol)
{
return _xamlConversionTypes.Any(ns => SymbolEqualityComparer.Default.Equals(ns, symbol));
return symbol?.GetAttributes().Any(a => a.AttributeClass?.Equals(_createFromStringAttributeSymbol) == true) == true;
}

private string BuildXamlTypeConverterLiteralValue(INamedTypeSymbol? symbol, string memberValue, bool includeQuotations)
Expand Down

0 comments on commit eed1949

Please sign in to comment.