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 (random) seed api #22

Merged
merged 4 commits into from
Jan 17, 2024
Merged

Conversation

WaffleLapkin
Copy link
Member

tl;dr: this PR adds the following APIs:

impl FxHasher {
    pub fn with_seed(seed: usize) -> FxHasher;
}

// Everything below is under cfg(feature = "rand")

pub struct FxRandomState { ... }

impl FxRandomState {
    pub fn new() -> FxRandomState;
}

impl BuildHasher for FxRandomState { ... }

This adds ways to create FxHasher with a custom or random seed.

Resolves #14
Resolves #15

Helps with #17, though different default seed may still be a good idea.

@PureWhiteWu
Copy link

Hi, is there any plan for this pr to be merged?
I think this is quite important.

src/lib.rs Outdated Show resolved Hide resolved
Copy link
Member

@Noratrieb Noratrieb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some tests for this?
One useful test would be to create two FxRandomState and ensure their seeds aren't the same.
Another possible test would be creating two FxRandomState on two different threads and ensuring their seeds aren't the same (this is not guaranteed to always pass, but should in practice always pass).
Another useful test would be for FxHasher::with_seed to use it with two different seeds (and the default) and ensure that hashing some integer with it produces different results in all cases.

@WaffleLapkin
Copy link
Member Author

WaffleLapkin commented Jan 17, 2024

@Nilstrieb are the tests I added match what you asked for?

I also added a BuildHasher impl you can provide a seed for, since there wasn't a way to do this without making your own type... Does it look reasonable to you? (I can drop the commit if you think it's unnecessary and writing your own hash builder is expected way of using set seeds)

Copy link
Member

@Noratrieb Noratrieb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me after style nit (also on the re-exports)

extern crate rand;

#[cfg(feature = "rand")]
mod random_state;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a newline between the modules to make it more obvious that only the former is cfged?

This commit adds an optional dependency on `rand` (behind a `rand`)
feature that allows seeding `FxHasher` with random seeds.

This is done via `FxRandomState` that implemented similar to
`std::collections::hash_map::RandomState`.

`FxHashMapRand` and `FxHashSetRand` are also introduced as type aliases
to `HashMap` and `HashSet` with `S = FxRandomState`.
@WaffleLapkin WaffleLapkin merged commit 3e580ce into rust-lang:master Jan 17, 2024
3 checks passed
@WaffleLapkin
Copy link
Member Author

Uuuuhhhhh is the master branch not protected? D:

@Noratrieb
Copy link
Member

I think it is, but I approved your PR, and GitHub does not dismiss approvals after pushing

@WaffleLapkin WaffleLapkin deleted the seed_api branch January 18, 2024 21:07
@WaffleLapkin
Copy link
Member Author

@Nilstrieb I merged the PR before CI passed (or at least it looked like I did, maybe it happened at the same time?). also fyi github has an option to dismiss approvals after pushing

@Noratrieb
Copy link
Member

interesting.. i wouldn't wanna dismiss approvals, that's just annoying, but if it's not gated on CI that would be a bit bad. not that it matters too much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide public API for initial seed of FxHasher Slow deserialization compared to stock HashMap
3 participants