Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Span<T>.Fill implementation for Array.Fill #52590

Merged
merged 16 commits into from
Jun 30, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public BitArray(int length, bool defaultValue)

if (defaultValue)
{
m_array.AsSpan().Fill(-1);
Array.Fill(m_array, -1);

// clear high bit values in the last int
Div32Rem(length, out int extraBits);
Expand Down
13 changes: 5 additions & 8 deletions src/libraries/System.Private.CoreLib/src/System/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -785,10 +785,8 @@ public static void Fill<T>(T[] array, T value)
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.array);
}

for (int i = 0; i < array.Length; i++)
{
array[i] = value;
}
var span = new Span<T>(ref MemoryMarshal.GetArrayDataReference(array), array.Length);
span.Fill(value);
}

public static void Fill<T>(T[] array, T value, int startIndex, int count)
Expand All @@ -808,10 +806,9 @@ public static void Fill<T>(T[] array, T value, int startIndex, int count)
ThrowHelper.ThrowCountArgumentOutOfRange_ArgumentOutOfRange_Count();
}

for (int i = startIndex; i < startIndex + count; i++)
{
array[i] = value;
}
ref T first = ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(array), startIndex);
xtqqczze marked this conversation as resolved.
Show resolved Hide resolved
var span = new Span<T>(ref first, count);
span.Fill(value);
}

public static T? Find<T>(T[] array, Predicate<T> match)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ private enum Token
private static char[] CreateDefaultIndentChars()
{
var result = new char[IndentArrayLength];
result.AsSpan().Fill(DefaultIndentChar);
Array.Fill(result, DefaultIndentChar);
return result;
}

Expand Down