From f372c2cfda9cf36a5976145a41e4824504b5e274 Mon Sep 17 00:00:00 2001 From: Henrik Gedionsen Date: Mon, 14 Oct 2024 15:55:35 +0200 Subject: [PATCH] Optimize array usage, plus other minor tweaks --- src/Castle.Core/Core/Resource/AssemblyResource.cs | 2 +- src/Castle.Core/Core/Resource/CustomUri.cs | 2 +- .../Core/StringObjectDictionaryAdapter.cs | 2 +- .../Contributors/ClassProxyTargetContributor.cs | 6 +----- .../ClassProxyWithTargetTargetContributor.cs | 7 +------ .../Contributors/CompositeTypeContributor.cs | 13 +++++++++++++ src/Castle.Core/DynamicProxy/CustomAttributeInfo.cs | 6 +++--- .../Generators/InvocationTypeGenerator.cs | 2 +- .../DynamicProxy/Internal/AttributeUtil.cs | 2 +- .../DynamicProxy/ProxyGenerationOptions.cs | 2 +- src/Castle.Core/DynamicProxy/ProxyGenerator.cs | 12 ++++++------ 11 files changed, 30 insertions(+), 26 deletions(-) diff --git a/src/Castle.Core/Core/Resource/AssemblyResource.cs b/src/Castle.Core/Core/Resource/AssemblyResource.cs index 08882f2f34..960fa47451 100644 --- a/src/Castle.Core/Core/Resource/AssemblyResource.cs +++ b/src/Castle.Core/Core/Resource/AssemblyResource.cs @@ -104,7 +104,7 @@ private string GetNameFound(string[] names) string nameFound = null; foreach(string name in names) { - if (string.Compare(resourcePath, name, StringComparison.OrdinalIgnoreCase) == 0) + if (string.Equals(resourcePath, name, StringComparison.OrdinalIgnoreCase)) { nameFound = name; break; diff --git a/src/Castle.Core/Core/Resource/CustomUri.cs b/src/Castle.Core/Core/Resource/CustomUri.cs index fe19ab1303..de9b017d4a 100644 --- a/src/Castle.Core/Core/Resource/CustomUri.cs +++ b/src/Castle.Core/Core/Resource/CustomUri.cs @@ -39,7 +39,7 @@ public CustomUri(string resourceIdentifier) { throw new ArgumentNullException(nameof(resourceIdentifier)); } - if (resourceIdentifier == string.Empty) + if (resourceIdentifier.Length == 0) { throw new ArgumentException("Empty resource identifier is not allowed", nameof(resourceIdentifier)); } diff --git a/src/Castle.Core/Core/StringObjectDictionaryAdapter.cs b/src/Castle.Core/Core/StringObjectDictionaryAdapter.cs index 9cedaff87c..23c4632cd4 100644 --- a/src/Castle.Core/Core/StringObjectDictionaryAdapter.cs +++ b/src/Castle.Core/Core/StringObjectDictionaryAdapter.cs @@ -181,7 +181,7 @@ public IEnumerator GetEnumerator() internal class EnumeratorAdapter : IEnumerator> { private readonly StringObjectDictionaryAdapter adapter; - private IEnumerator keyEnumerator; + private readonly IEnumerator keyEnumerator; private string currentKey; private object currentValue; diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs index fb376b025e..e60b4d08d2 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs @@ -16,8 +16,6 @@ namespace Castle.DynamicProxy.Contributors { using System; using System.Collections.Generic; - using System.Diagnostics; - using System.Linq; using System.Reflection; using System.Reflection.Emit; @@ -169,9 +167,7 @@ private Type GetDelegateType(MetaMethod method, ClassEmitter @class) var key = new CacheKey( typeof(Delegate), targetType, - new[] { method.MethodOnTarget.ReturnType } - .Concat(ArgumentsUtil.GetTypes(method.MethodOnTarget.GetParameters())). - ToArray(), + GetCacheKeyTypes(method), null); return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ => diff --git a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs index e9ab21fc32..d834373c3b 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs @@ -16,9 +16,6 @@ namespace Castle.DynamicProxy.Contributors { using System; using System.Collections.Generic; - using System.Diagnostics; - using System.Linq; - using System.Reflection; using Castle.DynamicProxy.Generators; using Castle.DynamicProxy.Generators.Emitters; @@ -121,9 +118,7 @@ private Type GetDelegateType(MetaMethod method, ClassEmitter @class) var key = new CacheKey( typeof(Delegate), targetType, - new[] { method.MethodOnTarget.ReturnType } - .Concat(ArgumentsUtil.GetTypes(method.MethodOnTarget.GetParameters())). - ToArray(), + GetCacheKeyTypes(method), null); return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ => diff --git a/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs b/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs index fc34dffbd1..6a165d0d9c 100644 --- a/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs +++ b/src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs @@ -137,6 +137,19 @@ private void ImplementMethod(MetaMethod method, ClassEmitter @class, } } + protected static Type[] GetCacheKeyTypes(MetaMethod method) + { + Type[] argumentTypes = ArgumentsUtil.GetTypes(method.MethodOnTarget.GetParameters()); + if (argumentTypes.Length < 1) + { + return new[] { method.MethodOnTarget.ReturnType }; + } + var types = new Type[argumentTypes.Length + 1]; + types[0] = method.MethodOnTarget.ReturnType; + argumentTypes.CopyTo(types, 1); + return types; + } + private sealed class MembersCollectorSink : IMembersCollectorSink { private readonly MetaType model; diff --git a/src/Castle.Core/DynamicProxy/CustomAttributeInfo.cs b/src/Castle.Core/DynamicProxy/CustomAttributeInfo.cs index 68ac6c765e..bbb4b41a59 100644 --- a/src/Castle.Core/DynamicProxy/CustomAttributeInfo.cs +++ b/src/Castle.Core/DynamicProxy/CustomAttributeInfo.cs @@ -34,9 +34,9 @@ namespace Castle.DynamicProxy public class CustomAttributeInfo : IEquatable { // Cached empty arrays to avoid unnecessary allocations - private static readonly PropertyInfo[] EmptyProperties = new PropertyInfo[0]; - private static readonly FieldInfo[] EmptyFields = new FieldInfo[0]; - private static readonly object?[] EmptyValues = new object?[0]; + private static readonly PropertyInfo[] EmptyProperties = Array.Empty(); + private static readonly FieldInfo[] EmptyFields = Array.Empty(); + private static readonly object?[] EmptyValues = Array.Empty(); private static readonly AttributeArgumentValueEqualityComparer ValueComparer = new AttributeArgumentValueEqualityComparer(); diff --git a/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs b/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs index c4e022de3a..15b6584a3a 100644 --- a/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs +++ b/src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs @@ -57,7 +57,7 @@ public AbstractTypeEmitter Generate(ClassEmitter @class, INamingScope namingScop { var methodInfo = method.Method; - var interfaces = new Type[0]; + var interfaces = Type.EmptyTypes; if (canChangeTarget) { diff --git a/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs b/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs index ee7e129cb0..a31c6b81ad 100644 --- a/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs +++ b/src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs @@ -205,7 +205,7 @@ private static bool ShouldSkipAttributeReplication(Type attribute, bool ignoreIn var constructor = typeof(TAttribute).GetConstructor(Type.EmptyTypes); Debug.Assert(constructor != null, "constructor != null"); - return new CustomAttributeInfo(constructor, new object[0]); + return new CustomAttributeInfo(constructor, Array.Empty()); } public static CustomAttributeInfo CreateInfo(Type attribute, object[] constructorArguments) diff --git a/src/Castle.Core/DynamicProxy/ProxyGenerationOptions.cs b/src/Castle.Core/DynamicProxy/ProxyGenerationOptions.cs index 2dbe74aaba..2574203c93 100644 --- a/src/Castle.Core/DynamicProxy/ProxyGenerationOptions.cs +++ b/src/Castle.Core/DynamicProxy/ProxyGenerationOptions.cs @@ -238,7 +238,7 @@ public object[] MixinsAsArray() { if (mixins == null) { - return new object[0]; + return Array.Empty(); } return mixins.ToArray(); diff --git a/src/Castle.Core/DynamicProxy/ProxyGenerator.cs b/src/Castle.Core/DynamicProxy/ProxyGenerator.cs index 2074dce620..7389bed28f 100644 --- a/src/Castle.Core/DynamicProxy/ProxyGenerator.cs +++ b/src/Castle.Core/DynamicProxy/ProxyGenerator.cs @@ -858,7 +858,7 @@ public TClass CreateClassProxyWithTarget(TClass? target, params IInterce Type.EmptyTypes, target, ProxyGenerationOptions.Default, - new object[0], + Array.Empty(), interceptors); } @@ -888,7 +888,7 @@ public TClass CreateClassProxyWithTarget(TClass? target, ProxyGeneration Type.EmptyTypes, target, options, - new object[0], + Array.Empty(), interceptors); } @@ -921,7 +921,7 @@ public object CreateClassProxyWithTarget(Type classToProxy, Type[]? additionalIn additionalInterfacesToProxy, target, ProxyGenerationOptions.Default, - new object[0], + Array.Empty(), interceptors); } @@ -1019,7 +1019,7 @@ public object CreateClassProxyWithTarget(Type classToProxy, object? target, para Type.EmptyTypes, target, ProxyGenerationOptions.Default, - new object[0], + Array.Empty(), interceptors); } @@ -1052,7 +1052,7 @@ public object CreateClassProxyWithTarget(Type classToProxy, object? target, Prox Type.EmptyTypes, target, options, - new object[0], + Array.Empty(), interceptors); } @@ -1087,7 +1087,7 @@ public object CreateClassProxyWithTarget(Type classToProxy, Type[]? additionalIn additionalInterfacesToProxy, target, options, - new object[0], + Array.Empty(), interceptors); }