-
Notifications
You must be signed in to change notification settings - Fork 7
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
Consider adding RandomState::with_seed
#1
Comments
Your concern is legitimate and I have ran into this myself as well, unfortunately I don't currently see a zero-cost way of making it happen. To reduce the size of hash maps Line 57 in 92a8df9
We could sacrifice one bit of the I could expand the |
The size rationale makes sense to me as this is a hashmap-optimized hasher. Sacrificing one bit of the instance's seed sounds reasonable at first glance. It could be possible to massage that (predictable) branch to a conditional move. Maybe 3 more instructions in total. let mut global_seed = &rand_global_seed;
if self.per_hasher_seed & 1 == 1 {
global_seed = &fixed_global_seed;
}
FoldHasher::with_seed(self.per_hasher_seed, global_seed.get()) |
Unfortunately that is not acceptable. The current view_assembly:
mov rax, qword ptr [rip + example::seed::GLOBAL_SEED_STORAGE::ha8aed685c422c912@GOTPCREL]
xor rdi, qword ptr [rsi]
mov rdx, rdi
mulx rax, rcx, qword ptr [rax]
xor rax, rcx
ret |
It seems that storing a reference in view_assembly:
mov rax, qword ptr [rsi]
xor rdi, qword ptr [rsi + 8]
mov rdx, rdi
mulx rax, rcx, qword ptr [rax]
xor rax, rcx
ret So the question is just whether this feature is worth increasing the size of |
Yeah, I think so, too. It makes it more practical, in my experience. You could even have a SmallRandomState variant that optimizes for size (but generates more code), probably more useful than FixedState. |
Just a feedback. I was also looking for |
Ditto on this issue. In our case, we want to pass a seed if we detect an environment variable in order to do deterministic regression testing, otherwise the normal random state is preferred. Great work on this crate, btw! |
I'd also love to see a with_seed method, or a distinct SeededRandomState struct with the feature. My use case is that I'm building fully static/const hash maps and sets. For that use case, I need to hash the keys at compile time to build the collections and then hash again at runtime in order to do lookups. I want customers of my collections to have different random seeds from customer to customer, and indeed from build to build in order to provide effective HashDoS protection. At the moment, I use ahash for this feature. I pick some seeds during build (via a build script or procedural macros) and burn those into the binary so I use the same seeds at runtime. |
Sometimes it's useful to create a deterministically seeded
RandomState
.Even if
FixedState
exists, it's a different type, so the user must choose one or another at the type level instead of creation time. I suppose there are uses for both patterns.The text was updated successfully, but these errors were encountered: