Skip to content

Use in on Unsafe.AreSame callsites #105000

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ public ReadOnlyTensorSpan<T> this[params scoped ReadOnlySpan<NRange> ranges]
left._shape.FlattenedLength == right._shape.FlattenedLength &&
left.Rank == right.Rank &&
left._shape.Lengths.SequenceEqual(right._shape.Lengths )&&
Unsafe.AreSame(ref left._reference, ref right._reference);
Unsafe.AreSame(in left._reference, in right._reference);

/// <summary>
/// This method is not supported as spans cannot be boxed. To compare two spans, use operator==.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ public TensorSpan<T> this[params scoped ReadOnlySpan<NRange> ranges]
left.Rank == right.Rank &&
left._shape.Lengths.SequenceEqual(right._shape.Lengths) &&
left._shape.Strides.SequenceEqual(right._shape.Strides) &&
Unsafe.AreSame(ref left._reference, ref right._reference);
Unsafe.AreSame(in left._reference, in right._reference);

/// <summary>
/// This method is not supported as spans cannot be boxed. To compare two spans, use operator==.
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Private.CoreLib/src/System/Buffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ private static void _BulkMoveWithWriteBarrier(ref byte destination, ref byte sou
{
Debug.Assert(byteCount > BulkMoveWithWriteBarrierChunk);

if (Unsafe.AreSame(ref source, ref destination))
if (Unsafe.AreSame(in source, in destination))
return;

// This is equivalent to: (destination - source) >= byteCount || (destination - source) < 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ private static void ReverseEndianness<T, TReverser>(ReadOnlySpan<T> source, Span
ref T sourceRef = ref MemoryMarshal.GetReference(source);
ref T destRef = ref MemoryMarshal.GetReference(destination);

if (Unsafe.AreSame(ref sourceRef, ref destRef) ||
if (Unsafe.AreSame(in sourceRef, in destRef) ||
!source.Overlaps(destination, out int elementOffset) ||
elementOffset < 0)
{
Expand Down Expand Up @@ -383,7 +383,7 @@ public static void ReverseEndianness(ReadOnlySpan<Int128> source, Span<Int128> d
ThrowDestinationTooSmall();
}

if (Unsafe.AreSame(ref MemoryMarshal.GetReference(source), ref MemoryMarshal.GetReference(destination)) ||
if (Unsafe.AreSame(in MemoryMarshal.GetReference(source), in MemoryMarshal.GetReference(destination)) ||
!source.Overlaps(destination, out int elementOffset) ||
elementOffset < 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ private static void SwapIfGreater(ref T i, ref T j)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void Swap(ref T i, ref T j)
{
Debug.Assert(!Unsafe.AreSame(ref i, ref j));
Debug.Assert(!Unsafe.AreSame(in i, in j));

T t = i;
i = j;
Expand Down Expand Up @@ -496,7 +496,7 @@ private static unsafe int PickPivotAndPartition(Span<T> keys)
}

// Put the pivot in the correct location.
if (!Unsafe.AreSame(ref leftRef, ref nextToLastRef))
if (!Unsafe.AreSame(in leftRef, in nextToLastRef))
{
Swap(ref leftRef, ref nextToLastRef);
}
Expand Down
4 changes: 2 additions & 2 deletions src/libraries/System.Private.CoreLib/src/System/Convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2540,8 +2540,8 @@ private static unsafe void ToBase64CharsLargeNoLineBreaks(ReadOnlySpan<byte> byt
// 1 byte at a time as is common in other vectorized operations, as nothing will remain after
// the 4-byte loop.

Debug.Assert(Unsafe.AreSame(ref srcBeginning, ref src));
Debug.Assert(Unsafe.AreSame(ref srcBeginning, ref Unsafe.As<ushort, byte>(ref dest)),
Debug.Assert(Unsafe.AreSame(in srcBeginning, in src));
Debug.Assert(Unsafe.AreSame(in srcBeginning, in Unsafe.As<ushort, byte>(ref dest)),
"The two references should have ended up exactly at the beginning");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ internal static bool TryFormat<TChar>(DateTime dateTime, Span<TChar> destination

var vlb = new ValueListBuilder<TChar>(destination);
FormatCustomized(dateTime, format, dtfi, offset, ref vlb);
bool success = Unsafe.AreSame(ref MemoryMarshal.GetReference(destination), ref MemoryMarshal.GetReference(vlb.AsSpan()));
bool success = Unsafe.AreSame(in MemoryMarshal.GetReference(destination), in MemoryMarshal.GetReference(vlb.AsSpan()));
if (success)
{
// The reference inside of the builder is still the destination. That means the builder didn't need to grow to beyond
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ public bool TryCopyTo(Span<T> destination)
/// </summary>
public static bool operator ==(ReadOnlySpan<T> left, ReadOnlySpan<T> right) =>
left._length == right._length &&
Unsafe.AreSame(ref left._reference, ref right._reference);
Unsafe.AreSame(in left._reference, in right._reference);

/// <summary>
/// For <see cref="ReadOnlySpan{Char}"/>, returns a new instance of string that represents the characters pointed to by the span.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@ internal override int LastIndexOfAnyExcept(ReadOnlySpan<char> span) =>
private int IndexOfAny<TNegator>(ref char searchSpace, int searchSpaceLength)
where TNegator : struct, IndexOfAnyAsciiSearcher.INegator
{
ref char searchSpaceEnd = ref Unsafe.Add(ref searchSpace, searchSpaceLength);
ref readonly char searchSpaceEnd = ref Unsafe.Add(ref searchSpace, searchSpaceLength);
ref char cur = ref searchSpace;
uint[] bitmap = _bitmap;

while (!Unsafe.AreSame(ref cur, ref searchSpaceEnd))
while (!Unsafe.AreSame(in cur, in searchSpaceEnd))
{
char c = cur;
if (TNegator.NegateIfNeeded(Contains(bitmap, c)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ private static TResult IndexOfAnyCore<TResult, TNegator, TOptimizations, TResult

if (searchSpaceLength < Vector128<ushort>.Count)
{
ref short searchSpaceEnd = ref Unsafe.Add(ref searchSpace, searchSpaceLength);
ref readonly short searchSpaceEnd = ref Unsafe.Add(ref searchSpace, searchSpaceLength);

while (!Unsafe.AreSame(ref currentSearchSpace, ref searchSpaceEnd))
while (!Unsafe.AreSame(in currentSearchSpace, in searchSpaceEnd))
{
char c = (char)currentSearchSpace;
if (TNegator.NegateIfNeeded(state.Lookup.Contains128(c)))
Expand Down Expand Up @@ -498,9 +498,9 @@ private static TResult IndexOfAnyCore<TResult, TNegator, TResultMapper>(ref byte

if (searchSpaceLength < sizeof(ulong))
{
ref byte searchSpaceEnd = ref Unsafe.Add(ref searchSpace, searchSpaceLength);
ref readonly byte searchSpaceEnd = ref Unsafe.Add(ref searchSpace, searchSpaceLength);

while (!Unsafe.AreSame(ref currentSearchSpace, ref searchSpaceEnd))
while (!Unsafe.AreSame(in currentSearchSpace, in searchSpaceEnd))
{
byte b = currentSearchSpace;
if (TNegator.NegateIfNeeded(state.Lookup.Contains(b)))
Expand Down Expand Up @@ -768,9 +768,9 @@ private static TResult IndexOfAnyCore<TResult, TNegator, TResultMapper>(ref byte

if (!IsVectorizationSupported || searchSpaceLength < sizeof(ulong))
{
ref byte searchSpaceEnd = ref Unsafe.Add(ref searchSpace, searchSpaceLength);
ref readonly byte searchSpaceEnd = ref Unsafe.Add(ref searchSpace, searchSpaceLength);

while (!Unsafe.AreSame(ref currentSearchSpace, ref searchSpaceEnd))
while (!Unsafe.AreSame(in currentSearchSpace, in searchSpaceEnd))
{
byte b = currentSearchSpace;
if (TNegator.NegateIfNeeded(state.Lookup.Contains(b)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ private static int IndexOfAnyVectorizedAvx512<TUseFastContains>(ref char searchS

if (Unsafe.IsAddressGreaterThan(ref cur, ref lastStartVector))
{
if (Unsafe.AreSame(ref cur, ref searchSpaceEnd))
if (Unsafe.AreSame(in cur, in searchSpaceEnd))
{
break;
}
Expand Down Expand Up @@ -520,7 +520,7 @@ private static int IndexOfAnyVectorized<TUseFastContains>(ref char searchSpace,

if (Unsafe.IsAddressGreaterThan(ref cur, ref lastStartVectorAvx2))
{
if (Unsafe.AreSame(ref cur, ref searchSpaceEnd))
if (Unsafe.AreSame(in cur, in searchSpaceEnd))
{
return -1;
}
Expand Down Expand Up @@ -560,7 +560,7 @@ private static int IndexOfAnyVectorized<TUseFastContains>(ref char searchSpace,

if (Unsafe.IsAddressGreaterThan(ref cur, ref lastStartVector))
{
if (Unsafe.AreSame(ref cur, ref searchSpaceEnd))
if (Unsafe.AreSame(in cur, in searchSpaceEnd))
{
break;
}
Expand Down Expand Up @@ -610,7 +610,7 @@ private static int LastIndexOfAnyVectorizedAvx512<TUseFastContains>(ref char sea

if (!Unsafe.IsAddressGreaterThan(ref cur, ref lastStartVector))
{
if (Unsafe.AreSame(ref cur, ref searchSpace))
if (Unsafe.AreSame(in cur, in searchSpace))
{
break;
}
Expand Down Expand Up @@ -699,7 +699,7 @@ private static int LastIndexOfAnyVectorized<TUseFastContains>(ref char searchSpa

if (!Unsafe.IsAddressGreaterThan(ref cur, ref lastStartVectorAvx2))
{
if (Unsafe.AreSame(ref cur, ref searchSpace))
if (Unsafe.AreSame(in cur, in searchSpace))
{
return -1;
}
Expand Down Expand Up @@ -741,7 +741,7 @@ private static int LastIndexOfAnyVectorized<TUseFastContains>(ref char searchSpa

if (!Unsafe.IsAddressGreaterThan(ref cur, ref lastStartVector))
{
if (Unsafe.AreSame(ref cur, ref searchSpace))
if (Unsafe.AreSame(in cur, in searchSpace))
{
break;
}
Expand Down Expand Up @@ -962,10 +962,10 @@ private static bool TryFindLastMatchOverlappedAvx512<TUseFastContains>(ref char
internal static int IndexOfAnySimpleLoop<TNegator>(ref char searchSpace, int searchSpaceLength, ReadOnlySpan<char> values)
where TNegator : struct, IndexOfAnyAsciiSearcher.INegator
{
ref char searchSpaceEnd = ref Unsafe.Add(ref searchSpace, searchSpaceLength);
ref readonly char searchSpaceEnd = ref Unsafe.Add(ref searchSpace, searchSpaceLength);
ref char cur = ref searchSpace;

while (!Unsafe.AreSame(ref cur, ref searchSpaceEnd))
while (!Unsafe.AreSame(in cur, in searchSpaceEnd))
{
char c = cur;
if (TNegator.NegateIfNeeded(Contains(values, c)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ public static int IndexOfAnySimpleLoop<TUseFastContains, TNegator>(ref char sear
where TUseFastContains : struct, SearchValues.IRuntimeConst
where TNegator : struct, IndexOfAnyAsciiSearcher.INegator
{
ref char searchSpaceEnd = ref Unsafe.Add(ref searchSpace, searchSpaceLength);
ref readonly char searchSpaceEnd = ref Unsafe.Add(ref searchSpace, searchSpaceLength);
ref char cur = ref searchSpace;

if (TUseFastContains.Value)
Expand All @@ -243,7 +243,7 @@ public static int IndexOfAnySimpleLoop<TUseFastContains, TNegator>(ref char sear
char[] hashEntries = state._hashEntries;
uint multiplier = state._multiplier;

while (!Unsafe.AreSame(ref cur, ref searchSpaceEnd))
while (!Unsafe.AreSame(in cur, in searchSpaceEnd))
{
char c = cur;
if (TNegator.NegateIfNeeded(FastContains(hashEntries, multiplier, c)))
Expand All @@ -256,7 +256,7 @@ public static int IndexOfAnySimpleLoop<TUseFastContains, TNegator>(ref char sear
}
else
{
while (!Unsafe.AreSame(ref cur, ref searchSpaceEnd))
while (!Unsafe.AreSame(in cur, in searchSpaceEnd))
{
char c = cur;
if (TNegator.NegateIfNeeded(state.SlowProbabilisticContains(c)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ private int IndexOfAnyN2Vector128(ReadOnlySpan<char> span)

if (Unsafe.IsAddressGreaterThan(ref searchSpace, ref lastSearchSpaceStart))
{
if (Unsafe.AreSame(ref searchSpace, ref Unsafe.Add(ref lastSearchSpaceStart, CharsPerIterationVector128)))
if (Unsafe.AreSame(in searchSpace, in Unsafe.Add(ref lastSearchSpaceStart, CharsPerIterationVector128)))
{
return -1;
}
Expand Down Expand Up @@ -276,7 +276,7 @@ private int IndexOfAnyN2Avx2(ReadOnlySpan<char> span)

if (Unsafe.IsAddressGreaterThan(ref searchSpace, ref lastSearchSpaceStart))
{
if (Unsafe.AreSame(ref searchSpace, ref Unsafe.Add(ref lastSearchSpaceStart, CharsPerIterationAvx2)))
if (Unsafe.AreSame(in searchSpace, in Unsafe.Add(ref lastSearchSpaceStart, CharsPerIterationAvx2)))
{
return -1;
}
Expand Down Expand Up @@ -328,7 +328,7 @@ private int IndexOfAnyN2Avx512(ReadOnlySpan<char> span)

if (Unsafe.IsAddressGreaterThan(ref searchSpace, ref lastSearchSpaceStart))
{
if (Unsafe.AreSame(ref searchSpace, ref Unsafe.Add(ref lastSearchSpaceStart, CharsPerIterationAvx512)))
if (Unsafe.AreSame(in searchSpace, in Unsafe.Add(ref lastSearchSpaceStart, CharsPerIterationAvx512)))
{
return -1;
}
Expand Down Expand Up @@ -398,7 +398,7 @@ private int IndexOfAnyN3Vector128(ReadOnlySpan<char> span)

if (Unsafe.IsAddressGreaterThan(ref searchSpace, ref lastSearchSpaceStart))
{
if (Unsafe.AreSame(ref searchSpace, ref Unsafe.Add(ref lastSearchSpaceStart, CharsPerIterationVector128)))
if (Unsafe.AreSame(in searchSpace, in Unsafe.Add(ref lastSearchSpaceStart, CharsPerIterationVector128)))
{
return -1;
}
Expand Down Expand Up @@ -455,7 +455,7 @@ private int IndexOfAnyN3Avx2(ReadOnlySpan<char> span)

if (Unsafe.IsAddressGreaterThan(ref searchSpace, ref lastSearchSpaceStart))
{
if (Unsafe.AreSame(ref searchSpace, ref Unsafe.Add(ref lastSearchSpaceStart, CharsPerIterationAvx2)))
if (Unsafe.AreSame(in searchSpace, in Unsafe.Add(ref lastSearchSpaceStart, CharsPerIterationAvx2)))
{
return -1;
}
Expand Down Expand Up @@ -510,7 +510,7 @@ private int IndexOfAnyN3Avx512(ReadOnlySpan<char> span)

if (Unsafe.IsAddressGreaterThan(ref searchSpace, ref lastSearchSpaceStart))
{
if (Unsafe.AreSame(ref searchSpace, ref Unsafe.Add(ref lastSearchSpaceStart, CharsPerIterationAvx512)))
if (Unsafe.AreSame(in searchSpace, in Unsafe.Add(ref lastSearchSpaceStart, CharsPerIterationAvx512)))
{
return -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private int IndexOf(ref char searchSpace, int searchSpaceLength)

if (Unsafe.IsAddressGreaterThan(ref searchSpace, ref lastSearchSpace))
{
if (Unsafe.AreSame(ref searchSpace, ref Unsafe.Add(ref lastSearchSpace, Vector512<ushort>.Count)))
if (Unsafe.AreSame(in searchSpace, in Unsafe.Add(ref lastSearchSpace, Vector512<ushort>.Count)))
{
return -1;
}
Expand Down Expand Up @@ -152,7 +152,7 @@ private int IndexOf(ref char searchSpace, int searchSpaceLength)

if (Unsafe.IsAddressGreaterThan(ref searchSpace, ref lastSearchSpace))
{
if (Unsafe.AreSame(ref searchSpace, ref Unsafe.Add(ref lastSearchSpace, Vector256<ushort>.Count)))
if (Unsafe.AreSame(in searchSpace, in Unsafe.Add(ref lastSearchSpace, Vector256<ushort>.Count)))
{
return -1;
}
Expand Down Expand Up @@ -199,7 +199,7 @@ private int IndexOf(ref char searchSpace, int searchSpaceLength)

if (Unsafe.IsAddressGreaterThan(ref searchSpace, ref lastSearchSpace))
{
if (Unsafe.AreSame(ref searchSpace, ref Unsafe.Add(ref lastSearchSpace, Vector128<ushort>.Count)))
if (Unsafe.AreSame(in searchSpace, in Unsafe.Add(ref lastSearchSpace, Vector128<ushort>.Count)))
{
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Private.CoreLib/src/System/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ public bool TryCopyTo(Span<T> destination)
/// </summary>
public static bool operator ==(Span<T> left, Span<T> right) =>
left._length == right._length &&
Unsafe.AreSame(ref left._reference, ref right._reference);
Unsafe.AreSame(in left._reference, in right._reference);

/// <summary>
/// Defines an implicit conversion of a <see cref="Span{T}"/> to a <see cref="ReadOnlySpan{T}"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -801,7 +801,7 @@ public static unsafe bool SequenceEqual(ref byte first, ref byte second, nuint l
Longer:
// Only check that the ref is the same if buffers are large,
// and hence its worth avoiding doing unnecessary comparisons
if (!Unsafe.AreSame(ref first, ref second))
if (!Unsafe.AreSame(in first, in second))
{
// C# compiler inverts this test, making the outer goto the conditional jmp.
goto Vector;
Expand Down Expand Up @@ -965,7 +965,7 @@ public static unsafe int SequenceCompareTo(ref byte first, int firstLength, ref
Debug.Assert(firstLength >= 0);
Debug.Assert(secondLength >= 0);

if (Unsafe.AreSame(ref first, ref second))
if (Unsafe.AreSame(in first, in second))
goto Equal;

nuint minLength = (nuint)(((uint)firstLength < (uint)secondLength) ? (uint)firstLength : (uint)secondLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ internal static unsafe void Memmove(ref byte dest, ref byte src, nuint len)
BuffersOverlap:
Debug.Assert(len > 0);
// If the buffers overlap perfectly, there's no point to copying the data.
if (Unsafe.AreSame(ref dest, ref src))
if (Unsafe.AreSame(in dest, in src))
{
// Both could be null with a non-zero length, perform an implicit null check.
_ = Unsafe.ReadUnaligned<byte>(ref dest);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ public static unsafe int SequenceCompareTo(ref char first, int firstLength, ref

int lengthDelta = firstLength - secondLength;

if (Unsafe.AreSame(ref first, ref second))
if (Unsafe.AreSame(in first, in second))
goto Equal;

nuint minLength = (nuint)(((uint)firstLength < (uint)secondLength) ? (uint)firstLength : (uint)secondLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1196,7 +1196,7 @@ public static bool SequenceEqual<T>(ref T first, ref T second, int length) where
{
Debug.Assert(length >= 0);

if (Unsafe.AreSame(ref first, ref second))
if (Unsafe.AreSame(in first, in second))
goto Equal;

nint index = 0; // Use nint for arithmetic to avoid unnecessary 64->32->64 truncations
Expand Down
Loading
Loading