Skip to content

Commit af4f831

Browse files
committed
Use unchecked constructor for SCG.List<T> to bypass redundant bounds checks
1 parent f5e8e9d commit af4f831

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

Ramstack.Structures/Collections/ArrayViewExtensions.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,13 @@ public static ArrayView<T> AsView<T>(this List<T>? list)
149149
if (list is not null)
150150
{
151151
var array = ListAccessor<T>.GetArray(list);
152-
var count = Math.Min(list.Count, array.Length);
152+
_ = array.Length;
153153

154-
return new ArrayView<T>(array, 0, count);
154+
//
155+
// SCG.List<T> maintains internal invariants, so we can safely use the unchecked constructor
156+
// to bypass redundant bounds checks for better performance.
157+
//
158+
return new ArrayView<T>(array, 0, list.Count, unused: 0);
155159
}
156160

157161
return ArrayView<T>.Empty;

Ramstack.Structures/Collections/ArrayView`1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public ArrayView(T[] array, int index, int length)
112112
/// <param name="length">The number of elements in the range.</param>
113113
/// <param name="unused">Unused parameter, exists solely to disambiguate overloads.</param>
114114
[MethodImpl(MethodImplOptions.AggressiveInlining)]
115-
private ArrayView(T[] array, int index, int length, int unused)
115+
internal ArrayView(T[] array, int index, int length, int unused)
116116
{
117117
_index = index;
118118
_count = length;

0 commit comments

Comments
 (0)