Skip to content

Conversation

BGR360
Copy link
Contributor

@BGR360 BGR360 commented Feb 20, 2022

This replaces rand with fastrand as the source of randomness for HandleId::new() in bevy_asset. This was the only crate with a dependency on rand, and now the dependency exists only as a dev-dependency.

fastrand was already in the dependency tree, thanks to futures-lite, async-executor, and tempfile to name a few.

Changelog

Removed rand from dependencies in bevy_asset in favor of existing in-tree fast-rand

This replaces `rand` with `fastrand` as the source of randomness for
`HandleId::new()` in `bevy_asset`. This was the only crate with a dependency
on `rand`, and now the dependency exists only as a dev-dependency.

`fastrand` was already in the dependency tree, thanks to `futures-lite`,
`async-executor`, and `tempfile` to name a few.
@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Feb 20, 2022
@BGR360
Copy link
Contributor Author

BGR360 commented Feb 20, 2022

It's worth discussing if this potentially degrades the uniqueness of HandleId::random(), but if it does, perhaps this issue is the proper solution: #3993

@alice-i-cecile alice-i-cecile added A-Assets Load files from disk to use for things like images, models, and sounds C-Performance A change motivated by improving speed, memory usage or compile times and removed S-Needs-Triage This issue needs to be labelled labels Feb 20, 2022
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

LGTM. I'm idly curious about the performance difference, but I don't think it would change my mind; stripping out heavy dependencies is generally good.

@bjorn3
Copy link
Contributor

bjorn3 commented Feb 20, 2022

Fastrand uses wyhash as prng and seeds with time and pid. Rand uses a csprng for rand::random() and seeds it with os randomness. I think we should still use rand for randomness exposed to the user as fastrand is rather predictable especially due to the bad seeding.

@NiklasEi
Copy link
Member

As long as the IDs are unique, is more predictability actually something bad for asset handles?

@bjorn3
Copy link
Contributor

bjorn3 commented Feb 20, 2022

For asset ids no. However for sources of randomness directly used by the game more predictability is a bad thing. While we can move bevy_assets to fastrand, whatever crate will expose a source of randomness to the user will still need to use rand.

@BGR360
Copy link
Contributor Author

BGR360 commented Feb 20, 2022

whatever crate will expose a source of randomness to the user will still need to use rand

For the record, there is currently no such crate in bevy, as far as i could see.

@bjorn3
Copy link
Contributor

bjorn3 commented Feb 20, 2022

It has been proposed but not (yet) accepted: #2504

@mcobzarenco
Copy link
Contributor

mcobzarenco commented Feb 21, 2022

I would like to understand what problem this PR solves as it's not covered in the description.

Unless there's a reason, I am opposed to this change as rand is a super widely used crate, widely contributed to (check out contributors in both projects) and using OS as a source of randomness vs. a homebrew solution "wyhash as prng and seeds with time and pid." seems preferable.

@BGR360
Copy link
Contributor Author

BGR360 commented Feb 21, 2022

The problem being solved is reducing unnecessary dependencies. I was under the impression that this was one of the core tenets of the Bevy philosophy.

The rand crate is self-proclaimed as not being a lightweight dependency.

Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

@alice-i-cecile A long time ago I actually did a benchmark between a few RNG crates. It might very well be wrong but for me fastrand indeed turned out to be the fastest: https://gist.github.com/r00ster91/817f4691b7d240c5908c64bde6bb1f7f (needs nightly)

[src/main.rs:59] std_rng_duration = 12.914µs
[src/main.rs:59] micro_rand_duration = 8.526µs
[src/main.rs:59] nanorand_duration = 5.6µs
[src/main.rs:59] oorandom_duration = 4.769µs
[src/main.rs:59] small_rng_duration = 2.034µs
[src/main.rs:59] fastrand_duration = 1.854µs

(Output manually sorted)
That's an improvements of >85%!

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Apr 22, 2022
@alice-i-cecile
Copy link
Member

@BGR360 can you retitle this to "Remove rand crate from dependency tree"?

And add the following to your PR description

## Changelog

Removed `rand` from dependencies in `bevy_asset` in favor of existing in-tree `fast-rand`

Once that's done I'm happy to merge this.

@BGR360 BGR360 changed the title Yeet rand Remove rand crate from dependency tree Apr 25, 2022
@BGR360
Copy link
Contributor Author

BGR360 commented Apr 25, 2022

@alice-i-cecile done!

@alice-i-cecile
Copy link
Member

Blocking this for now: as @TheRawMeatball points out, can we just use a global atomic rather than a random UUID? That would be my preferred solution if possible.

@BGR360
Copy link
Contributor Author

BGR360 commented Apr 25, 2022

Sorry, where did @TheRawMeatball point this out?

@alice-i-cecile
Copy link
Member

On Discord a couple minutes ago, in #engine-dev. I just wanted to give credit where it's due :)

Ongoing discussion in this thread: https://discord.com/channels/691052431525675048/968230879317078116

@alice-i-cecile alice-i-cecile removed the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Apr 26, 2022
@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Jul 14, 2022
@alice-i-cecile
Copy link
Member

This is clearly correct, and fixes #1376, which is in the milestone.

Merging now, we can hammer out the broader questions on "should we be using global atomics" after.

@alice-i-cecile
Copy link
Member

bors r+

bors bot pushed a commit that referenced this pull request Jul 14, 2022
This replaces `rand` with `fastrand` as the source of randomness for `HandleId::new()` in `bevy_asset`. This was the only crate with a dependency on `rand`, and now the dependency exists only as a dev-dependency.

`fastrand` was already in the dependency tree, thanks to `futures-lite`, `async-executor`, and `tempfile` to name a few.

## Changelog

Removed `rand` from dependencies in `bevy_asset` in favor of existing in-tree `fast-rand`
@bors bors bot changed the title Remove rand crate from dependency tree [Merged by Bors] - Remove rand crate from dependency tree Jul 14, 2022
@bors bors bot closed this Jul 14, 2022
inodentry pushed a commit to IyesGames/bevy that referenced this pull request Aug 8, 2022
This replaces `rand` with `fastrand` as the source of randomness for `HandleId::new()` in `bevy_asset`. This was the only crate with a dependency on `rand`, and now the dependency exists only as a dev-dependency.

`fastrand` was already in the dependency tree, thanks to `futures-lite`, `async-executor`, and `tempfile` to name a few.

## Changelog

Removed `rand` from dependencies in `bevy_asset` in favor of existing in-tree `fast-rand`
james7132 pushed a commit to james7132/bevy that referenced this pull request Oct 28, 2022
This replaces `rand` with `fastrand` as the source of randomness for `HandleId::new()` in `bevy_asset`. This was the only crate with a dependency on `rand`, and now the dependency exists only as a dev-dependency.

`fastrand` was already in the dependency tree, thanks to `futures-lite`, `async-executor`, and `tempfile` to name a few.

## Changelog

Removed `rand` from dependencies in `bevy_asset` in favor of existing in-tree `fast-rand`
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
This replaces `rand` with `fastrand` as the source of randomness for `HandleId::new()` in `bevy_asset`. This was the only crate with a dependency on `rand`, and now the dependency exists only as a dev-dependency.

`fastrand` was already in the dependency tree, thanks to `futures-lite`, `async-executor`, and `tempfile` to name a few.

## Changelog

Removed `rand` from dependencies in `bevy_asset` in favor of existing in-tree `fast-rand`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Assets Load files from disk to use for things like images, models, and sounds C-Performance A change motivated by improving speed, memory usage or compile times S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants