Skip to content
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

Merged
merged 2 commits into from
Jun 5, 2021
Merged

Use Random.Shared #25021

merged 2 commits into from
Jun 5, 2021

Conversation

martincostello
Copy link
Member

Use the new Random.Shared property to remove the need to allocate a new instance when targeting net6.0.

Use the new Random.Shared property to remove the need to allocate a new instance.
Copy link
Member

@roji roji left a 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?

@martincostello
Copy link
Member Author

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).

I was working on the basis it would remove the need for the allocating of a new Random for each instance of an execution strategy and just use the shared one (albeit it being a minor saving), but I'm happy to revert that part if required.

@roji
Copy link
Member

roji commented Jun 5, 2021

@martincostello yeah, reducing the allocation does make sense. But as I added above:

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.

So I think it's better to revert the ExecutionStrategy change, and recycle the entire object in a separate, later effort.

@martincostello
Copy link
Member Author

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

@roji roji left a 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 :)).

@ghost
Copy link

ghost commented Jun 5, 2021

Hello @roji!

Because this pull request has the auto-merge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

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 (@msftbot) and give me an instruction to get started! Learn more here.

@roji roji removed the auto-merge label Jun 5, 2021
@roji roji merged commit 92bae0a into dotnet:main Jun 5, 2021
@martincostello martincostello deleted the Use-Random-Shared branch June 5, 2021 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants