Skip to content

Commit 46a4004

Browse files
authored
Optimise bounds check in Replace.ReplaceComparer (#119560)
[Diffs](MihuBot/runtime-utils#1556)
1 parent 275a125 commit 46a4004

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4761,6 +4761,8 @@ ref Unsafe.As<T, long>(ref dst),
47614761
ReplaceDefaultComparer(source, destination, oldValue, newValue);
47624762
static void ReplaceDefaultComparer(ReadOnlySpan<T> source, Span<T> destination, T oldValue, T newValue)
47634763
{
4764+
destination = destination.Slice(0, source.Length);
4765+
47644766
for (int i = 0; i < source.Length; i++)
47654767
{
47664768
destination[i] = EqualityComparer<T>.Default.Equals(source[i], oldValue) ? newValue : source[i];
@@ -4772,7 +4774,9 @@ static void ReplaceDefaultComparer(ReadOnlySpan<T> source, Span<T> destination,
47724774
ReplaceComparer(source, destination, oldValue, newValue, comparer);
47734775
static void ReplaceComparer(ReadOnlySpan<T> source, Span<T> destination, T oldValue, T newValue, IEqualityComparer<T>? comparer)
47744776
{
4777+
destination = destination.Slice(0, source.Length);
47754778
comparer ??= EqualityComparer<T>.Default;
4779+
47764780
for (int i = 0; i < source.Length; i++)
47774781
{
47784782
destination[i] = comparer.Equals(source[i], oldValue) ? newValue : source[i];

0 commit comments

Comments
 (0)