Skip to content

Commit 0a807e8

Browse files
karakasastephentoubeiriktsarpalis
authored
improve InsertRange(Span<>) perf by reducing (#107683)
one array copy similar to #90089 Co-authored-by: Stephen Toub <[email protected]> Co-authored-by: Eirik Tsarpalis <[email protected]>
1 parent c875470 commit 0a807e8

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

src/libraries/System.Private.CoreLib/src/System/Collections/Generic/CollectionExtensions.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,13 @@ public static void InsertRange<T>(this List<T> list, int index, params ReadOnlyS
136136
{
137137
if (list._items.Length - list._size < source.Length)
138138
{
139-
list.Grow(checked(list._size + source.Length));
139+
list.GrowForInsertion(index, source.Length);
140140
}
141-
142-
// If the index at which to insert is less than the number of items in the list,
143-
// shift all items past that location in the list down to the end, making room
144-
// to copy in the new data.
145-
if (index < list._size)
141+
else if (index < list._size)
146142
{
143+
// If the index at which to insert is less than the number of items in the list,
144+
// shift all items past that location in the list down to the end, making room
145+
// to copy in the new data.
147146
Array.Copy(list._items, index, list._items, index + source.Length, list._size - index);
148147
}
149148

src/libraries/System.Private.CoreLib/src/System/Collections/Generic/List.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ internal void Grow(int capacity)
461461
/// </summary>
462462
/// <param name="indexToInsert">Index of the first insertion.</param>
463463
/// <param name="insertionCount">How many elements will be inserted.</param>
464-
private void GrowForInsertion(int indexToInsert, int insertionCount = 1)
464+
internal void GrowForInsertion(int indexToInsert, int insertionCount = 1)
465465
{
466466
Debug.Assert(insertionCount > 0);
467467

0 commit comments

Comments
 (0)