From 939962c72191805e4101263c85eb8b6083141ea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?JasonXuDeveloper=20-=20=E5=82=91?= Date: Mon, 3 Feb 2025 12:40:44 +0800 Subject: [PATCH] release v3.1.1 - [fix] fix #139 - [opt] optimised private accessor performance on <= NET6.0 - [opt] attempt to optimize generator performance --- src/Nino.Generator/NinoTypeHelper.cs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/Nino.Generator/NinoTypeHelper.cs b/src/Nino.Generator/NinoTypeHelper.cs index 38434ef..390b38f 100644 --- a/src/Nino.Generator/NinoTypeHelper.cs +++ b/src/Nino.Generator/NinoTypeHelper.cs @@ -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; @@ -463,6 +464,23 @@ private static void AddTypeArguments(INamedTypeSymbol symbol, List private static readonly ConcurrentDictionary> AllTypesCache = new(); + private static readonly ConcurrentDictionary> 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 GetAllTypes(Compilation compilation) { @@ -478,10 +496,9 @@ public static IEnumerable 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)); } }