Repository navigation
Replace Unsafe.SizeOf with sizeof - #130727
Conversation
Co-authored-by: Copilot <[email protected]> Copilot-Session: 4f79da18-eb49-496c-a46d-fa89c70d9ce5
|
Azure Pipelines: Successfully started running 6 pipeline(s). 9 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @dotnet/area-system-runtime-intrinsics |
There was a problem hiding this comment.
Pull request overview
This PR performs a repo-wide mechanical cleanup to prefer the C# sizeof(...) operator over Unsafe.SizeOf<T>(), and removes now-unnecessary unsafe { ... } blocks used solely to enable sizeof expressions.
Changes:
- Replace
Unsafe.SizeOf<T>()withsizeof(T)/sizeof(SomeType)across tools, libraries, and tests. - Remove redundant
unsafestatement blocks where they only wrappedsizeof(...). - Update related documentation/comments to reference
sizeof(T)instead ofUnsafe.SizeOf<T>.
Reviewed changes
Copilot reviewed 219 out of 253 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/tools/StressLogAnalyzer/src/Program.cs | Use sizeof(StressLogHeader) for header-size validation. |
| src/tests/Loader/classloader/ExtendedLayout/CStruct.cs | Replace Unsafe.SizeOf with sizeof in layout assertions. |
| src/tests/Loader/classloader/explicitlayout/Regressions/100220/Runtime_100220.cs | Replace Unsafe.SizeOf with sizeof in regression assertions. |
| src/tests/JIT/Regression/JitBlue/Runtime_92201/Runtime_92201.cs | Replace Unsafe.SizeOf with sizeof in vector sizing / copy lengths. |
| src/tests/JIT/Regression/JitBlue/Runtime_76194/Runtime_76194.cs | Use sizeof(T) in pointer arithmetic helper. |
| src/tests/JIT/Regression/JitBlue/GitHub_3539/GitHub_3539.cs | Use sizeof(Bgr) for stackalloc sizing. |
| src/tests/JIT/HardwareIntrinsics/X86/Sse41/MinHorizontal.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86/Sse41/ConvertToVector128Int64.UInt32.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86/Sse41/ConvertToVector128Int64.UInt16.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86/Sse41/ConvertToVector128Int64.SByte.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86/Sse41/ConvertToVector128Int64.Int32.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86/Sse41/ConvertToVector128Int64.Int16.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86/Sse41/ConvertToVector128Int64.Byte.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86/Sse41/ConvertToVector128Int32.UInt16.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86/Sse41/ConvertToVector128Int32.SByte.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86/Sse41/ConvertToVector128Int32.Int16.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86/Sse41/ConvertToVector128Int32.Byte.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86/Sse41/ConvertToVector128Int16.SByte.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86/Sse41/ConvertToVector128Int16.Byte.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86/Sse2/ConvertToUInt64.Vector128UInt64.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86/Sse2/ConvertToInt64WithTruncation.Vector128Double.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86/Sse2/ConvertToInt32WithTruncation.Int32.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86/Shared/SimpleUnOpTest_DataTable.cs | Use sizeof for buffer size computations in test helper. |
| src/tests/JIT/HardwareIntrinsics/X86/Shared/SimpleTernOpTest_DataTable.cs | Use sizeof for buffer size computations in test helper. |
| src/tests/JIT/HardwareIntrinsics/X86/Shared/SimpleBinOpTest_DataTable.cs | Use sizeof for buffer size computations in test helper. |
| src/tests/JIT/HardwareIntrinsics/X86/Shared/SimpleBinOpConvTest.template | Update templates to use sizeof(...) instead of Unsafe.SizeOf<...>(). |
| src/tests/JIT/HardwareIntrinsics/X86/Shared/SimpleBinOpConvTest_DataTable.cs | Use sizeof for buffer size computations in test helper. |
| src/tests/JIT/HardwareIntrinsics/X86/Shared/SimdScalarUnOpTest.template | Update templates to use sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/X86/Shared/SimdScalarUnOpTest_DataTable.cs | Use sizeof for buffer size computations in test helper. |
| src/tests/JIT/HardwareIntrinsics/X86/Shared/SimdScalarUnOpConvTest.template | Update templates to use sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/X86/Shared/ScalarSimdUnOpTest.template | Update templates to use sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/X86/Shared/ScalarSimdUnOpTest_DataTable.cs | Use sizeof for buffer size computations in test helper. |
| src/tests/JIT/HardwareIntrinsics/X86/Shared/PclmulqdqOpTest.template | Update templates to use sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/X86/Shared/LoadUnOpTest.template | Update templates to use sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/X86/Shared/ImmUnOpTest.template | Update templates to use sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/X86/Shared/GenericUnOpTest.template | Update templates to use sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/X86/Shared/ExtractVector128Test.template | Update templates to use sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/X86/Shared/ExtractScalarTest.template | Update templates to use sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/X86/Shared/AesUnOpTest.template | Update templates to use sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/X86/Shared/_BooleanUnaryOpTestTemplate.template | Update templates to use sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/X86/General/VectorArray.cs | Use sizeof(T) for element size in vector array tests. |
| src/tests/JIT/HardwareIntrinsics/X86_Avx512/Avx512F/ConvertToVector512Double.Single.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86_Avx512/Avx512F/ConvertToVector512Double.Int32.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86_Avx512/Avx512F/ConvertToVector256Single.Double.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86_Avx512/Avx512F/ConvertToVector256Int32WithTruncation.Double.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86_Avx512/Avx512F/ConvertToVector256Int32.Double.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86_Avx/Avx1/ConvertToVector256Single.Int32.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86_Avx/Avx1/ConvertToVector256Int32WithTruncation.Single.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86_Avx/Avx1/ConvertToVector256Int32.Single.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86_Avx/Avx1/ConvertToVector256Double.Single.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86_Avx/Avx1/ConvertToVector256Double.Int32.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86_Avx/Avx1/ConvertToVector128Single.Double.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86_Avx/Avx1/ConvertToVector128Int32WithTruncation.Double.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/X86_Avx/Avx1/ConvertToVector128Int32.Double.cs | Replace Unsafe.SizeOf with sizeof for vector sizing / copy lengths. |
| src/tests/JIT/HardwareIntrinsics/General/Shared/VectorZeroTest.template | Update template sizing logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/General/Shared/VectorToStringTest.template | Update template sizing logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/General/Shared/VectorToScalarTest.template | Update template sizing logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/General/Shared/VectorGetAndWithLowerAndUpperTest.template | Update template sizing logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/General/Shared/VectorGetAndWithElementTest.template | Update template sizing logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/General/Shared/VectorExtendTest.template | Update template sizing logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/General/Shared/VectorCreateVectorTest.template | Update template sizing logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/General/Shared/VectorCreateTest.template | Update template sizing logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/General/Shared/VectorCreateElementTest.template | Update template sizing logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/General/Shared/VectorAsTest.template | Update template sizing logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/General/Shared/VectorAsNumericsVectorTest.template | Update template sizing logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/General/Shared/VectorAsNumericsTest.template | Update template sizing logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/General/Shared/VectorAllBitsSetTest.template | Update template sizing logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/Arm/Shared/SveSimpleNoOpTest.template | Update template sizing logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/Arm/Shared/SveSaturatingByActiveElementCount.template | Update template sizing/copy logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/Arm/Shared/SvePrefetchTest.template | Update template sizing logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/Arm/Shared/SveLoadVectorMaskedTest.template | Update template sizing/copy logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/Arm/Shared/SveLoadNonFaultingMaskedUnOpTest.template | Update template sizing/copy logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/Arm/Shared/ScalarBinOpRetVecTest.template | Update template sizing logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/Arm/Shared/LoadVectorx2Test.template | Update template sizing/copy logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/Arm/Shared/LoadUnOpTest.template | Update template sizing/copy logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/Arm/Shared/LoadPairVectorTest.template | Update template sizing/copy logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/Arm/Shared/Helpers.cs | Use sizeof in generic helpers and debug asserts. |
| src/tests/JIT/HardwareIntrinsics/Arm/Shared/ExtractTest.template | Update template sizing/copy logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/Arm/Shared/AesUnOpTest.template | Update template sizing/copy logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/Arm/Shared/_UnaryOpScalarTestTemplate.template | Update template sizing/copy logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/Arm/Shared/_SveUnaryOpTestTemplate.template | Update template sizing logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/Arm/Shared/_SveBinaryOpTestTemplate.template | Update template sizing logic to sizeof(...). |
| src/tests/JIT/HardwareIntrinsics/Arm/Shared/_ImmOpTestTemplate.template | Update template sizing/copy logic to sizeof(...). |
| src/tests/JIT/Directed/VectorABI/VectorMgdMgdStatic.cs | Use sizeof(Vector128<T>) for element count sizing. |
| src/tests/JIT/Directed/VectorABI/VectorMgdMgdArray.cs | Use sizeof(Vector128<T>) for element count sizing. |
| src/tests/JIT/Directed/VectorABI/VectorMgdMgd.cs | Use sizeof(Vector128<T>) for element count sizing. |
| src/tests/Interop/StructPacking/StructPacking.cs | Use sizeof(...) for ITestStructure.Size implementations. |
| src/tests/Common/CoreCLRTestLibrary/Vectors.cs | Use sizeof for vector/type size computations in test helpers. |
| src/native/managed/cdac/tests/TestInfrastructure/TestPlaceholderTarget.cs | Remove redundant unsafe block; use sizeof(T) for stackalloc sizing. |
| src/native/managed/cdac/tests/DataGenerator/TestTarget.cs | Use sizeof(T) for span sizing in read/write helpers. |
| src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs | Update remarks to reference sizeof(T). |
| src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.DbRow.cs | Use sizeof(DbRow) in invariant assert. |
| src/libraries/System.Runtime/tests/System.Runtime.CompilerServices.Unsafe.Tests/UnsafeTests.cs | Switch SizeOf test to sizeof(...) (see review comment). |
| src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector64.cs | Remove redundant unsafe block; keep sizeof(T) usage. |
| src/libraries/System.Private.CoreLib/src/System/Runtime/Intrinsics/Vector128.cs | Remove redundant unsafe block; keep sizeof(T) usage. |
| src/libraries/System.Private.CoreLib/src/System/Number.BigInteger.cs | Remove redundant unsafe block; keep sizeof(...) sizing logic. |
| src/libraries/System.Private.CoreLib/src/System/Buffers/Text/Base64Helper/Base64Helper.cs | Replace Unsafe.SizeOf with sizeof in debug bounds helpers. |
| src/libraries/System.Net.Sockets/src/System/Net/Sockets/DynamicWinsockMethods.cs | Remove redundant unsafe block around sizeof(...) arguments. |
| src/libraries/System.Net.Ping/src/System/Net/NetworkInformation/Ping.RawSocket.cs | Remove redundant unsafe block; keep sizeof(...) sizing logic. |
| src/libraries/System.Memory/tests/TestHelpers.cs | Use sizeof(...) for test buffer sizing. |
| src/libraries/System.Memory/tests/Span/Overflow.cs | Replace Unsafe.SizeOf with sizeof in test sizing logic. |
| src/libraries/System.Memory/tests/Span/EnumerateLines.cs | Use sizeof(T) for span byte-offset computations in tests. |
| src/libraries/System.Memory/tests/Span/CopyTo.cs | Replace Unsafe.SizeOf with sizeof in test sizing logic. |
| src/libraries/System.Memory/tests/ReadOnlySpan/Overflow.cs | Replace Unsafe.SizeOf with sizeof in test sizing logic. |
| src/libraries/System.Memory/tests/ReadOnlySpan/CopyTo.cs | Replace Unsafe.SizeOf with sizeof in test sizing logic. |
| src/libraries/System.Memory/tests/MemoryMarshal/CastSpan.cs | Use sizeof(EmptyStruct) in test assertion. |
| src/libraries/System.Memory/tests/Binary/BinaryReaderUnitTests.cs | Use sizeof(...) for test buffer sizing. |
| src/libraries/System.IO.Compression/src/System/IO/Compression/Zstandard/ZstandardDictionary.cs | Use sizeof(nuint) for pooled buffer sizing/casts. |
| src/libraries/System.Formats.Nrbf/src/System/Formats/Nrbf/ArraySinglePrimitiveRecord.cs | Use sizeof(T) for decoded primitive sizing. |
| src/libraries/System.Data.OleDb/src/OleDb_Util.cs | Use sizeof(ComVariant) for variant sizing constant. |
| src/libraries/Common/src/System/Security/Cryptography/PqcBlobHelpers.cs | Use sizeof(...) for blob header sizing and offsets. |
| src/installer/managed/Microsoft.NET.HostModel/MachO/BinaryFormat/LinkEditLoadCommand.cs | Remove redundant unsafe block; use sizeof(LinkEditLoadCommand). |
| src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs | Update remarks to reference sizeof(T). |
| src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.NativeAot.cs | Update remarks to reference sizeof(T). |
| .github/skills/code-review/SKILL.md | Update guidance text to refer to using the sizeof operator consistently. |
Co-authored-by: Copilot <[email protected]> Copilot-Session: 4f79da18-eb49-496c-a46d-fa89c70d9ce5
|
Not for check-in, but did you consider adding a local |
Yep, just tried to slap an [Obsolete(error=true)] on it and Libs+Clr compiled just fine |
tannergooding
left a comment
There was a problem hiding this comment.
Changes themselves LGTM. Fairly straightforward and mechanical
|
/ba-g timeouts |
Replaces codebase usages with
sizeof(T)and removes redundantunsafecontexts, while retaining directUnsafe.SizeOf<T>()API test coverage.Historically,
sizeof(T)could not be used with possibly managed types and, until recently, required anunsafecontext. The compiler now supports these uses withoutunsafe, makingsizeof(T)simpler and avoiding the additional generic/type-system overhead ofUnsafe.SizeOf<T>().Note
This pull request was generated with GitHub Copilot.