-
Notifications
You must be signed in to change notification settings - Fork 5
Eliminate heap allocations in Random/RandomNumberGenerator polyfills #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
0a92dcf
62d1c91
f988e73
b84a204
55e43c1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| // ReSharper disable PartialTypeWithSinglePart | ||
|
|
||
| using System; | ||
| using System.Buffers; | ||
| using System.Diagnostics.CodeAnalysis; | ||
|
|
||
| [ExcludeFromCodeCoverage] | ||
|
|
@@ -16,9 +17,19 @@ internal static class MemberPolyfills_NetCore21_Random | |
| // https://learn.microsoft.com/dotnet/api/system.random.nextbytes#system-random-nextbytes(system-span((system-byte))) | ||
| public void NextBytes(Span<byte> buffer) | ||
| { | ||
| var bufferArray = buffer.ToArray(); | ||
| random.NextBytes(bufferArray); | ||
| bufferArray.CopyTo(buffer); | ||
| if (buffer.IsEmpty) | ||
| return; | ||
|
|
||
| var bufferArray = ArrayPool<byte>.Shared.Rent(buffer.Length); | ||
| try | ||
| { | ||
| random.NextBytes(bufferArray); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can populate more than necessary
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in b84a204 — reverted to |
||
| bufferArray.AsSpan(0, buffer.Length).CopyTo(buffer); | ||
| } | ||
| finally | ||
| { | ||
| ArrayPool<byte>.Shared.Return(bufferArray); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.