From 726f783883f22c2ebf97b79d01b045471a3fbe6c Mon Sep 17 00:00:00 2001 From: Nick Craver Date: Mon, 10 Dec 2018 06:01:52 -0500 Subject: [PATCH] Fix for dotnet/coreclr#21456 (Regressions in attribute allocations for non-generic attributes) (dotnet/coreclr#21462) * Fix for dotnet/coreclr#21456 - restrict increased generic attribute allocations to only generic attributes This is a trivial quick-fix for dotnet/coreclr#21456 where regressions between 2.1 and 3.0 were discovered on most attibute pathways due to the allocation overhead in the generic-supporting pathways. The workaround is to simply not take that slow/expensive path for non-generics. While I'd like to optimize `RuntimeModule.ResolveMethod` further, there's a public surface area in play there that makes the changes non-trivial. There, we'll have to choose overhead on the public path (which may still be a net win), or duplication in code for another path. * Update comments Commit migrated from https://github.com/dotnet/coreclr/commit/130cec3b15815ae023a7aac9dc96aa97b36117a0 --- .../src/System/Reflection/CustomAttribute.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs index 50015dd..0ebedf0 100644 --- a/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs +++ b/src/coreclr/src/System.Private.CoreLib/src/System/Reflection/CustomAttribute.cs @@ -1616,7 +1616,15 @@ namespace System.Reflection if (ctorHasParameters) { // Resolve method ctor token found in decorated decoratedModule scope - ctor = decoratedModule.ResolveMethod(caRecord.tkCtor, attributeType.GenericTypeArguments, null).MethodHandle.GetMethodInfo(); + // See https://github.com/dotnet/coreclr/issues/21456 for why we fast-path non-generics here (fewer allocations) + if (attributeType.IsGenericType) + { + ctor = decoratedModule.ResolveMethod(caRecord.tkCtor, attributeType.GenericTypeArguments, null).MethodHandle.GetMethodInfo(); + } + else + { + ctor = ModuleHandle.ResolveMethodHandleInternal(decoratedModule.GetNativeHandle(), caRecord.tkCtor); + } } else { -- 2.7.4