-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Use Random.Shared #25021
Use Random.Shared #25021
Conversation
Use the new Random.Shared property to remove the need to allocate a new instance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for submitting this.
For the change in ExecutionStrategy, I'm not sure this is appropriate. Random.Shared was explicitly meant for one-off random generation, to allow people to avoid managing an instance of Random themselves (see https://github.com/dotnet/runtime/pull/50297/files). But ExecutionStrategy already has this field (and uses it repeatedly, not one-off), so none of the advantages apply here (this doesn't simplify any code).
In addition, the point of Random.Shared is also to ensure thread-safety when accessed concurrently. This again isn't the case for ExecutionStrategy, which itself isn't supposed to be thread-safe. The thread-safe implementation is very likely to (slightly) degrade perf.
I do get that this reduces an allocation. However, it would be better to simply recycle ExecutionStrategy itself - along with its dependent Random instance. This is something I looked into as part of #23611, but didn't get around to doing.
However, the changes in the tests definitely do make sense to me. If you agree with the above, could you exclude the ExecutionStrategy change?
I was working on the basis it would remove the need for the allocating of a new |
@martincostello yeah, reducing the allocation does make sense. But as I added above:
So I think it's better to revert the ExecutionStrategy change, and recycle the entire object in a separate, later effort. |
Ah sorry, I must have skipped over that sentence. Sure, I'll take that part out of the PR then. |
Revert change as suggested in PR feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @martincostello (FWIW you didn't skip over the sentence - I edited and added it right after posting, but you were faster reading :)).
Hello @roji! Because this pull request has the p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (
|
Use the new
Random.Shared
property to remove the need to allocate a new instance when targetingnet6.0
.