Skip to content

Commit

Permalink
Optimize array usage, plus other minor tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Henr1k80 authored and stakx committed Oct 15, 2024
1 parent 17fd99c commit f372c2c
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/Castle.Core/Core/Resource/AssemblyResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Castle.Core/Core/Resource/CustomUri.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Castle.Core/Core/StringObjectDictionaryAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public IEnumerator GetEnumerator()
internal class EnumeratorAdapter : IEnumerator<KeyValuePair<string, object>>
{
private readonly StringObjectDictionaryAdapter adapter;
private IEnumerator<string> keyEnumerator;
private readonly IEnumerator<string> keyEnumerator;
private string currentKey;
private object currentValue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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, _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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, _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Castle.Core/DynamicProxy/CustomAttributeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ namespace Castle.DynamicProxy
public class CustomAttributeInfo : IEquatable<CustomAttributeInfo>
{
// 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<PropertyInfo>();
private static readonly FieldInfo[] EmptyFields = Array.Empty<FieldInfo>();
private static readonly object?[] EmptyValues = Array.Empty<object?>();

private static readonly AttributeArgumentValueEqualityComparer ValueComparer = new AttributeArgumentValueEqualityComparer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<object>());
}

public static CustomAttributeInfo CreateInfo(Type attribute, object[] constructorArguments)
Expand Down
2 changes: 1 addition & 1 deletion src/Castle.Core/DynamicProxy/ProxyGenerationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ public object[] MixinsAsArray()
{
if (mixins == null)
{
return new object[0];
return Array.Empty<object>();
}

return mixins.ToArray();
Expand Down
12 changes: 6 additions & 6 deletions src/Castle.Core/DynamicProxy/ProxyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ public TClass CreateClassProxyWithTarget<TClass>(TClass? target, params IInterce
Type.EmptyTypes,
target,
ProxyGenerationOptions.Default,
new object[0],
Array.Empty<object>(),
interceptors);
}

Expand Down Expand Up @@ -888,7 +888,7 @@ public TClass CreateClassProxyWithTarget<TClass>(TClass? target, ProxyGeneration
Type.EmptyTypes,
target,
options,
new object[0],
Array.Empty<object>(),
interceptors);
}

Expand Down Expand Up @@ -921,7 +921,7 @@ public object CreateClassProxyWithTarget(Type classToProxy, Type[]? additionalIn
additionalInterfacesToProxy,
target,
ProxyGenerationOptions.Default,
new object[0],
Array.Empty<object>(),
interceptors);
}

Expand Down Expand Up @@ -1019,7 +1019,7 @@ public object CreateClassProxyWithTarget(Type classToProxy, object? target, para
Type.EmptyTypes,
target,
ProxyGenerationOptions.Default,
new object[0],
Array.Empty<object>(),
interceptors);
}

Expand Down Expand Up @@ -1052,7 +1052,7 @@ public object CreateClassProxyWithTarget(Type classToProxy, object? target, Prox
Type.EmptyTypes,
target,
options,
new object[0],
Array.Empty<object>(),
interceptors);
}

Expand Down Expand Up @@ -1087,7 +1087,7 @@ public object CreateClassProxyWithTarget(Type classToProxy, Type[]? additionalIn
additionalInterfacesToProxy,
target,
options,
new object[0],
Array.Empty<object>(),
interceptors);
}

Expand Down

0 comments on commit f372c2c

Please sign in to comment.