Skip to content

Commit

Permalink
release v3.1.1
Browse files Browse the repository at this point in the history
- [fix] fix #139
- [opt] optimised private accessor performance on <= NET6.0
- [opt] attempt to optimize generator performance
  • Loading branch information
JasonXuDeveloper committed Feb 3, 2025
1 parent ccd0565 commit 939962c
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/Nino.Generator/NinoTypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
Expand Down Expand Up @@ -463,6 +464,23 @@ private static void AddTypeArguments(INamedTypeSymbol symbol, List<ITypeSymbol>


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

[SuppressMessage("MicrosoftCodeAnalysisCorrectness", "RS1024:Symbols should be compared for equality")]
private static INamedTypeSymbol[] GetTypesInAssembly(IAssemblySymbol assembly)
{
var hash = string.IsNullOrEmpty(assembly.Name)
? assembly.Name.GetLegacyNonRandomizedHashCode()
: assembly.GetHashCode();
if (AllAssembliesTypesCache.TryGetValue(hash, out var types))
//return a copy
return types.ToArray();

var allTypes = GetTypesInNamespace(assembly.GlobalNamespace).ToList();
AllAssembliesTypesCache[hash] = allTypes;
//return a copy
return allTypes.ToArray();
}

public static IEnumerable<INamedTypeSymbol> GetAllTypes(Compilation compilation)
{
Expand All @@ -478,10 +496,9 @@ public static IEnumerable<INamedTypeSymbol> GetAllTypes(Compilation compilation)
// Add all types from each referenced assembly
foreach (var referencedAssembly in compilation.References)
{
var assemblySymbol = compilation.GetAssemblyOrModuleSymbol(referencedAssembly) as IAssemblySymbol;
if (assemblySymbol != null)
if (compilation.GetAssemblyOrModuleSymbol(referencedAssembly) is IAssemblySymbol assemblySymbol)
{
allTypes.AddRange(GetTypesInNamespace(assemblySymbol.GlobalNamespace));
allTypes.AddRange(GetTypesInAssembly(assemblySymbol));
}
}

Expand Down

0 comments on commit 939962c

Please sign in to comment.