Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 49d737d

Browse files
committed
Don't use Array.Reverse in ImmutableArray<T>.Builder.Reverse
The non-generic Array.Reverse does not perform well for non-primitive value types.
1 parent 6983fdc commit 49d737d

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray`1+Builder.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,21 @@ public int LastIndexOf(T item, int startIndex, int count, IEqualityComparer<T> e
624624
/// </summary>
625625
public void Reverse()
626626
{
627-
Array.Reverse(_elements, 0, _count);
627+
// The non-generic Array.Reverse is not used because it does not perform
628+
// well for non-primitive value types.
629+
// If/when a generic Array.Reverse<T> becomes available, the below code
630+
// can be deleted and replaced with a call to Array.Reverse<T>.
631+
int i = 0;
632+
int j = _count - 1;
633+
T[] array = _elements;
634+
while (i < j)
635+
{
636+
T temp = array[i];
637+
array[i] = array[j];
638+
array[j] = temp;
639+
i++;
640+
j--;
641+
}
628642
}
629643

630644
/// <summary>

0 commit comments

Comments
 (0)