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

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

Merged
merged 1 commit into from
Aug 13, 2018

Conversation

ljedrz
Copy link
Contributor

@ljedrz ljedrz commented Aug 3, 2018

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
Collaborator

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
Copy link
Member

varkor commented Aug 4, 2018

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
Copy link
Contributor Author

ljedrz commented Aug 5, 2018

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
Copy link
Member

varkor commented Aug 7, 2018

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
Copy link
Contributor

bors commented Aug 7, 2018

📌 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
Copy link
Member

kennytm commented Aug 10, 2018

@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
Copy link
Contributor Author

ljedrz commented Aug 10, 2018

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
Copy link
Member

kennytm commented Aug 10, 2018

@bors r=varkor

@bors
Copy link
Contributor

bors commented Aug 10, 2018

📌 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
@@ -1300,7 +1300,7 @@ impl<'a, 'tcx> SizeSkeleton<'tcx> {
let tail = tcx.struct_tail(pointee);
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
Member

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?

@@ -708,7 +708,7 @@ impl<'a, 'gcx, 'tcx> ExistentialTraitRef<'tcx> {
pub fn with_self_ty(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>, self_ty: Ty<'tcx>)
-> 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
Member

Choose a reason for hiding this comment

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

Same here.

@@ -1247,7 +1247,7 @@ impl<'a, 'tcx, 'gcx> ExistentialProjection<'tcx> {
-> ty::ProjectionPredicate<'tcx>
{
// 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
Member

Choose a reason for hiding this comment

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

Same here.

@eddyb
Copy link
Member

eddyb commented Dec 9, 2019

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