Skip to content

Replace Unsafe.SizeOf with sizeof - #130727

Merged
EgorBo merged 2 commits into
dotnet:mainfrom
EgorBo:replace-unsafe-sizeof
Jul 15, 2026
Merged

EgorBo merged 2 commits into
dotnet:mainfrom
EgorBo:replace-unsafe-sizeof

Conversation

@EgorBo

@EgorBo EgorBo commented Jul 14, 2026 •

Copy link
Copy Markdown
Member

Replaces codebase usages with sizeof(T) and removes redundant unsafe contexts, while retaining direct Unsafe.SizeOf<T>() API test coverage.

Historically, sizeof(T) could not be used with possibly managed types and, until recently, required an unsafe context. The compiler now supports these uses without unsafe, making sizeof(T) simpler and avoiding the additional generic/type-system overhead of Unsafe.SizeOf<T>().

Note

This pull request was generated with GitHub Copilot.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 4f79da18-eb49-496c-a46d-fa89c70d9ce5
@azure-pipelines

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-runtime-intrinsics
See info in area-owners.md if you want to be subscribed.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>() with sizeof(T) / sizeof(SomeType) across tools, libraries, and tests.
  • Remove redundant unsafe statement blocks where they only wrapped sizeof(...).
  • Update related documentation/comments to reference sizeof(T) instead of Unsafe.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
Copilot AI review requested due to automatic review settings July 14, 2026 21:04
@tannergooding

Copy link
Copy Markdown
Member

Not for check-in, but did you consider adding a local [Obsolete] on Unsafe.SizeOf to help validate we really did get everything?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 219 out of 252 changed files in this pull request and generated 16 comments.

@EgorBo

EgorBo commented Jul 14, 2026

Copy link
Copy Markdown
Member Author

Not for check-in, but did you consider adding a local [Obsolete] on Unsafe.SizeOf to help validate we really did get everything?

Yep, just tried to slap an [Obsolete(error=true)] on it and Libs+Clr compiled just fine

@tannergooding tannergooding left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes themselves LGTM. Fairly straightforward and mechanical

@EgorBo

EgorBo commented Jul 15, 2026

Copy link
Copy Markdown
Member Author

/ba-g timeouts

@EgorBo
EgorBo merged commit a77821a into dotnet:main Jul 15, 2026
201 of 208 checks passed
@EgorBo
EgorBo deleted the replace-unsafe-sizeof branch July 15, 2026 10:12
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview7 milestone Jul 16, 2026
@github-actions github-actions Bot locked and limited conversation to collaborators Aug 16, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants