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

Add a deterministic constructor for RandomState #135578

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bkragl
Copy link

@bkragl bkragl commented Jan 16, 2025

RandomState is the default hasher for std::collections::{HashMap, HashSet}. Today, the only way to construct a RandomState is via RandomState:new(), which generates fresh random keys on every call. That's a good default for security, but it makes testing challenging, because test failures might not be reproducible due to nondeterministic behavior.

Although in principle it is possible to use other deterministic hashers, there are libraries which have hash collections with RandomState hardcoded in their public interface. For example, see aws_sdk_dynamodb (and this discussion).

This change adds a new constructor for RandomState that returns a fixed value, which makes it possible for programs to behave deterministically even if they are locked in to RandomState for hashing.

ACP: rust-lang/libs-team#523

@rustbot
Copy link
Collaborator

rustbot commented Jan 16, 2025

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @jhpratt (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 16, 2025
@rust-log-analyzer

This comment has been minimized.

@tbu-
Copy link
Contributor

tbu- commented Jan 16, 2025

RandomState is the default hasher for std::collections::{HashMap, HashSet}. Today, the only way to construct a RandomState is via RandomState:new(), which generates fresh random keys on every call. That's a good default for security, but it makes testing challenging, because test failures might not be reproducible due to nondeterministic behavior.

Can you give an example where this appeared as a problem?

@bkragl bkragl force-pushed the deterministic_random_state branch from d6af4c9 to ee7f783 Compare January 16, 2025 16:16
@Noratrieb
Copy link
Member

Thank you for the PR! This PR contains a public library API change, which should follow the proper process of creating an ACP first: https://std-dev-guide.rust-lang.org/development/feature-lifecycle.html
Use @rustbot ready when your ACP has been accepeted.
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 16, 2025
@bkragl
Copy link
Author

bkragl commented Jan 19, 2025

Thanks for pointing me to the ACP process. I've opened rust-lang/libs-team#523.

(Side note: It would be awesome to have a master flow chart that guides through the process for any possible Rust contribution scenario. I missed the ACP process because it wasn't mentioned in the "Library Tracking Issue" template.)

@LFS6502 LFS6502 added the S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. label Mar 1, 2025
@Dylan-DPC Dylan-DPC removed the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Mar 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-ACP Status: PR has an ACP and is waiting for the ACP to complete. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants