Draft
Conversation
Rewrite the bulk and tail byte writes in the xoshiro Random implementations to use BitConverter and span copies instead of unsafe pointer access or MemoryMarshal.Write. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-runtime |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the Xoshiro-based Random.NextBytes implementations in System.Private.CoreLib to avoid unsafe code by switching bulk and tail writes to BitConverter.TryWriteBytes.
Changes:
- Replace
MemoryMarshal.Writebulk writes withBitConverter.TryWriteBytesand addDebug.Assertfor the expected success path. - Replace unsafe tail byte-copy logic with a scratch span plus
TryWriteBytes, removing the need forunsafe. - Remove the
System.Runtime.InteropServicesusing directives made unnecessary by the refactor.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/Random.Xoshiro256StarStarImpl.cs | Refactors NextBytes(Span<byte>) to use TryWriteBytes for bulk and tail writes, removing unsafe pointer usage. |
| src/libraries/System.Private.CoreLib/src/System/Random.Xoshiro128StarStarImpl.cs | Same refactor pattern for the 32-bit Xoshiro implementation’s NextBytes(Span<byte>). |
Member
Author
This was referenced Apr 25, 2026
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.
Note
This PR description was generated by GitHub Copilot.
Summary
Random.NextBytesbulk writes to useBitConverter.TryWriteBytesBitConverter.TryWriteBytesunsafemethod declarations andMemoryMarshalusing directivesValidation
build.cmd clr.corelib+clr.nativecorelib+libs.pretest -rc checkeddotnet.cmd build /t:test src\libraries\System.Runtime\tests\System.Runtime.Extensions.Tests\System.Runtime.Extensions.Tests.csproj -p:XUnitClassName=System.Tests.RandomTests -p:TargetFramework=net11.0-windows -v:minimal(121 tests, 0 failed)