-
Notifications
You must be signed in to change notification settings - Fork 431
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
Optimise Rng::gen for arrays #1282
Comments
If we choose option 1 we should document that the methods will produce different results |
This documents speed (but not value differences): https://docs.rs/rand/latest/rand/trait.Rng.html#arrays-and-tuples Yes, looks like docs should be better. |
I don't know if my opinion is in anyway relevant here (not really involved in the project) but if it is I probably agree that 1 is the best option, keeping the flexibility that |
Summary
Currently,
rng.gen::<[u8; 4]>()
will invoke the RNG four times. In contrast,rng.fill(&mut array)
would do so once. Ideally we should optimise the former to do the same as the latter.Details
Fill
only supports arrays and slices over a few types:bool, char, f32, f64
: these invokerng.gen()
for each element (wasteful forbool
, but required for the rest)u8
: straight byte copyto_le
(for portability)rng.gen()
does not support slices (unsized) but supports arrays over anything supported by theStandard
distribution.We could:
rng.fill(&mut array)
is not equivalent toarray = rng.gen()
.Fill
method where possible. But who knows when Rust will support this.rng.gen()
to types supported byFill
(API breaking change). We can try to support enough types to satisfy most users, but cannot support user-defined types.If we choose (3), we could extend this such that both methods support all generable element types once Specialization is stable.
Motivation
The text was updated successfully, but these errors were encountered: