Skip to content

Commit 5262a45

Browse files
committed
⚡️ Optimize NotNull array extension
1 parent 664d643 commit 5262a45

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

Chasm.Collections/ArrayExtensions.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -514,12 +514,15 @@ [Pure] public static ReadOnlyCollection<T> AsReadOnly<T>(this T[] array)
514514
{
515515
if (array is null) throw new ArgumentNullException(nameof(array));
516516
if (default(T) is not null) return array!;
517-
return Array.FindAll(array, Typed<T>.NotNullPredicate)!;
518-
}
519517

520-
private static class Typed<T>
521-
{
522-
public static readonly Predicate<T?> NotNullPredicate = static v => v is not null;
518+
List<T> results = [];
519+
for (int i = 0; i < array.Length; i++)
520+
{
521+
T? item = array[i];
522+
if (item is not null)
523+
results.Add(item);
524+
}
525+
return results.ToArray();
523526
}
524527

525528
}

Chasm.Collections/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Chasm.Collections Changelog
22

33
### v2.4.2 (next)
4+
- ⚡️ Improved the performance of `NotNull` array extension by inlining the `Array.FindAll` call;
45
- ⚡️ Microoptimized IL code size of `CollectionExtensions.Add` extensions;
56

67
### v2.4.1

0 commit comments

Comments
 (0)