Skip to content

Commit b65d089

Browse files
committed
Slightly improved perf of source gen and analyzers
1 parent 4e4070d commit b65d089

16 files changed

+81
-84
lines changed

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<Copyright>(c) $([System.DateTime]::Now.Year), Pawel Gerr. All rights reserved.</Copyright>
5-
<VersionPrefix>8.0.0</VersionPrefix>
5+
<VersionPrefix>8.0.1</VersionPrefix>
66
<Authors>Pawel Gerr</Authors>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<PackageProjectUrl>https://github.com/PawelGerr/Thinktecture.Runtime.Extensions</PackageProjectUrl>

samples/Directory.Build.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<IsPackable>false</IsPackable>
1313
<ImplicitUsings>disable</ImplicitUsings>
1414
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
15+
<ReportAnalyzer>true</ReportAnalyzer>
1516
<ThinktectureRuntimeExtensions_SourceGenerator_LogFilePathMustBeUnique>false</ThinktectureRuntimeExtensions_SourceGenerator_LogFilePathMustBeUnique>
1617
<ThinktectureRuntimeExtensions_SourceGenerator_LogFilePath>C:\temp\ttre_samples_logs.txt</ThinktectureRuntimeExtensions_SourceGenerator_LogFilePath>
1718
</PropertyGroup>

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/AdHocUnions/AdHocUnionSourceGenerator.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ private bool IsUnionCandidate(TypeDeclarationSyntax typeDeclaration)
120120
return null;
121121
}
122122

123-
if (attributeType.TypeArguments.Length < 2)
123+
if (attributeType.Arity < 2)
124124
{
125-
Logger.LogDebug($"Expected the attribute type to have at least 2 type arguments but found {attributeType.TypeArguments.Length.ToString()}", tds);
125+
Logger.LogDebug($"Expected the attribute type to have at least 2 type arguments but found {attributeType.Arity.ToString()}", tds);
126126
return null;
127127
}
128128

@@ -140,9 +140,9 @@ private bool IsUnionCandidate(TypeDeclarationSyntax typeDeclaration)
140140
return new SourceGenContext(new SourceGenError("Could not fetch type information for code generation of a discriminated union", tds));
141141

142142
var settings = new AdHocUnionSettings(context.Attributes[0],
143-
attributeType.TypeArguments.Length,
143+
attributeType.Arity,
144144
attributeInfo);
145-
var memberTypeStates = new AdHocUnionMemberTypeState[attributeType.TypeArguments.Length];
145+
var memberTypeStates = attributeType.Arity == 0 ? [] : new AdHocUnionMemberTypeState[attributeType.Arity];
146146

147147
for (var i = 0; i < attributeType.TypeArguments.Length; i++)
148148
{

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/Diagnostics/ThinktectureRuntimeExtensionsAnalyzer.cs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ private static void AnalyzeMethodCall(OperationAnalysisContext context)
144144
if (declaredType.IsEnum(out var attribute))
145145
{
146146
var isValidatable = attribute.FindIsValidatable() ?? false;
147-
var nonIgnoredMembers = declaredType.GetNonIgnoredMembers();
148-
var items = declaredType.GetEnumItems(nonIgnoredMembers);
147+
var items = declaredType.GetEnumItems();
149148

150149
AnalyzeEnumSwitchMap(context,
151150
items,
@@ -482,16 +481,15 @@ private static void ValidateCustomKeyMemberImplementation(
482481
TypeMustBePartial(context, type);
483482
TypeMustNotBeGeneric(context, type, locationOfFirstDeclaration, "Value Object");
484483

485-
var nonIgnoredItems = type.GetNonIgnoredMembers();
486-
var assignableMembers = type.GetAssignableFieldsAndPropertiesAndCheckForReadOnly(nonIgnoredItems, factory, false, true, context.CancellationToken, context)
484+
var assignableMembers = type.GetAssignableFieldsAndPropertiesAndCheckForReadOnly(factory, false, true, context.CancellationToken, context)
487485
.Where(m => !m.IsStatic)
488486
.ToList();
489487

490488
var baseClass = type.BaseType;
491489

492490
while (!baseClass.IsNullOrObject())
493491
{
494-
baseClass.IterateAssignableFieldsAndPropertiesAndCheckForReadOnly(baseClass.GetNonIgnoredMembers(), false, context.CancellationToken, locationOfFirstDeclaration, context).Enumerate();
492+
baseClass.IterateAssignableFieldsAndPropertiesAndCheckForReadOnly(false, context.CancellationToken, locationOfFirstDeclaration, context).Enumerate();
495493

496494
baseClass = baseClass.BaseType;
497495
}
@@ -557,22 +555,21 @@ private static void ValidateEnum(
557555
TypeMustBePartial(context, enumType);
558556
TypeMustNotBeGeneric(context, enumType, locationOfFirstDeclaration, "Enumeration");
559557

560-
var nonIgnoredMembers = enumType.GetNonIgnoredMembers();
561-
var items = enumType.GetEnumItems(nonIgnoredMembers);
558+
var items = enumType.GetEnumItems();
562559

563560
if (items.Length == 0)
564561
ReportDiagnostic(context, DiagnosticsDescriptors.EnumerationHasNoItems, locationOfFirstDeclaration, enumType);
565562

566-
Check_ItemLike_StaticProperties(context, enumType, nonIgnoredMembers);
563+
Check_ItemLike_StaticProperties(context, enumType);
567564
EnumItemsMustBePublic(context, enumType, items);
568565

569-
_ = enumType.GetAssignableFieldsAndPropertiesAndCheckForReadOnly(nonIgnoredMembers, factory, false, false, context.CancellationToken, context).ToList();
566+
_ = enumType.GetAssignableFieldsAndPropertiesAndCheckForReadOnly(factory, false, false, context.CancellationToken, context).ToList();
570567

571568
var baseClass = enumType.BaseType;
572569

573570
while (!baseClass.IsNullOrObject())
574571
{
575-
baseClass.IterateAssignableFieldsAndPropertiesAndCheckForReadOnly(baseClass.GetNonIgnoredMembers(), false, context.CancellationToken, locationOfFirstDeclaration, context).Enumerate();
572+
baseClass.IterateAssignableFieldsAndPropertiesAndCheckForReadOnly(false, context.CancellationToken, locationOfFirstDeclaration, context).Enumerate();
576573

577574
baseClass = baseClass.BaseType;
578575
}
@@ -581,14 +578,13 @@ private static void ValidateEnum(
581578

582579
EnumKeyMemberNameMustNotBeItem(context, attribute, locationOfFirstDeclaration);
583580

584-
ValidateKeyedSmartEnum(context, enumType, attribute, nonIgnoredMembers, locationOfFirstDeclaration);
581+
ValidateKeyedSmartEnum(context, enumType, attribute, locationOfFirstDeclaration);
585582
}
586583

587584
private static void ValidateKeyedSmartEnum(
588585
OperationAnalysisContext context,
589586
INamedTypeSymbol enumType,
590587
IObjectCreationOperation attribute,
591-
ImmutableArray<ISymbol> nonIgnoredMembers,
592588
Location locationOfFirstDeclaration)
593589
{
594590
var keyType = (attribute.Type as INamedTypeSymbol)?.TypeArguments.FirstOrDefault();
@@ -609,7 +605,7 @@ private static void ValidateKeyedSmartEnum(
609605

610606
if (isValidatable)
611607
{
612-
ValidateCreateInvalidItem(context, enumType, keyType, nonIgnoredMembers, locationOfFirstDeclaration);
608+
ValidateCreateInvalidItem(context, enumType, keyType, locationOfFirstDeclaration);
613609
}
614610
else if (enumType.IsValueType)
615611
{
@@ -627,14 +623,15 @@ private static void TypeMustNotBeGeneric(OperationAnalysisContext context, IName
627623

628624
private static void Check_ItemLike_StaticProperties(
629625
OperationAnalysisContext context,
630-
INamedTypeSymbol enumType,
631-
ImmutableArray<ISymbol> nonIgnoredMembers)
626+
INamedTypeSymbol enumType)
632627
{
633-
for (var i = 0; i < nonIgnoredMembers.Length; i++)
628+
var members = enumType.GetMembers();
629+
630+
for (var i = 0; i < members.Length; i++)
634631
{
635-
var member = nonIgnoredMembers[i];
632+
var member = members[i];
636633

637-
if (member.IsStatic && member is IPropertySymbol property && SymbolEqualityComparer.Default.Equals(property.Type, enumType))
634+
if (member.IsStatic && member is IPropertySymbol property && SymbolEqualityComparer.Default.Equals(property.Type, enumType) && !property.IsIgnored())
638635
ReportDiagnostic(context, DiagnosticsDescriptors.StaticPropertiesAreNotConsideredItems, property.GetIdentifier(context.CancellationToken)?.GetLocation() ?? Location.None, property.Name);
639636
}
640637
}
@@ -680,10 +677,9 @@ private static void ValidateCreateInvalidItem(
680677
OperationAnalysisContext context,
681678
INamedTypeSymbol enumType,
682679
ITypeSymbol keyType,
683-
ImmutableArray<ISymbol> nonIgnoredMembers,
684680
Location location)
685681
{
686-
var hasCreateInvalidItemImplementation = enumType.HasCreateInvalidItemImplementation(nonIgnoredMembers, keyType, context.CancellationToken, context);
682+
var hasCreateInvalidItemImplementation = enumType.HasCreateInvalidItemImplementation(keyType, context.CancellationToken, context);
687683

688684
if (!hasCreateInvalidItemImplementation && enumType.IsAbstract)
689685
{

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/EnumSourceGeneratorState.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public EnumSourceGeneratorState(
3030
INamedTypeSymbol type,
3131
KeyMemberState? keyMember,
3232
ValidationErrorState validationError,
33-
ImmutableArray<ISymbol> nonIgnoredMembers,
3433
EnumSettings settings,
3534
bool hasCreateInvalidItemImplementation,
3635
bool hasDerivedTypes,
@@ -53,8 +52,8 @@ public EnumSourceGeneratorState(
5352
IsAbstract = type.IsAbstract;
5453

5554
BaseType = type.GetBaseType(factory);
56-
Items = new EnumItems(type.GetEnumItems(nonIgnoredMembers));
57-
AssignableInstanceFieldsAndProperties = type.GetAssignableFieldsAndPropertiesAndCheckForReadOnly(nonIgnoredMembers, factory, true, false, cancellationToken).ToList();
55+
Items = new EnumItems(type.GetEnumItems());
56+
AssignableInstanceFieldsAndProperties = type.GetAssignableFieldsAndPropertiesAndCheckForReadOnly(factory, true, false, cancellationToken).ToList();
5857
}
5958

6059
public override bool Equals(object? obj)

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/SmartEnums/SmartEnumSourceGenerator.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,9 @@ private bool IsEnumCandidate(TypeDeclarationSyntax typeDeclaration)
350350
return null;
351351
}
352352

353-
if (attributetype.TypeArguments.Length != 1)
353+
if (attributetype.Arity != 1)
354354
{
355-
Logger.LogDebug($"Expected the attribute type to have 1 type argument but found {attributetype.TypeArguments.Length.ToString()}", tds);
355+
Logger.LogDebug($"Expected the attribute type to have 1 type argument but found {attributetype.Arity.ToString()}", tds);
356356
return null;
357357
}
358358

@@ -393,15 +393,13 @@ private bool IsEnumCandidate(TypeDeclarationSyntax typeDeclaration)
393393
keyMember = settings.CreateKeyMember(keyTypedMemberState);
394394
}
395395

396-
var nonIgnoredMembers = type.GetNonIgnoredMembers();
397-
var hasCreateInvalidItemImplementation = keyMemberType is not null && settings.IsValidatable && type.HasCreateInvalidItemImplementation(nonIgnoredMembers, keyMemberType, cancellationToken);
396+
var hasCreateInvalidItemImplementation = keyMemberType is not null && settings.IsValidatable && type.HasCreateInvalidItemImplementation(keyMemberType, cancellationToken);
398397

399398
var derivedTypeNames = FindDerivedTypes(type);
400399
var enumState = new EnumSourceGeneratorState(factory,
401400
type,
402401
keyMember,
403402
attributeInfo.ValidationError,
404-
nonIgnoredMembers,
405403
new EnumSettings(settings, attributeInfo),
406404
hasCreateInvalidItemImplementation,
407405
derivedTypeNames.Count > 0,

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/TypedMemberState.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,13 @@ public TypedMemberState(ITypeSymbol type)
143143

144144
private static bool IsToStringNullable(ITypeSymbol type)
145145
{
146-
var array = type.GetMembers();
146+
var array = type.GetMembers("ToString");
147147

148148
for (var i = 0; i < array.Length; i++)
149149
{
150150
var member = array[i];
151151

152-
if (member is not IMethodSymbol { Name: "ToString" } toString)
152+
if (member is not IMethodSymbol toString)
153153
continue;
154154

155155
return toString.ReturnNullableAnnotation == NullableAnnotation.Annotated;

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/Unions/UnionSourceGenState.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public UnionSourceGenState(
2222
Namespace = type.ContainingNamespace?.IsGlobalNamespace == true ? null : type.ContainingNamespace?.ToString();
2323
HasNonDefaultConstructor = !type.Constructors.IsDefaultOrEmpty && type.Constructors.Any(c => !c.IsImplicitlyDeclared);
2424
ContainingTypes = type.GetContainingTypes();
25-
GenericsFullyQualified = type.TypeArguments.Length == 0
25+
GenericsFullyQualified = type.Arity == 0
2626
? []
2727
: type.TypeArguments.Select(t => t.ToFullyQualifiedDisplayString()).ToList();
2828

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/Unions/UnionSourceGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ private bool IsCandidate(SyntaxNode syntaxNode, CancellationToken cancellationTo
8989
return null;
9090
}
9191

92-
if (attributeType.TypeArguments.Length != 0)
92+
if (attributeType.Arity != 0)
9393
{
94-
Logger.LogDebug($"Expected the union attribute type to have no type arguments but found {attributeType.TypeArguments.Length.ToString()}", tds);
94+
Logger.LogDebug($"Expected the union attribute type to have no type arguments but found {attributeType.Arity.ToString()}", tds);
9595
return null;
9696
}
9797

src/Thinktecture.Runtime.Extensions.SourceGenerator/CodeAnalysis/ValueObjects/ComplexValueObjectSourceGeneratorState.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,10 @@ public ComplexValueObjectSourceGeneratorState(
4040
NullableAnnotation = type.NullableAnnotation;
4141
IsNullableStruct = type.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T;
4242

43-
var nonIgnoredMembers = type.GetNonIgnoredMembers();
44-
AssignableInstanceFieldsAndProperties = type.GetAssignableFieldsAndPropertiesAndCheckForReadOnly(nonIgnoredMembers, factory, true, true, cancellationToken).ToList();
43+
AssignableInstanceFieldsAndProperties = type.GetAssignableFieldsAndPropertiesAndCheckForReadOnly(factory, true, true, cancellationToken).ToList();
4544
EqualityMembers = GetEqualityMembers();
4645

47-
var factoryValidationReturnType = nonIgnoredMembers.IsDefaultOrEmpty
48-
? null
49-
: (nonIgnoredMembers.FirstOrDefault(m => m.IsStatic && m.Name == Constants.Methods.VALIDATE_FACTORY_ARGUMENTS && m is IMethodSymbol method && method.ReturnType.SpecialType != SpecialType.System_Void) as IMethodSymbol)?.ReturnType;
46+
var factoryValidationReturnType = (type.GetMembers().FirstOrDefault(m => m.IsStatic && m.Name == Constants.Methods.VALIDATE_FACTORY_ARGUMENTS && m is IMethodSymbol method && method.ReturnType.SpecialType != SpecialType.System_Void) as IMethodSymbol)?.ReturnType;
5047

5148
if (factoryValidationReturnType is not null)
5249
FactoryValidationReturnType = factoryValidationReturnType.ToFullyQualifiedDisplayString();

0 commit comments

Comments
 (0)