Skip to content

Optimise from_rng & try_from_rng methods for RapidRng - #2

Open
Bluefinger wants to merge 1 commit into
hoxxep:mainfrom
Bluefinger:optimise-from-rng
Open

Optimise from_rng & try_from_rng methods for RapidRng#2
Bluefinger wants to merge 1 commit into
hoxxep:mainfrom
Bluefinger:optimise-from-rng

Conversation

@Bluefinger

Copy link
Copy Markdown

The default method implementations for SeedableRng use fill_bytes which works well as a default, but is not the optimal path for a PRNG like RapidRng. fill_bytes works well for larger buffer sizes, but for a smaller state, it doesn't optimise well as [u8; 8] lacks alignment information that u64 has. Thankfully, next_u64() can be used to directly create the RapidRng state, bypassing a lot of fill_bytes machinery and being a lot easier for the compiler to optimise.

Comment thread rapidrand/src/lib.rs
}

fn try_from_rng<R: TryRng + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
match rng.try_next_u64() {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This is just unsugared ?, mostly done this way to optimise dev compile times to keep them fast. For release mode, it all gets optimised pretty well anyway and makes small difference to compile speed. Runtime speed should be the same.

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.

1 participant