Skip to content

Commit f372c2c

Browse files
Henr1k80stakx
authored andcommitted
Optimize array usage, plus other minor tweaks
1 parent 17fd99c commit f372c2c

11 files changed

+30
-26
lines changed

src/Castle.Core/Core/Resource/AssemblyResource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ private string GetNameFound(string[] names)
104104
string nameFound = null;
105105
foreach(string name in names)
106106
{
107-
if (string.Compare(resourcePath, name, StringComparison.OrdinalIgnoreCase) == 0)
107+
if (string.Equals(resourcePath, name, StringComparison.OrdinalIgnoreCase))
108108
{
109109
nameFound = name;
110110
break;

src/Castle.Core/Core/Resource/CustomUri.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public CustomUri(string resourceIdentifier)
3939
{
4040
throw new ArgumentNullException(nameof(resourceIdentifier));
4141
}
42-
if (resourceIdentifier == string.Empty)
42+
if (resourceIdentifier.Length == 0)
4343
{
4444
throw new ArgumentException("Empty resource identifier is not allowed", nameof(resourceIdentifier));
4545
}

src/Castle.Core/Core/StringObjectDictionaryAdapter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ public IEnumerator GetEnumerator()
181181
internal class EnumeratorAdapter : IEnumerator<KeyValuePair<string, object>>
182182
{
183183
private readonly StringObjectDictionaryAdapter adapter;
184-
private IEnumerator<string> keyEnumerator;
184+
private readonly IEnumerator<string> keyEnumerator;
185185
private string currentKey;
186186
private object currentValue;
187187

src/Castle.Core/DynamicProxy/Contributors/ClassProxyTargetContributor.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ namespace Castle.DynamicProxy.Contributors
1616
{
1717
using System;
1818
using System.Collections.Generic;
19-
using System.Diagnostics;
20-
using System.Linq;
2119
using System.Reflection;
2220
using System.Reflection.Emit;
2321

@@ -169,9 +167,7 @@ private Type GetDelegateType(MetaMethod method, ClassEmitter @class)
169167
var key = new CacheKey(
170168
typeof(Delegate),
171169
targetType,
172-
new[] { method.MethodOnTarget.ReturnType }
173-
.Concat(ArgumentsUtil.GetTypes(method.MethodOnTarget.GetParameters())).
174-
ToArray(),
170+
GetCacheKeyTypes(method),
175171
null);
176172

177173
return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ =>

src/Castle.Core/DynamicProxy/Contributors/ClassProxyWithTargetTargetContributor.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ namespace Castle.DynamicProxy.Contributors
1616
{
1717
using System;
1818
using System.Collections.Generic;
19-
using System.Diagnostics;
20-
using System.Linq;
21-
using System.Reflection;
2219

2320
using Castle.DynamicProxy.Generators;
2421
using Castle.DynamicProxy.Generators.Emitters;
@@ -121,9 +118,7 @@ private Type GetDelegateType(MetaMethod method, ClassEmitter @class)
121118
var key = new CacheKey(
122119
typeof(Delegate),
123120
targetType,
124-
new[] { method.MethodOnTarget.ReturnType }
125-
.Concat(ArgumentsUtil.GetTypes(method.MethodOnTarget.GetParameters())).
126-
ToArray(),
121+
GetCacheKeyTypes(method),
127122
null);
128123

129124
return scope.TypeCache.GetOrAddWithoutTakingLock(key, _ =>

src/Castle.Core/DynamicProxy/Contributors/CompositeTypeContributor.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ private void ImplementMethod(MetaMethod method, ClassEmitter @class,
137137
}
138138
}
139139

140+
protected static Type[] GetCacheKeyTypes(MetaMethod method)
141+
{
142+
Type[] argumentTypes = ArgumentsUtil.GetTypes(method.MethodOnTarget.GetParameters());
143+
if (argumentTypes.Length < 1)
144+
{
145+
return new[] { method.MethodOnTarget.ReturnType };
146+
}
147+
var types = new Type[argumentTypes.Length + 1];
148+
types[0] = method.MethodOnTarget.ReturnType;
149+
argumentTypes.CopyTo(types, 1);
150+
return types;
151+
}
152+
140153
private sealed class MembersCollectorSink : IMembersCollectorSink
141154
{
142155
private readonly MetaType model;

src/Castle.Core/DynamicProxy/CustomAttributeInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ namespace Castle.DynamicProxy
3434
public class CustomAttributeInfo : IEquatable<CustomAttributeInfo>
3535
{
3636
// Cached empty arrays to avoid unnecessary allocations
37-
private static readonly PropertyInfo[] EmptyProperties = new PropertyInfo[0];
38-
private static readonly FieldInfo[] EmptyFields = new FieldInfo[0];
39-
private static readonly object?[] EmptyValues = new object?[0];
37+
private static readonly PropertyInfo[] EmptyProperties = Array.Empty<PropertyInfo>();
38+
private static readonly FieldInfo[] EmptyFields = Array.Empty<FieldInfo>();
39+
private static readonly object?[] EmptyValues = Array.Empty<object?>();
4040

4141
private static readonly AttributeArgumentValueEqualityComparer ValueComparer = new AttributeArgumentValueEqualityComparer();
4242

src/Castle.Core/DynamicProxy/Generators/InvocationTypeGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public AbstractTypeEmitter Generate(ClassEmitter @class, INamingScope namingScop
5757
{
5858
var methodInfo = method.Method;
5959

60-
var interfaces = new Type[0];
60+
var interfaces = Type.EmptyTypes;
6161

6262
if (canChangeTarget)
6363
{

src/Castle.Core/DynamicProxy/Internal/AttributeUtil.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private static bool ShouldSkipAttributeReplication(Type attribute, bool ignoreIn
205205
var constructor = typeof(TAttribute).GetConstructor(Type.EmptyTypes);
206206
Debug.Assert(constructor != null, "constructor != null");
207207

208-
return new CustomAttributeInfo(constructor, new object[0]);
208+
return new CustomAttributeInfo(constructor, Array.Empty<object>());
209209
}
210210

211211
public static CustomAttributeInfo CreateInfo(Type attribute, object[] constructorArguments)

src/Castle.Core/DynamicProxy/ProxyGenerationOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ public object[] MixinsAsArray()
238238
{
239239
if (mixins == null)
240240
{
241-
return new object[0];
241+
return Array.Empty<object>();
242242
}
243243

244244
return mixins.ToArray();

0 commit comments

Comments
 (0)