Skip to content

Commit

Permalink
release v3.0.9
Browse files Browse the repository at this point in the history
- [opt] optimised code generation for user-defined dictionary types
- [opt] optimised source generator performance by caching collected types (**experimental**)
  • Loading branch information
JasonXuDeveloper committed Jan 19, 2025
1 parent fec74db commit 3b4b937
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions src/Nino.Generator/NinoTypeHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
Expand Down Expand Up @@ -29,7 +30,7 @@ public static List<ITypeSymbol> GetPotentialCollectionTypes(this ImmutableArray<

return null;
})
.Concat(NinoTypeHelper.GetAllNinoRequiredTypes(compilation))
.Concat(GetAllNinoRequiredTypes(compilation))
.Where(s => s != null)
.Select(s => s!)
.Distinct(SymbolEqualityComparer.Default)
Expand All @@ -43,7 +44,8 @@ public static List<ITypeSymbol> GetPotentialCollectionTypes(this ImmutableArray<
namedTypeSymbol.Name == "ICollection" && namedTypeSymbol.TypeArguments.Length == 1);
var cond = forDeserialization
? i != null //when deserializing, we want ICollection and dont care other things
: i != null && !i.IsUnmanagedType && // when serializing, we want ICollection of what we want, i.e. not unmanaged, otherwise we have a default fallback
: i != null &&
!i.IsUnmanagedType && // when serializing, we want ICollection of what we want, i.e. not unmanaged, otherwise we have a default fallback
i.TypeArguments.All(t => t.IsSerializableType());
if (cond)
{
Expand Down Expand Up @@ -103,21 +105,21 @@ bool IsTypeParameter(ITypeSymbol typeSymbol)
.OfType<IPropertySymbol>()
.Where(p => p.IsIndexer)
.ToList();

//ensure there exists one public indexer that returns vType and takes only kType
var validIndexers = indexers.Where(p =>
p.Type.Equals(vType, SymbolEqualityComparer.Default) && p.Parameters.Length == 1 &&
p.Parameters[0].Type.Equals(kType, SymbolEqualityComparer.Default))
p.Type.Equals(vType, SymbolEqualityComparer.Default) && p.Parameters.Length == 1 &&
p.Parameters[0].Type.Equals(kType, SymbolEqualityComparer.Default))
.ToList();
if (!validIndexers.Any()) return false;

//ensure the valid indexer has public getter and setter
if (validIndexers.Any(p => p.GetMethod?.DeclaredAccessibility == Accessibility.Public &&
p.SetMethod?.DeclaredAccessibility == Accessibility.Public))
{
return true;
}

return false;
}

Expand Down Expand Up @@ -370,8 +372,14 @@ bool IsContainingTypeValid(ITypeSymbol type)
.ToList();
}

private static readonly ConcurrentDictionary<int, List<ITypeSymbol>> AllNinoRequiredTypesCache = new();

public static IEnumerable<ITypeSymbol> GetAllNinoRequiredTypes(Compilation compilation)
{
if (AllNinoRequiredTypesCache.TryGetValue(compilation.GetHashCode(), out var types))
//return a copy
return types.ToArray();

var lst = GetAllTypes(compilation)
.Where(s => s != null)
.Distinct(SymbolEqualityComparer.Default)
Expand Down Expand Up @@ -416,11 +424,14 @@ public static IEnumerable<ITypeSymbol> GetAllNinoRequiredTypes(Compilation compi
{
AddElementRecursively(typeSymbol, ret);
}

return ret.Distinct(SymbolEqualityComparer.Default)
AllNinoRequiredTypesCache[compilation.GetHashCode()] = ret.Distinct(SymbolEqualityComparer.Default)
.Where(s => s != null)
.Select(s => (ITypeSymbol)s!)
.ToList();

//return a copy
return AllNinoRequiredTypesCache[compilation.GetHashCode()].ToArray();
}

private static void AddElementRecursively(ITypeSymbol symbol, List<ITypeSymbol> ret)
Expand Down Expand Up @@ -450,8 +461,15 @@ private static void AddTypeArguments(INamedTypeSymbol symbol, List<ITypeSymbol>
}
}


private static readonly ConcurrentDictionary<int, List<INamedTypeSymbol>> AllTypesCache = new();

public static IEnumerable<INamedTypeSymbol> GetAllTypes(Compilation compilation)
{
if (AllTypesCache.TryGetValue(compilation.GetHashCode(), out var types))
//return a copy
return types.ToArray();

var allTypes = new List<INamedTypeSymbol>();

// Add all types from the current assembly (compilation)
Expand All @@ -467,13 +485,15 @@ public static IEnumerable<INamedTypeSymbol> GetAllTypes(Compilation compilation)
}
}


//distinct
return allTypes
AllTypesCache[compilation.GetHashCode()] = allTypes
.Distinct(SymbolEqualityComparer.Default)
.Where(s => s != null)
.Select(s => (INamedTypeSymbol)s!)
.ToList();

//return a copy
return AllTypesCache[compilation.GetHashCode()].ToArray();
}

private static IEnumerable<INamedTypeSymbol> GetTypesInNamespace(INamespaceSymbol namespaceSymbol)
Expand Down Expand Up @@ -583,7 +603,7 @@ public static string GetTypeModifiers(this ITypeSymbol typeSymbol)

return typeKind;
}

public static bool IsPolymorphicType(this ITypeSymbol typeDecl)
{
var baseType = typeDecl.BaseType;
Expand Down

0 comments on commit 3b4b937

Please sign in to comment.