From 136e7e60bf06722797b7204276af7936bbda3925 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Mon, 18 Aug 2025 14:09:09 -0700 Subject: [PATCH] [NativeAOT] Streamline GetAssemblyCount Avoid unnecessary code and allocations --- .../src/Internal/Reflection/Core/AssemblyBinder.cs | 1 + .../src/System/Reflection/Assembly.NativeAot.cs | 6 +----- .../Reflection/Execution/AssemblyBinderImplementation.cs | 5 +++++ 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/AssemblyBinder.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/AssemblyBinder.cs index cee8abb23ab324..b799c50aeac5f2 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/AssemblyBinder.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Reflection/Core/AssemblyBinder.cs @@ -33,5 +33,6 @@ public abstract class AssemblyBinder public abstract bool Bind(RuntimeAssemblyName refName, bool cacheMissedLookups, out AssemblyBindResult result, out Exception exception); public abstract IList GetLoadedAssemblies(); + public abstract int GetLoadedAssembliesCount(); } } diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Assembly.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Assembly.NativeAot.cs index 2e0ba72727f33d..86d20bf94b2edb 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Assembly.NativeAot.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Reflection/Assembly.NativeAot.cs @@ -35,13 +35,9 @@ public static Assembly Load(string assemblyString) } // Performance metric to count the number of assemblies - // Caching since in NativeAOT, the number will be the same - private static uint s_assemblyCount; internal static uint GetAssemblyCount() { - if (s_assemblyCount == 0) - s_assemblyCount = (uint)Internal.Reflection.Core.Execution.ReflectionCoreExecution.ExecutionEnvironment.AssemblyBinder.GetLoadedAssemblies().Count; - return s_assemblyCount; + return (uint)Internal.Reflection.Core.Execution.ReflectionCoreExecution.ExecutionEnvironment.AssemblyBinder.GetLoadedAssembliesCount(); } [Obsolete("Assembly.LoadWithPartialName has been deprecated. Use Assembly.Load() instead.")] diff --git a/src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Reflection/Execution/AssemblyBinderImplementation.cs b/src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Reflection/Execution/AssemblyBinderImplementation.cs index fba014a2415992..43bda9d00fb01a 100644 --- a/src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Reflection/Execution/AssemblyBinderImplementation.cs +++ b/src/coreclr/nativeaot/System.Private.TypeLoader/src/Internal/Reflection/Execution/AssemblyBinderImplementation.cs @@ -94,6 +94,11 @@ public sealed override IList GetLoadedAssemblies() return loadedAssemblies; } + public sealed override int GetLoadedAssembliesCount() + { + return ScopeGroups.Length; + } + // // Encapsulates the assembly ref->def matching policy. //