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

Avoid using rand::thread_rng in the stdlib benchmarks. #96626

Merged
merged 1 commit into from
May 5, 2022

Conversation

thomcc
Copy link
Member

@thomcc thomcc commented May 2, 2022

This is kind of an anti-pattern because it introduces extra nondeterminism for no real reason. In thread_rng's case this comes both from the random seed and also from the reseeding operations it does, which occasionally does syscalls (which adds additional nondeterminism). The impact of this would be pretty small in most cases, but it's a good practice to avoid (particularly because avoiding it was not hard).

Anyway, several of our benchmarks already did the right thing here anyway, so the change was pretty easy and mostly just applying it more universally. That said, the stdlib benchmarks aren't particularly stable (nor is our benchmark framework particularly great), so arguably this doesn't matter that much in practice.

Anyway, this also bumps the rand dev-dependency to 0.8, since it had fallen somewhat out of date. Nevermind, too much of a headache.

@rustbot rustbot added the T-libs Relevant to the library team, which will review and decide on the PR/issue. label May 2, 2022
@rust-highfive
Copy link
Collaborator

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with r? rust-lang/libs-api @rustbot label +T-libs-api -T-libs to request review from a libs-api team reviewer. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@rust-highfive
Copy link
Collaborator

r? @m-ou-se

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 2, 2022
@klensy
Copy link
Contributor

klensy commented May 2, 2022

There was previous try to update rand in #86963, so maybe you hit the same errors on the path.

@rust-log-analyzer

This comment has been minimized.

@thomcc
Copy link
Member Author

thomcc commented May 2, 2022

I also ran x test from the wrong rust-lang/rust checkout, so there's, uh, other issues. (Should be good now)

@thomcc
Copy link
Member Author

thomcc commented May 2, 2022

There was previous try to update rand in #86963, so maybe you hit the same errors on the path.

I'm willing to revert that patch if it causes trouble, though I think it should be fine -- this is only an update of the dev-dependencies in the stdlib, and doesn't touch rand anywhere else in the compiler, which seems like a bit of a quagmire. My feeling here his also matches the comment in #86963 (comment) (emph mine):

Generally speaking updates to dependencies of the compiler or standard library that aren't just used in tests need some scrutiny for potential breakage caused by their new implementations...

But yeah, I don't mind reverting the first patch in this PR (which does the update) either.

@thomcc
Copy link
Member Author

thomcc commented May 2, 2022

Ah, I see. I suppose this may break the stdlib tests on wasm. Hmm, I'll see if I can check locally. I suspect that we don't actually need high quality randomness in the stdlib's tests (they're mostly for generating random data to test against), although we could just seed an rng from entropy from the sys module too, perhaps.

@thomcc
Copy link
Member Author

thomcc commented May 2, 2022

Ah.

error: target is not supported, for more information see: https://docs.rs/getrandom/#unsupported-targets
   --> /Users/thom/.cargo/registry/src/git.luolix.top-1ecc6299db9ec823/getrandom-0.2.0/src/lib.rs:224:9
    |
224 | /         compile_error!("target is not supported, for more information see: \
225 | |                         https://docs.rs/getrandom/#unsupported-targets");
    | |________________________________________________________________________^

That's quite a headache, because I don't think it'd be good behavior for stdlib to enable getrandom's custom entropy functionality -- I think if a user on resolver=1 was using -Zbuild-std, this would enable that feature for them. Perhaps that's fine, but it seems at least kind of dubious. (My gut feeling is to say the issues here mean we should move away from use rand in the stdlib's tests, since we don't really need it, but... I don't feel that strongly about it and wouldn't do it in this PR anyway).

Either way, given that updating the version of rand wasn't the goal of this PR (I just did it because I was in the neighborhood), I've removed that patch from the PR.

@m-ou-se
Copy link
Member

m-ou-se commented May 2, 2022

@bors r+

@bors
Copy link
Contributor

bors commented May 2, 2022

📌 Commit 0812759 has been approved by m-ou-se

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 2, 2022
@bors
Copy link
Contributor

bors commented May 5, 2022

⌛ Testing commit 0812759 with merge 12d3f10...

@bors
Copy link
Contributor

bors commented May 5, 2022

☀️ Test successful - checks-actions
Approved by: m-ou-se
Pushing 12d3f10 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label May 5, 2022
@bors bors merged commit 12d3f10 into rust-lang:master May 5, 2022
@rustbot rustbot added this to the 1.62.0 milestone May 5, 2022
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (12d3f10): comparison url.

Summary: This benchmark run did not return any relevant results.

If you disagree with this performance assessment, please file an issue in rust-lang/rustc-perf.

@rustbot label: -perf-regression

bors added a commit to rust-lang-ci/rust that referenced this pull request Jan 8, 2023
…, r=Mark-Simulacrum

Update `rand` in the stdlib tests, and remove the `getrandom` feature from it.

The main goal is actually removing `getrandom`, so that eventually we can allow running the stdlib test suite on tier3 targets which don't have `getrandom` support. Currently those targets can only run the subset of stdlib tests that exist in uitests, and (generally speaking), we prefer not to test libstd functionality in uitests, which came up recently in rust-lang#104095 and rust-lang#104185. Additionally, the fact that we can't update `rand`/`getrandom` means we're stuck with the old set of tier3 targets, so can't test new ones.

~~Anyway, I haven't checked that this actually does allow use on tier3 targets (I think it does not, as some work is needed in stdlib submodules) but it moves us slightly closer to this, and seems to allow at least finally updating our `rand` dep, which definitely improves the status quo.~~ Checked and works now.

For the most part, our tests and benchmarks are fine using hard-coded seeds. A couple tests seem to fail with this (stuff manipulating the environment expecting no collisions, for example), or become pointless (all inputs to a function become equivalent). In these cases I've done a (gross) dance (ab)using `RandomState` and `Location::caller()` for some extra "entropy".

Trying to share that code seems *way* more painful than it's worth given that the duplication is a 7-line function, even if the lines are quite gross. (Keeping in mind that sharing it would require adding `rand` as a non-dev dep to std, and exposing a type from it publicly, all of which sounds truly awful, even if done behind a perma-unstable feature).

See also some previous attempts:
- rust-lang#86963 (in particular rust-lang#86963 (comment) which explains why this is non-trivial)
- rust-lang#89131
- rust-lang#96626 (comment) (I tried in that PR at the same time, but settled for just removing the usage of `thread_rng()` from the benchmarks, since that was the main goal).
- rust-lang#104185
- Probably more. It's very tempting of a thing to "just update".

r? `@Mark-Simulacrum`
thomcc pushed a commit to tcdi/postgrestd that referenced this pull request May 31, 2023
…Simulacrum

Update `rand` in the stdlib tests, and remove the `getrandom` feature from it.

The main goal is actually removing `getrandom`, so that eventually we can allow running the stdlib test suite on tier3 targets which don't have `getrandom` support. Currently those targets can only run the subset of stdlib tests that exist in uitests, and (generally speaking), we prefer not to test libstd functionality in uitests, which came up recently in rust-lang/rust#104095 and rust-lang/rust#104185. Additionally, the fact that we can't update `rand`/`getrandom` means we're stuck with the old set of tier3 targets, so can't test new ones.

~~Anyway, I haven't checked that this actually does allow use on tier3 targets (I think it does not, as some work is needed in stdlib submodules) but it moves us slightly closer to this, and seems to allow at least finally updating our `rand` dep, which definitely improves the status quo.~~ Checked and works now.

For the most part, our tests and benchmarks are fine using hard-coded seeds. A couple tests seem to fail with this (stuff manipulating the environment expecting no collisions, for example), or become pointless (all inputs to a function become equivalent). In these cases I've done a (gross) dance (ab)using `RandomState` and `Location::caller()` for some extra "entropy".

Trying to share that code seems *way* more painful than it's worth given that the duplication is a 7-line function, even if the lines are quite gross. (Keeping in mind that sharing it would require adding `rand` as a non-dev dep to std, and exposing a type from it publicly, all of which sounds truly awful, even if done behind a perma-unstable feature).

See also some previous attempts:
- rust-lang/rust#86963 (in particular rust-lang/rust#86963 (comment) which explains why this is non-trivial)
- rust-lang/rust#89131
- rust-lang/rust#96626 (comment) (I tried in that PR at the same time, but settled for just removing the usage of `thread_rng()` from the benchmarks, since that was the main goal).
- rust-lang/rust#104185
- Probably more. It's very tempting of a thing to "just update".

r? `@Mark-Simulacrum`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants