Remove unsafe code from BigInteger.TryWriteBigEndian and TryWriteLittleEndian#124858
Draft
Remove unsafe code from BigInteger.TryWriteBigEndian and TryWriteLittleEndian#124858
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/runtime-infrastructure |
… BigInteger Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
…ndly loops Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Remove unsafe code from TryWrite methods in BigInteger
Remove unsafe code from BigInteger.TryWriteBigEndian and TryWriteLittleEndian
Feb 25, 2026
jkotas
reviewed
Feb 25, 2026
src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs
Outdated
Show resolved
Hide resolved
…/LittleEndian Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
EgorBo
reviewed
Feb 25, 2026
src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs
Outdated
Show resolved
Hide resolved
…m index Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
Member
|
@copilot please create microbenchmarks using the performance-benchmarks skill to validate performance of these methods (different values, lengths, etc). |
This comment was marked as outdated.
This comment was marked as outdated.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors TryWriteBigEndian and TryWriteLittleEndian methods in BigInteger to replace unsafe code patterns (Unsafe, MemoryMarshal.Cast, BitConverter) with BinaryPrimitives APIs and use forward loops throughout instead of reversed loops.
Changes:
- Replaced pointer-based writing with
BinaryPrimitives.WriteInt32BigEndian/LittleEndianandBinaryPrimitives.WriteUInt32BigEndian/LittleEndian - Restructured control flow with early return pattern for insufficient destination buffer
- Simplified loop structures to use forward iteration with offset calculations for reverse ordering where needed
src/libraries/System.Runtime.Numerics/src/System/Numerics/BigInteger.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: EgorBo <523221+EgorBo@users.noreply.github.com>
This was referenced Feb 25, 2026
This comment was marked as resolved.
This comment was marked as resolved.
1 similar comment
This comment was marked as resolved.
This comment was marked as resolved.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace
Unsafe.WriteUnaligned/pointer-arithmetic patterns inBigInteger.TryWriteBigEndianandTryWriteLittleEndianwith safe, idiomatic equivalents usingBinaryPrimitives.Description
bits is nullpath — replace manual endianness-conditionalUnsafe.WriteUnalignedwithBinaryPrimitives:_bitspositive path — replaceref bytepointer walking withBinaryPrimitives.WriteUInt32BigEndian/WriteUInt32LittleEndianusing forward loops. For big endian, wordi(LSW-first) is written atdestination.Slice((bits.Length - 1 - i) * sizeof(uint)):_bitsnegative path — for little endian, a slidingSpan<byte>advancing bysizeof(uint)each iteration is used. For big endian, the write offset is computed directly from the loop index asbyteCount - (i + 1) * sizeof(uint), avoiding a decrementing offset variable. The original do-while/while carry propagation structure is preserved (avoids per-iteration carry branch after carry stops propagating):Sign-extension detection — replace
Unsafe.AreSamepointer comparison with a simple integer check:Structural improvements:
Slice(offset)without explicit length to minimize redundant bounds checking (BinaryPrimitives validates length ≥ 4 internally)Unsafe,MemoryMarshal.Cast, andBitConverterare no longer used in either method. All loops are forward loops. All existing tests (2647) pass.💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.