Skip to content

Consider changing assert! to debug_assert! when it calls visit_with - #53025

Merged
bors merged 1 commit into
rust-lang:masterfrom
ljedrz:debug_asserts_limited
Aug 13, 2018
Merged

Consider changing assert! to debug_assert! when it calls visit_with#53025
bors merged 1 commit into
rust-lang:masterfrom
ljedrz:debug_asserts_limited

Conversation

@ljedrz

@ljedrz ljedrz commented Aug 3, 2018

Copy link
Copy Markdown
Contributor

The perf run from #52956 revealed that there were 3 benchmarks that benefited most from changing assert!s to debug_assert!s:

I analyzed their fixing PRs and decided to look for potentially heavy assertions in the files they modified. I noticed that all of the non-trivial ones contained indirect calls to visit_with().

It might be a good idea to consider changing assert! to debug_assert! in those places in order to get the performance wins shown by the benchmarks.

@rust-highfive

Copy link
Copy Markdown
Contributor

r? @varkor

(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 Aug 3, 2018
@varkor

varkor commented Aug 4, 2018

Copy link
Copy Markdown
Contributor

Although the perf improvements are nice, I'd want to be sure that this assertions have not been hit in practice (or would definitely be caught by another, informative assertion), because assertions can often be very useful.

I'm not quite sure we'd establish this, other than reasoning about the code. Perhaps it'd be possible instead to pull these assertions further out of the loops they're in (possibly keeping the debug_assert!s where they are, but adding additional asserts that would trigger if something went wrong, potentially at a slightly less informative location).

@ljedrz

ljedrz commented Aug 5, 2018

Copy link
Copy Markdown
Contributor Author

While the general idea is nice, I'm not sure about the possibility of moving these checks outside of loops, because they aren't just repeated - as far as I can tell these calls are recursive, but the objects they are called on change along the way.

@varkor

varkor commented Aug 7, 2018

Copy link
Copy Markdown
Contributor

Okay, as these conditions are non-trivial with regards to performance and it seems likely to me that if they fail, we're going to hit other assertions (later on, but assuming we can reproduce the issue, that should be sufficient), I think it's probably fine to change these. Thanks for looking into it!

@bors r+ rollup

@bors

bors commented Aug 7, 2018

Copy link
Copy Markdown
Collaborator

📌 Commit 6bd031bc8d7a3178a44060094d74cd67aeeb491c has been approved by varkor

@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 Aug 7, 2018
@kennytm

kennytm commented Aug 10, 2018

Copy link
Copy Markdown
Member

@bors r-

Merge conflict in src/librustc/traits/select.rs.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Aug 10, 2018
@ljedrz
ljedrz force-pushed the debug_asserts_limited branch from 6bd031b to b187c42 Compare August 10, 2018 06:43
@ljedrz

ljedrz commented Aug 10, 2018

Copy link
Copy Markdown
Contributor Author

Rebased.

@kennytm kennytm added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 10, 2018
@kennytm

kennytm commented Aug 10, 2018

Copy link
Copy Markdown
Member

@bors r=varkor

@bors

bors commented Aug 10, 2018

Copy link
Copy Markdown
Collaborator

📌 Commit b187c42 has been approved by varkor

@bors bors removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 10, 2018
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Aug 12, 2018
Consider changing assert! to debug_assert! when it calls visit_with

The perf run from rust-lang#52956 revealed that there were 3 benchmarks that benefited most from changing `assert!`s to `debug_assert!`s:

- issue rust-lang#46449: avg -4.7% for -check
- deeply-nested (AKA rust-lang#38528): avg -3.4% for -check
- regression rust-lang#31157: avg -3.2% for -check

I analyzed their fixing PRs and decided to look for potentially heavy assertions in the files they modified. I noticed that all of the non-trivial ones contained indirect calls to `visit_with()`.

It might be a good idea to consider changing `assert!` to `debug_assert!` in those places in order to get the performance wins shown by the benchmarks.
bors added a commit that referenced this pull request Aug 12, 2018
Rollup of 15 pull requests

Successful merges:

 - #52955 (Update compiler test documentation)
 - #53019 (Don't collect() when size_hint is useless)
 - #53025 (Consider changing assert! to debug_assert! when it calls visit_with)
 - #53059 (Remove explicit returns where unnecessary)
 - #53165 ( Add aarch64-unknown-netbsd target)
 - #53210 (Deny future duplication of rustc-ap-syntax)
 - #53223 (A few cleanups for rustc_data_structures)
 - #53230 ([nll] enable feature(nll) on various crates for bootstrap: part 4)
 - #53231 (Add let keyword doc)
 - #53240 (Add individual documentation for <integer>`.swap_bytes`/.`reverse_bits`)
 - #53253 (Remove unwanted console log)
 - #53264 (Show that Command can be reused and remodified)
 - #53267 (Fix styles)
 - #53273 (Add links to std::char::REPLACEMENT_CHARACTER from docs.)
 - #53283 (wherein we suggest float for integer literals where a float was expected)

Failed merges:

r? @ghost
@bors
bors merged commit b187c42 into rust-lang:master Aug 13, 2018
@ljedrz
ljedrz deleted the debug_asserts_limited branch August 13, 2018 05:39
Comment thread src/librustc/ty/layout.rs
match tail.sty {
ty::TyParam(_) | ty::TyProjection(_) => {
assert!(tail.has_param_types() || tail.has_self_ty());
debug_assert!(tail.has_param_types() || tail.has_self_ty());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Checking flags on Ty is O(1), why were these changed?

Comment thread src/librustc/ty/sty.rs
-> ty::TraitRef<'tcx> {
// otherwise the escaping regions would be captured by the binder
assert!(!self_ty.has_escaping_regions());
debug_assert!(!self_ty.has_escaping_regions());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same here.

Comment thread src/librustc/ty/sty.rs
{
// otherwise the escaping regions would be captured by the binders
assert!(!self_ty.has_escaping_regions());
debug_assert!(!self_ty.has_escaping_regions());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same here.

@eddyb

eddyb commented Dec 9, 2019

Copy link
Copy Markdown
Contributor

This PR seems like it's working around TraitRef not having its own precomputed flags?
Maybe we should be interning TraitRefs, similar to Ty?

Unlike what @varkor said, I doubt many of these are correct, if any, due to how the situations they're checking for (e.g. "no inference variables") can have side-effects without causing an ICE.
E.g. inference variables can get resolved to something that the code assumed to not be there because it 1. looked for the thing explicitly 2. it asserted there are no inference variables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants