diff --git a/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs
index a93afa1efd507e..082e91bc719c93 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/GC.CoreCLR.cs
@@ -677,7 +677,7 @@ internal static void UnregisterMemoryLoadChangeNotification(Action notification)
/// If pinned is set to true, must not be a reference type or a type that contains object references.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)] // forced to ensure no perf drop for small memory buffers (hot path)
- public static T[] AllocateUninitializedArray(int length, bool pinned = false) // T[] rather than T?[] to match `new T[length]` behavior
+ public static unsafe T[] AllocateUninitializedArray(int length, bool pinned = false) // T[] rather than T?[] to match `new T[length]` behavior
{
if (!pinned)
{
@@ -689,10 +689,13 @@ public static T[] AllocateUninitializedArray(int length, bool pinned = false)
// for debug builds we always want to call AllocateNewArray to detect AllocateNewArray bugs
#if !DEBUG
// small arrays are allocated using `new[]` as that is generally faster.
- if (length < 2048 / Unsafe.SizeOf())
+#pragma warning disable 8500 // sizeof of managed types
+ if (length < 2048 / sizeof(T))
+#pragma warning restore 8500
{
return new T[length];
}
+
#endif
}
else if (RuntimeHelpers.IsReferenceOrContainsReferences())
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Runtime/DependentHandle.cs b/src/coreclr/System.Private.CoreLib/src/System/Runtime/DependentHandle.cs
index a8ccb94aae2eb8..027c41ae7533e1 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Runtime/DependentHandle.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Runtime/DependentHandle.cs
@@ -237,13 +237,12 @@ public void Dispose()
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern object? InternalGetTarget(IntPtr dependentHandle);
#else
- private static unsafe object? InternalGetTarget(IntPtr dependentHandle)
- {
- // This optimization is the same that is used in GCHandle in RELEASE mode.
- // This is not used in DEBUG builds as the runtime performs additional checks.
- // The logic below is the inlined copy of ObjectFromHandle in the unmanaged runtime.
- return Unsafe.As(ref *(IntPtr*)(nint)dependentHandle);
- }
+ // This optimization is the same that is used in GCHandle in RELEASE mode.
+ // This is not used in DEBUG builds as the runtime performs additional checks.
+ // The logic below is the inlined copy of ObjectFromHandle in the unmanaged runtime.
+#pragma warning disable 8500 // address of managed types
+ private static unsafe object? InternalGetTarget(IntPtr dependentHandle) => *(object*)dependentHandle;
+#pragma warning restore 8500
#endif
[MethodImpl(MethodImplOptions.InternalCall)]
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/GCHandle.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/GCHandle.CoreCLR.cs
index b84be38d886fc6..c62d6aeb75fe42 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/GCHandle.CoreCLR.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/GCHandle.CoreCLR.cs
@@ -18,8 +18,9 @@ public partial struct GCHandle
[MethodImpl(MethodImplOptions.InternalCall)]
internal static extern object? InternalGet(IntPtr handle);
#else
- internal static unsafe object? InternalGet(IntPtr handle) =>
- Unsafe.As(ref *(IntPtr*)(nint)handle);
+#pragma warning disable 8500 // address of managed types
+ internal static unsafe object? InternalGet(IntPtr handle) => *(object*)handle;
+#pragma warning restore 8500
#endif
[MethodImpl(MethodImplOptions.InternalCall)]
diff --git a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CachedInterfaceDispatch.cs b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CachedInterfaceDispatch.cs
index 841f0cd145643b..ee9c669b6cb35e 100644
--- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CachedInterfaceDispatch.cs
+++ b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/CachedInterfaceDispatch.cs
@@ -15,7 +15,9 @@ internal static unsafe class CachedInterfaceDispatch
private static unsafe IntPtr RhpCidResolve(IntPtr callerTransitionBlockParam, IntPtr pCell)
{
IntPtr locationOfThisPointer = callerTransitionBlockParam + TransitionBlock.GetThisOffset();
- object pObject = Unsafe.As(ref *(IntPtr*)locationOfThisPointer);
+#pragma warning disable 8500 // address of managed types
+ object pObject = *(object*)locationOfThisPointer;
+#pragma warning restore 8500
IntPtr dispatchResolveTarget = RhpCidResolve_Worker(pObject, pCell);
return dispatchResolveTarget;
}
diff --git a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InteropServices/UnsafeGCHandle.cs b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InteropServices/UnsafeGCHandle.cs
index 2d4b13a92c5e28..cf95df4680444c 100644
--- a/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InteropServices/UnsafeGCHandle.cs
+++ b/src/coreclr/nativeaot/Runtime.Base/src/System/Runtime/InteropServices/UnsafeGCHandle.cs
@@ -41,7 +41,9 @@ public unsafe object Target
// The runtime performs additional checks in debug builds
return InternalCalls.RhHandleGet(_handle);
#else
- return Unsafe.As(ref *(IntPtr*)_handle);
+#pragma warning disable 8500 // address of managed types
+ return *(object*)_handle;
+#pragma warning restore 8500
#endif
}
diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/GC.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/GC.NativeAot.cs
index 7e8441cdb90b8e..6153784b66fbf7 100644
--- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/GC.NativeAot.cs
+++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/GC.NativeAot.cs
@@ -773,7 +773,9 @@ public static unsafe T[] AllocateUninitializedArray(int length, bool pinned =
// for debug builds we always want to call AllocateNewArray to detect AllocateNewArray bugs
#if !DEBUG
// small arrays are allocated using `new[]` as that is generally faster.
- if (length < 2048 / Unsafe.SizeOf())
+#pragma warning disable 8500 // sizeof of managed types
+ if (length < 2048 / sizeof(T))
+#pragma warning restore 8500
{
return new T[length];
}
diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.NativeAot.cs
index ed85c17e1dc5fc..b263ee17e26482 100644
--- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.NativeAot.cs
+++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.NativeAot.cs
@@ -305,8 +305,10 @@ ptr is string ||
// Compat note: CLR wouldn't bother with a range check. If someone does this,
// they're likely taking dependency on some CLR implementation detail quirk.
- if (checked(ofs + Unsafe.SizeOf()) > size)
+#pragma warning disable 8500 // sizeof of managed types
+ if (checked(ofs + sizeof(T)) > size)
throw new ArgumentOutOfRangeException(nameof(ofs));
+#pragma warning restore 8500
IntPtr nativeBytes = AllocCoTaskMem(size);
NativeMemory.Clear((void*)nativeBytes, (nuint)size);
@@ -384,8 +386,10 @@ ptr is string ||
// Compat note: CLR wouldn't bother with a range check. If someone does this,
// they're likely taking dependency on some CLR implementation detail quirk.
- if (checked(ofs + Unsafe.SizeOf()) > size)
+#pragma warning disable 8500 // sizeof of managed types
+ if (checked(ofs + sizeof(T)) > size)
throw new ArgumentOutOfRangeException(nameof(ofs));
+#pragma warning restore 8500
IntPtr nativeBytes = AllocCoTaskMem(size);
NativeMemory.Clear((void*)nativeBytes, (nuint)size);
diff --git a/src/coreclr/tools/Common/TypeSystem/IL/Stubs/UnsafeIntrinsics.cs b/src/coreclr/tools/Common/TypeSystem/IL/Stubs/UnsafeIntrinsics.cs
index f25671773490bf..2b90323728049f 100644
--- a/src/coreclr/tools/Common/TypeSystem/IL/Stubs/UnsafeIntrinsics.cs
+++ b/src/coreclr/tools/Common/TypeSystem/IL/Stubs/UnsafeIntrinsics.cs
@@ -22,8 +22,6 @@ public static MethodIL EmitIL(MethodDesc method)
{
case "AsPointer":
return new ILStubMethodIL(method, new byte[] { (byte)ILOpcode.ldarg_0, (byte)ILOpcode.conv_u, (byte)ILOpcode.ret }, Array.Empty(), null);
- case "SizeOf":
- return EmitSizeOf(method);
case "As":
case "AsRef":
return new ILStubMethodIL(method, new byte[] { (byte)ILOpcode.ldarg_0, (byte)ILOpcode.ret }, Array.Empty(), null);
@@ -98,19 +96,6 @@ public static MethodIL EmitIL(MethodDesc method)
return null;
}
- private static MethodIL EmitSizeOf(MethodDesc method)
- {
- Debug.Assert(method.Signature.IsStatic && method.Signature.Length == 0);
-
- TypeSystemContext context = method.Context;
-
- ILEmitter emit = new ILEmitter();
- ILCodeStream codeStream = emit.NewCodeStream();
- codeStream.Emit(ILOpcode.sizeof_, emit.NewToken(context.GetSignatureVariable(0, method: true)));
- codeStream.Emit(ILOpcode.ret);
- return emit.Link(method);
- }
-
private static MethodIL EmitAdd(MethodDesc method)
{
Debug.Assert(method.Signature.IsStatic && method.Signature.Length == 2);
diff --git a/src/coreclr/vm/corelib.h b/src/coreclr/vm/corelib.h
index 9f97a8001cec5c..b5ff0a1c795939 100644
--- a/src/coreclr/vm/corelib.h
+++ b/src/coreclr/vm/corelib.h
@@ -694,7 +694,6 @@ DEFINE_METHOD(UNSAFE, BYREF_IS_NULL, IsNullRef, NoSig)
DEFINE_METHOD(UNSAFE, BYREF_NULLREF, NullRef, NoSig)
DEFINE_METHOD(UNSAFE, AS_REF_IN, AsRef, GM_RefT_RetRefT)
DEFINE_METHOD(UNSAFE, AS_REF_POINTER, AsRef, GM_VoidPtr_RetRefT)
-DEFINE_METHOD(UNSAFE, SIZEOF, SizeOf, NoSig)
DEFINE_METHOD(UNSAFE, BYREF_AS, As, GM_RefTFrom_RetRefTTo)
DEFINE_METHOD(UNSAFE, OBJECT_AS, As, GM_Obj_RetT)
DEFINE_METHOD(UNSAFE, BYREF_ADD, Add, GM_RefT_Int_RetRefT)
diff --git a/src/coreclr/vm/jitinterface.cpp b/src/coreclr/vm/jitinterface.cpp
index 6d37ff818217fb..c1553c14f6c031 100644
--- a/src/coreclr/vm/jitinterface.cpp
+++ b/src/coreclr/vm/jitinterface.cpp
@@ -6789,24 +6789,6 @@ bool getILIntrinsicImplementationForUnsafe(MethodDesc * ftn,
return true;
}
- else if (tk == CoreLibBinder::GetMethod(METHOD__UNSAFE__SIZEOF)->GetMemberDef())
- {
- _ASSERTE(ftn->HasMethodInstantiation());
- Instantiation inst = ftn->GetMethodInstantiation();
-
- _ASSERTE(ftn->GetNumGenericMethodArgs() == 1);
- mdToken tokGenericArg = FindGenericMethodArgTypeSpec(CoreLibBinder::GetModule()->GetMDImport());
-
- static const BYTE ilcode[] =
- {
- CEE_PREFIX1, (CEE_SIZEOF & 0xFF), (BYTE)(tokGenericArg), (BYTE)(tokGenericArg >> 8), (BYTE)(tokGenericArg >> 16), (BYTE)(tokGenericArg >> 24),
- CEE_RET
- };
-
- setILIntrinsicMethodInfo(methInfo,const_cast(ilcode),sizeof(ilcode), 1);
-
- return true;
- }
else if (tk == CoreLibBinder::GetMethod(METHOD__UNSAFE__BYREF_AS)->GetMemberDef() ||
tk == CoreLibBinder::GetMethod(METHOD__UNSAFE__OBJECT_AS)->GetMemberDef() ||
tk == CoreLibBinder::GetMethod(METHOD__UNSAFE__AS_REF_POINTER)->GetMemberDef() ||
diff --git a/src/libraries/Microsoft.Extensions.Logging.EventSource/src/LoggingEventSource.cs b/src/libraries/Microsoft.Extensions.Logging.EventSource/src/LoggingEventSource.cs
index 28f073d9ee0612..c9bc8c5c0aaaf4 100644
--- a/src/libraries/Microsoft.Extensions.Logging.EventSource/src/LoggingEventSource.cs
+++ b/src/libraries/Microsoft.Extensions.Logging.EventSource/src/LoggingEventSource.cs
@@ -516,7 +516,9 @@ private static unsafe void SetEventData(ref EventData eventData, ref T value,
else
{
eventData.DataPointer = (IntPtr)Unsafe.AsPointer(ref value);
- eventData.Size = Unsafe.SizeOf();
+#pragma warning disable 8500 // sizeof of managed types
+ eventData.Size = sizeof(T);
+#pragma warning restore 8500
}
}
}
diff --git a/src/libraries/System.Memory/src/System/Buffers/ArrayMemoryPool.cs b/src/libraries/System.Memory/src/System/Buffers/ArrayMemoryPool.cs
index 88ae8b05de7e18..aa0fb4de26bc14 100644
--- a/src/libraries/System.Memory/src/System/Buffers/ArrayMemoryPool.cs
+++ b/src/libraries/System.Memory/src/System/Buffers/ArrayMemoryPool.cs
@@ -9,12 +9,14 @@ internal sealed partial class ArrayMemoryPool : MemoryPool
{
public sealed override int MaxBufferSize => Array.MaxLength;
- public sealed override IMemoryOwner Rent(int minimumBufferSize = -1)
+ public sealed override unsafe IMemoryOwner Rent(int minimumBufferSize = -1)
{
+#pragma warning disable 8500 // sizeof of managed types
if (minimumBufferSize == -1)
- minimumBufferSize = 1 + (4095 / Unsafe.SizeOf());
+ minimumBufferSize = 1 + (4095 / sizeof(T));
else if (((uint)minimumBufferSize) > Array.MaxLength)
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.minimumBufferSize);
+#pragma warning restore 8500
return new ArrayMemoryPoolBuffer(minimumBufferSize);
}
diff --git a/src/libraries/System.Private.CoreLib/src/System/Array.cs b/src/libraries/System.Private.CoreLib/src/System/Array.cs
index 2e9f5f2e6c8694..a937f69a2d83b9 100644
--- a/src/libraries/System.Private.CoreLib/src/System/Array.cs
+++ b/src/libraries/System.Private.CoreLib/src/System/Array.cs
@@ -11,6 +11,8 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
+#pragma warning disable 8500 // sizeof of managed types
+
namespace System
{
[Serializable]
@@ -1308,7 +1310,7 @@ public static int IndexOf(T[] array, T value, int startIndex)
return IndexOf(array, value, startIndex, array.Length - startIndex);
}
- public static int IndexOf(T[] array, T value, int startIndex, int count)
+ public static unsafe int IndexOf(T[] array, T value, int startIndex, int count)
{
if (array == null)
{
@@ -1327,7 +1329,7 @@ public static int IndexOf(T[] array, T value, int startIndex, int count)
if (RuntimeHelpers.IsBitwiseEquatable())
{
- if (Unsafe.SizeOf() == sizeof(byte))
+ if (sizeof(T) == sizeof(byte))
{
int result = SpanHelpers.IndexOfValueType(
ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Unsafe.As(array)), startIndex),
@@ -1335,7 +1337,7 @@ ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Unsafe.As(array))
count);
return (result >= 0 ? startIndex : 0) + result;
}
- else if (Unsafe.SizeOf() == sizeof(short))
+ else if (sizeof(T) == sizeof(short))
{
int result = SpanHelpers.IndexOfValueType(
ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Unsafe.As(array)), startIndex),
@@ -1343,7 +1345,7 @@ ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Unsafe.As(array)
count);
return (result >= 0 ? startIndex : 0) + result;
}
- else if (Unsafe.SizeOf() == sizeof(int))
+ else if (sizeof(T) == sizeof(int))
{
int result = SpanHelpers.IndexOfValueType(
ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Unsafe.As(array)), startIndex),
@@ -1351,7 +1353,7 @@ ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Unsafe.As(array)),
count);
return (result >= 0 ? startIndex : 0) + result;
}
- else if (Unsafe.SizeOf() == sizeof(long))
+ else if (sizeof(T) == sizeof(long))
{
int result = SpanHelpers.IndexOfValueType(
ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Unsafe.As(array)), startIndex),
@@ -1534,7 +1536,7 @@ public static int LastIndexOf(T[] array, T value, int startIndex)
return LastIndexOf(array, value, startIndex, (array.Length == 0) ? 0 : (startIndex + 1));
}
- public static int LastIndexOf(T[] array, T value, int startIndex, int count)
+ public static unsafe int LastIndexOf(T[] array, T value, int startIndex, int count)
{
if (array == null)
{
@@ -1574,7 +1576,7 @@ public static int LastIndexOf(T[] array, T value, int startIndex, int count)
if (RuntimeHelpers.IsBitwiseEquatable())
{
- if (Unsafe.SizeOf() == sizeof(byte))
+ if (sizeof(T) == sizeof(byte))
{
int endIndex = startIndex - count + 1;
int result = SpanHelpers.LastIndexOfValueType(
@@ -1584,7 +1586,7 @@ ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Unsafe.As(array))
return (result >= 0 ? endIndex : 0) + result;
}
- else if (Unsafe.SizeOf() == sizeof(short))
+ else if (sizeof(T) == sizeof(short))
{
int endIndex = startIndex - count + 1;
int result = SpanHelpers.LastIndexOfValueType(
@@ -1594,7 +1596,7 @@ ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Unsafe.As(array)
return (result >= 0 ? endIndex : 0) + result;
}
- else if (Unsafe.SizeOf() == sizeof(int))
+ else if (sizeof(T) == sizeof(int))
{
int endIndex = startIndex - count + 1;
int result = SpanHelpers.LastIndexOfValueType(
@@ -1604,7 +1606,7 @@ ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(Unsafe.As(array)),
return (result >= 0 ? endIndex : 0) + result;
}
- else if (Unsafe.SizeOf() == sizeof(long))
+ else if (sizeof(T) == sizeof(long))
{
int endIndex = startIndex - count + 1;
int result = SpanHelpers.LastIndexOfValueType(
diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffer.cs b/src/libraries/System.Private.CoreLib/src/System/Buffer.cs
index 7ea4502c95fff8..52bfa1d39d5dbf 100644
--- a/src/libraries/System.Private.CoreLib/src/System/Buffer.cs
+++ b/src/libraries/System.Private.CoreLib/src/System/Buffer.cs
@@ -348,15 +348,16 @@ internal static unsafe void _ZeroMemory(ref byte b, nuint byteLength)
#if !MONO // Mono BulkMoveWithWriteBarrier is in terms of elements (not bytes) and takes a type handle.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static void Memmove(ref T destination, ref T source, nuint elementCount)
+ internal static unsafe void Memmove(ref T destination, ref T source, nuint elementCount)
{
+#pragma warning disable 8500 // sizeof of managed types
if (!RuntimeHelpers.IsReferenceOrContainsReferences())
{
// Blittable memmove
Memmove(
ref Unsafe.As(ref destination),
ref Unsafe.As(ref source),
- elementCount * (nuint)Unsafe.SizeOf());
+ elementCount * (nuint)sizeof(T));
}
else
{
@@ -364,8 +365,9 @@ ref Unsafe.As(ref source),
BulkMoveWithWriteBarrier(
ref Unsafe.As(ref destination),
ref Unsafe.As(ref source),
- elementCount * (nuint)Unsafe.SizeOf());
+ elementCount * (nuint)sizeof(T));
}
+#pragma warning restore 8500
}
// The maximum block size to for __BulkMoveWithWriteBarrier FCall. This is required to avoid GC starvation.
diff --git a/src/libraries/System.Private.CoreLib/src/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs b/src/libraries/System.Private.CoreLib/src/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs
index 9206dde5e5fa6f..bffa23fc679632 100644
--- a/src/libraries/System.Private.CoreLib/src/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs
+++ b/src/libraries/System.Private.CoreLib/src/System/Buffers/TlsOverPerCoreLockedStacksArrayPool.cs
@@ -459,13 +459,18 @@ public void Trim(int currentMilliseconds, int id, Utilities.MemoryPressure press
{
trimCount++;
}
- if (Unsafe.SizeOf() > StackModerateTypeSize)
+ unsafe
{
- trimCount++;
- }
- if (Unsafe.SizeOf() > StackLargeTypeSize)
- {
- trimCount++;
+#pragma warning disable 8500 // sizeof of managed types
+ if (sizeof(T) > StackModerateTypeSize)
+ {
+ trimCount++;
+ }
+ if (sizeof(T) > StackLargeTypeSize)
+ {
+ trimCount++;
+ }
+#pragma warning restore 8500
}
break;
diff --git a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ArraySortHelper.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ArraySortHelper.cs
index 966a1b0ba4358b..cf481d8999649a 100644
--- a/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ArraySortHelper.cs
+++ b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ArraySortHelper.cs
@@ -449,7 +449,7 @@ private static void IntroSort(Span keys, int depthLimit)
}
}
- private static int PickPivotAndPartition(Span keys)
+ private static unsafe int PickPivotAndPartition(Span keys)
{
Debug.Assert(keys.Length >= Array.IntrosortSizeThreshold);
@@ -494,7 +494,9 @@ private static int PickPivotAndPartition(Span keys)
{
Swap(ref leftRef, ref nextToLastRef);
}
- return (int)((nint)Unsafe.ByteOffset(ref zeroRef, ref leftRef) / Unsafe.SizeOf());
+#pragma warning disable 8500 // sizeof of managed types
+ return (int)((nint)Unsafe.ByteOffset(ref zeroRef, ref leftRef) / sizeof(T));
+#pragma warning restore 8500
}
private static void HeapSort(Span keys)
diff --git a/src/libraries/System.Private.CoreLib/src/System/IndexOfAnyValues/IndexOfAnyAsciiSearcher.cs b/src/libraries/System.Private.CoreLib/src/System/IndexOfAnyValues/IndexOfAnyAsciiSearcher.cs
index c33807fd33a184..8d9e2794ad2db8 100644
--- a/src/libraries/System.Private.CoreLib/src/System/IndexOfAnyValues/IndexOfAnyAsciiSearcher.cs
+++ b/src/libraries/System.Private.CoreLib/src/System/IndexOfAnyValues/IndexOfAnyAsciiSearcher.cs
@@ -8,6 +8,8 @@
using System.Runtime.Intrinsics.Arm;
using System.Runtime.Intrinsics.X86;
+#pragma warning disable 8500 // sizeof of managed types
+
namespace System.Buffers
{
internal static class IndexOfAnyAsciiSearcher
@@ -585,17 +587,17 @@ private static Vector128 Shuffle(Vector128 vector, Vector128 i
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static int ComputeFirstIndex(ref T searchSpace, ref T current, Vector128 result)
+ private static unsafe int ComputeFirstIndex(ref T searchSpace, ref T current, Vector128 result)
where TNegator : struct, INegator
{
uint mask = TNegator.ExtractMask(result);
int offsetInVector = BitOperations.TrailingZeroCount(mask);
- return offsetInVector + (int)(Unsafe.ByteOffset(ref searchSpace, ref current) / Unsafe.SizeOf());
+ return offsetInVector + (int)(Unsafe.ByteOffset(ref searchSpace, ref current) / sizeof(T));
}
#pragma warning disable IDE0060 // https://github.com/dotnet/roslyn-analyzers/issues/6228
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static int ComputeFirstIndexOverlapped(ref T searchSpace, ref T current0, ref T current1, Vector128 result)
+ private static unsafe int ComputeFirstIndexOverlapped(ref T searchSpace, ref T current0, ref T current1, Vector128 result)
where TNegator : struct, INegator
{
uint mask = TNegator.ExtractMask(result);
@@ -606,21 +608,21 @@ private static int ComputeFirstIndexOverlapped(ref T searchSpace, r
current0 = ref current1;
offsetInVector -= Vector128.Count;
}
- return offsetInVector + (int)(Unsafe.ByteOffset(ref searchSpace, ref current0) / Unsafe.SizeOf());
+ return offsetInVector + (int)(Unsafe.ByteOffset(ref searchSpace, ref current0) / sizeof(T));
}
#pragma warning restore IDE0060 // https://github.com/dotnet/roslyn-analyzers/issues/6228
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static int ComputeLastIndex(ref T searchSpace, ref T current, Vector128 result)
+ private static unsafe int ComputeLastIndex(ref T searchSpace, ref T current, Vector128 result)
where TNegator : struct, INegator
{
uint mask = TNegator.ExtractMask(result) & 0xFFFF;
int offsetInVector = 31 - BitOperations.LeadingZeroCount(mask);
- return offsetInVector + (int)(Unsafe.ByteOffset(ref searchSpace, ref current) / Unsafe.SizeOf());
+ return offsetInVector + (int)(Unsafe.ByteOffset(ref searchSpace, ref current) / sizeof(T));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static int ComputeLastIndexOverlapped(ref T searchSpace, ref T secondVector, Vector128 result)
+ private static unsafe int ComputeLastIndexOverlapped(ref T searchSpace, ref T secondVector, Vector128 result)
where TNegator : struct, INegator
{
uint mask = TNegator.ExtractMask(result) & 0xFFFF;
@@ -631,7 +633,7 @@ private static int ComputeLastIndexOverlapped(ref T searchSpace, re
}
// We matched within the second vector
- return offsetInVector - Vector128.Count + (int)(Unsafe.ByteOffset(ref searchSpace, ref secondVector) / Unsafe.SizeOf());
+ return offsetInVector - Vector128.Count + (int)(Unsafe.ByteOffset(ref searchSpace, ref secondVector) / sizeof(T));
}
internal interface INegator
diff --git a/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs b/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs
index b218550bd2720a..835816e0f38a49 100644
--- a/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs
+++ b/src/libraries/System.Private.CoreLib/src/System/MemoryExtensions.cs
@@ -10,6 +10,8 @@
using System.Runtime.InteropServices;
using System.Runtime.Intrinsics;
+#pragma warning disable 8500 // sizeof of managed types
+
namespace System
{
///
@@ -265,32 +267,32 @@ public static ReadOnlyMemory AsMemory(this string? text, Range range)
/// The span to search.
/// The value to search for.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool Contains(this Span span, T value) where T : IEquatable?
+ public static unsafe bool Contains(this Span span, T value) where T : IEquatable?
{
if (RuntimeHelpers.IsBitwiseEquatable())
{
- if (Unsafe.SizeOf() == sizeof(byte))
+ if (sizeof(T) == sizeof(byte))
{
return SpanHelpers.ContainsValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
Unsafe.As(ref value),
span.Length);
}
- else if (Unsafe.SizeOf() == sizeof(short))
+ else if (sizeof(T) == sizeof(short))
{
return SpanHelpers.ContainsValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
Unsafe.As(ref value),
span.Length);
}
- else if (Unsafe.SizeOf() == sizeof(int))
+ else if (sizeof(T) == sizeof(int))
{
return SpanHelpers.ContainsValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
Unsafe.As(ref value),
span.Length);
}
- else if (Unsafe.SizeOf() == sizeof(long))
+ else if (sizeof(T) == sizeof(long))
{
return SpanHelpers.ContainsValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
@@ -309,32 +311,32 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
/// The span to search.
/// The value to search for.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool Contains(this ReadOnlySpan span, T value) where T : IEquatable?
+ public static unsafe bool Contains(this ReadOnlySpan span, T value) where T : IEquatable?
{
if (RuntimeHelpers.IsBitwiseEquatable())
{
- if (Unsafe.SizeOf() == sizeof(byte))
+ if (sizeof(T) == sizeof(byte))
{
return SpanHelpers.ContainsValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
Unsafe.As(ref value),
span.Length);
}
- else if (Unsafe.SizeOf() == sizeof(short))
+ else if (sizeof(T) == sizeof(short))
{
return SpanHelpers.ContainsValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
Unsafe.As(ref value),
span.Length);
}
- else if (Unsafe.SizeOf() == sizeof(int))
+ else if (sizeof(T) == sizeof(int))
{
return SpanHelpers.ContainsValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
Unsafe.As(ref value),
span.Length);
}
- else if (Unsafe.SizeOf() == sizeof(long))
+ else if (sizeof(T) == sizeof(long))
{
return SpanHelpers.ContainsValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
@@ -352,29 +354,29 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
/// The span to search.
/// The value to search for.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int IndexOf(this Span span, T value) where T : IEquatable?
+ public static unsafe int IndexOf(this Span span, T value) where T : IEquatable?
{
if (RuntimeHelpers.IsBitwiseEquatable())
{
- if (Unsafe.SizeOf() == sizeof(byte))
+ if (sizeof(T) == sizeof(byte))
return SpanHelpers.IndexOfValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
Unsafe.As(ref value),
span.Length);
- if (Unsafe.SizeOf() == sizeof(short))
+ if (sizeof(T) == sizeof(short))
return SpanHelpers.IndexOfValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
Unsafe.As(ref value),
span.Length);
- if (Unsafe.SizeOf() == sizeof(int))
+ if (sizeof(T) == sizeof(int))
return SpanHelpers.IndexOfValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
Unsafe.As(ref value),
span.Length);
- if (Unsafe.SizeOf() == sizeof(long))
+ if (sizeof(T) == sizeof(long))
return SpanHelpers.IndexOfValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
Unsafe.As(ref value),
@@ -390,18 +392,18 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
/// The span to search.
/// The sequence to search for.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int IndexOf(this Span span, ReadOnlySpan value) where T : IEquatable?
+ public static unsafe int IndexOf(this Span span, ReadOnlySpan value) where T : IEquatable?
{
if (RuntimeHelpers.IsBitwiseEquatable())
{
- if (Unsafe.SizeOf() == sizeof(byte))
+ if (sizeof(T) == sizeof(byte))
return SpanHelpers.IndexOf(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
span.Length,
ref Unsafe.As(ref MemoryMarshal.GetReference(value)),
value.Length);
- if (Unsafe.SizeOf() == sizeof(char))
+ if (sizeof(T) == sizeof(char))
return SpanHelpers.IndexOf(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
span.Length,
@@ -418,32 +420,32 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(value)),
/// The span to search.
/// The value to search for.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int LastIndexOf(this Span span, T value) where T : IEquatable?
+ public static unsafe int LastIndexOf(this Span span, T value) where T : IEquatable?
{
if (RuntimeHelpers.IsBitwiseEquatable())
{
- if (Unsafe.SizeOf() == sizeof(byte))
+ if (sizeof(T) == sizeof(byte))
{
return SpanHelpers.LastIndexOfValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
Unsafe.As(ref value),
span.Length);
}
- else if (Unsafe.SizeOf() == sizeof(short))
+ else if (sizeof(T) == sizeof(short))
{
return SpanHelpers.LastIndexOfValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
Unsafe.As(ref value),
span.Length);
}
- else if (Unsafe.SizeOf() == sizeof(int))
+ else if (sizeof(T) == sizeof(int))
{
return SpanHelpers.LastIndexOfValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
Unsafe.As(ref value),
span.Length);
}
- else if (Unsafe.SizeOf() == sizeof(long))
+ else if (sizeof(T) == sizeof(long))
{
return SpanHelpers.LastIndexOfValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
@@ -461,11 +463,11 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
/// The span to search.
/// The sequence to search for.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int LastIndexOf(this Span span, ReadOnlySpan value) where T : IEquatable?
+ public static unsafe int LastIndexOf(this Span span, ReadOnlySpan value) where T : IEquatable?
{
if (RuntimeHelpers.IsBitwiseEquatable())
{
- if (Unsafe.SizeOf() == sizeof(byte))
+ if (sizeof(T) == sizeof(byte))
{
return SpanHelpers.LastIndexOf(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
@@ -473,7 +475,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
ref Unsafe.As(ref MemoryMarshal.GetReference(value)),
value.Length);
}
- else if (Unsafe.SizeOf() == sizeof(char))
+ else if (sizeof(T) == sizeof(char))
{
return SpanHelpers.LastIndexOf(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
@@ -554,25 +556,25 @@ public static int IndexOfAnyExcept(this Span span, IndexOfAnyValues val
/// If all of the values are , returns -1.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int IndexOfAnyExcept(this ReadOnlySpan span, T value) where T : IEquatable?
+ public static unsafe int IndexOfAnyExcept(this ReadOnlySpan span, T value) where T : IEquatable?
{
if (SpanHelpers.CanVectorizeAndBenefit(span.Length))
{
- if (Unsafe.SizeOf() == sizeof(byte))
+ if (sizeof(T) == sizeof(byte))
{
return SpanHelpers.IndexOfAnyExceptValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
Unsafe.As(ref value),
span.Length);
}
- else if (Unsafe.SizeOf() == sizeof(short))
+ else if (sizeof(T) == sizeof(short))
{
return SpanHelpers.IndexOfAnyExceptValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
Unsafe.As(ref value),
span.Length);
}
- else if (Unsafe.SizeOf() == sizeof(int))
+ else if (sizeof(T) == sizeof(int))
{
return SpanHelpers.IndexOfAnyExceptValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
@@ -581,7 +583,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
}
else
{
- Debug.Assert(Unsafe.SizeOf() == sizeof(long));
+ Debug.Assert(sizeof(T) == sizeof(long));
return SpanHelpers.IndexOfAnyExceptValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
@@ -605,11 +607,11 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
/// If all of the values are or , returns -1.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int IndexOfAnyExcept(this ReadOnlySpan span, T value0, T value1) where T : IEquatable?
+ public static unsafe int IndexOfAnyExcept(this ReadOnlySpan span, T value0, T value1) where T : IEquatable?
{
if (SpanHelpers.CanVectorizeAndBenefit(span.Length))
{
- if (Unsafe.SizeOf() == sizeof(byte))
+ if (sizeof(T) == sizeof(byte))
{
return SpanHelpers.IndexOfAnyExceptValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
@@ -617,7 +619,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
Unsafe.As(ref value1),
span.Length);
}
- else if (Unsafe.SizeOf() == sizeof(short))
+ else if (sizeof(T) == sizeof(short))
{
return SpanHelpers.IndexOfAnyExceptValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
@@ -641,11 +643,11 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
/// If all of the values are , , and , returns -1.
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static int IndexOfAnyExcept(this ReadOnlySpan span, T value0, T value1, T value2) where T : IEquatable?
+ public static unsafe int IndexOfAnyExcept(this ReadOnlySpan span, T value0, T value1, T value2) where T : IEquatable?
{
if (RuntimeHelpers.IsBitwiseEquatable())
{
- if (Unsafe.SizeOf() == sizeof(byte))
+ if (sizeof(T) == sizeof(byte))
{
return SpanHelpers.IndexOfAnyExceptValueType(
ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
@@ -654,7 +656,7 @@ ref Unsafe.As(ref MemoryMarshal.GetReference(span)),
Unsafe.As(ref value2),
span.Length);
}
- else if (Unsafe.SizeOf() == sizeof(short))
+ else if (sizeof(T) == sizeof(short))
{
return SpanHelpers.IndexOfAnyExceptValueType(
ref Unsafe.As