Skip to content

Commit 56e8207

Browse files
committed
Switching from IEnumerable.None to IList.None for more performant implementation
1 parent 82d9851 commit 56e8207

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

AgileMapper/Extensions/EnumerableExtensions.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,15 @@ public static T First<T>(this IList<T> items, Func<T, bool> predicate)
4343
[DebuggerStepThrough]
4444
public static bool None<T>(this ICollection<T> items) => items.Count == 0;
4545

46-
public static bool None<T>(this IEnumerable<T> items, Func<T, bool> predicate)
46+
public static bool None<T>(this IList<T> items, Func<T, bool> predicate)
4747
{
48-
using (var enumerator = items.GetEnumerator())
48+
for (int i = 0, n = items.Count; i < n; i++)
4949
{
50-
while (enumerator.MoveNext())
50+
var item = items[i];
51+
52+
if (predicate.Invoke(item))
5153
{
52-
if (predicate.Invoke(enumerator.Current))
53-
{
54-
return false;
55-
}
54+
return false;
5655
}
5756
}
5857

@@ -165,13 +164,26 @@ public static T[] Prepend<T>(this T[] array, T initialItem)
165164

166165
public static T[] Append<T>(this T[] array, T extraItem)
167166
{
168-
var newArray = new T[array.Length + 1];
167+
switch (array.Length)
168+
{
169+
case 0:
170+
return new[] { extraItem };
169171

170-
array.CopyTo(newArray, 0);
172+
case 1:
173+
return new[] { array[0], extraItem };
171174

172-
newArray[array.Length] = extraItem;
175+
case 2:
176+
return new[] { array[0], array[1], extraItem };
173177

174-
return newArray;
178+
default:
179+
var newArray = new T[array.Length + 1];
180+
181+
array.CopyTo(newArray, 0);
182+
183+
newArray[array.Length] = extraItem;
184+
185+
return newArray;
186+
}
175187
}
176188

177189
public static IEnumerable<T> Exclude<T>(this IEnumerable<T> items, IEnumerable<T> excludedItems)

AgileMapper/ObjectPopulation/DerivedComplexTypeMappingsFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ private static Expression GetMapFromConditionOrDefaultExpression(
283283
return ifSourceVariableIsDerivedTypeThenMap;
284284
}
285285

286-
private static ICollection<DerivedTypePair> GetTypePairsFor(
286+
private static IList<DerivedTypePair> GetTypePairsFor(
287287
Type derivedSourceType,
288288
Type targetType,
289289
IMemberMapperData mapperData)
@@ -386,7 +386,7 @@ public TypePairGroup(IGrouping<Type, DerivedTypePair> typePairGroup)
386386

387387
public Type DerivedTargetType { get; }
388388

389-
public ICollection<DerivedTypePair> TypePairs { get; }
389+
public IList<DerivedTypePair> TypePairs { get; }
390390
}
391391
}
392392

0 commit comments

Comments
 (0)