Skip to content

Collect VTable stats & add -Zprint-vtable-sizes - #112400

Merged
bors merged 5 commits into
rust-lang:masterfrom
WaffleLapkin:vtable_stats
Jun 14, 2023
Merged

Collect VTable stats & add -Zprint-vtable-sizes#112400
bors merged 5 commits into
rust-lang:masterfrom
WaffleLapkin:vtable_stats

Conversation

@WaffleLapkin

Copy link
Copy Markdown
Member

This is a bit hacky/buggy, but I'm not entirely sure how to fix it, so I want to ask reviewers for help...

To try this, use either of those:

  • cargo clean && RUSTFLAGS="-Zprint-vtable-sizes" cargo +toolchain b
  • cargo clean && cargo rustc +toolchain -Zprint-vtable-sizes
  • rustc +toolchain -Zprint-vtable-sizes ./file.rs

@rustbot

Copy link
Copy Markdown
Collaborator

r? @b-naber

(rustbot has picked a reviewer for you, use r? to override)

@rustbotrustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 7, 2023
Comment threadcompiler/rustc_interface/src/passes.rs Outdated
Comment threadcompiler/rustc_session/src/code_stats.rs Outdated
@WaffleLapkin

Copy link
Copy Markdown
MemberAuthor

r? @compiler-errors

Comment threadcompiler/rustc_interface/src/passes.rs Outdated
Comment threadcompiler/rustc_interface/src/passes.rs Outdated
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Jun 8, 2023
…=Nilstrieb
Don't `use compile_error as print`
I've spent **1.5 hours** debugging this while trying to compile rust-lang#112400, if we use `compile_error!`, we should not just forward user input to it, but issue a reasonable error message.
The better solution would be to use a lint like `clippy::print_stdout`, but since we don't have clippy in CI, let's at least make the macro error better.
Also note that some functions called here actually do use `println` (see for example `print_type_sizes` function).
@WaffleLapkin

Copy link
Copy Markdown
MemberAuthor

So, status update:

  • I've fixed both problematic examples I noted above
  • I've also removed vtable_entries_no_resolve in favor of counting entries by hand, as suggested by @compiler-errors
  • I've added a skip of object-unsafe traits
  • I've also squashed all the fixes together

With this I think this is ready for review.

Some things we might want to fix:

  • I've made some things pub which maybe shouldn't be pub
  • Naming is somewhat off, for example size_words_without_upcasting is ehhh
  • There are no tests. There probably should be at least some tests.

Comment threadcompiler/rustc_interface/src/passes.rs Outdated
Comment on lines +921 to +927
let existential_trait_ref = trait_ref.map_bound(|trait_ref| {
ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref)
});

// Lookup the shape of vtable for the trait.
let own_existential_entries =
tcx.own_existential_vtable_entries(existential_trait_ref.def_id());

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.

Suggested change
let existential_trait_ref = trait_ref.map_bound(|trait_ref| {
ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref)
});
// Lookup the shape of vtable for the trait.
let own_existential_entries =
tcx.own_existential_vtable_entries(existential_trait_ref.def_id());
// Lookup the shape of vtable for the trait.
let own_existential_entries =
tcx.own_existential_vtable_entries(trait_ref.def_id());

Comment threadcompiler/rustc_interface/src/passes.rs Outdated
size_words_without_upcasting: unupcasted_cost,
size_words_with_upcasting: unupcasted_cost + upcast_cost,
difference_words: upcast_cost,
difference_percent: upcast_cost as f64 / unupcasted_cost as f64 * 100.,

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.

why isnt this (size_words_without_upcasting + size_words_with_upcasting) / size_words_without_upcasting... not sure if i totally understand the math here, that could use some explanation i think 😅

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.

nvm, I think I get it. This creates a "how much % larger is the new vtable compared to the old table". could use doc tho

Comment on lines +929 to +938
let own_entries = own_existential_entries.iter().copied().map(|_def_id| {
// The original code here ignores the method if its predicates are impossible.
// We can't really do that as, for example, all not trivial bounds on generic
// parameters are impossible (since we don't know the parameters...),
// see the comment above.

1
});

unupcasted_cost += own_entries.sum::<usize>();

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.

Can you uplift this comment and just use count 😅

@rust-log-analyzer

This comment has been minimized.

@WaffleLapkin

Copy link
Copy Markdown
MemberAuthor

@compiler-errors I've simplified the code, added some more docs, renamed some variables to make sense and finally added a test. I think this should be ready

@compiler-errors

Copy link
Copy Markdown
Contributor

@bors r+

@bors

bors commented Jun 13, 2023

Copy link
Copy Markdown
Collaborator

📌 Commit af4631a has been approved by compiler-errors

It is now in the queue for this repository.

@borsbors removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 13, 2023
@borsbors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Jun 13, 2023
@bors

bors commented Jun 14, 2023

Copy link
Copy Markdown
Collaborator

⌛ Testing commit af4631a with merge 7b0eac4...

@bors

bors commented Jun 14, 2023

Copy link
Copy Markdown
Collaborator

☀️ Test successful - checks-actions
Approved by: compiler-errors
Pushing 7b0eac4 to master...

@borsbors added the merged-by-bors This PR was explicitly merged by bors. label Jun 14, 2023
@bors
bors merged commit 7b0eac4 into rust-lang:masterJun 14, 2023
@rustbotrustbot added this to the 1.72.0 milestone Jun 14, 2023
@rust-timer

Copy link
Copy Markdown
Collaborator

Finished benchmarking commit (7b0eac4): comparison URL.

Overall result: ❌✅ regressions and improvements - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

meanrangecount
Regressions ❌
(primary)
--0
Regressions ❌
(secondary)
0.9%[0.9%, 0.9%]1
Improvements ✅
(primary)
-0.3%[-0.3%, -0.3%]2
Improvements ✅
(secondary)
--0
All ❌✅ (primary)-0.3%[-0.3%, -0.3%]2

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

meanrangecount
Regressions ❌
(primary)
3.7%[3.7%, 3.7%]1
Regressions ❌
(secondary)
--0
Improvements ✅
(primary)
-0.1%[-0.1%, -0.1%]1
Improvements ✅
(secondary)
--0
All ❌✅ (primary)1.8%[-0.1%, 3.7%]2

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

meanrangecount
Regressions ❌
(primary)
--0
Regressions ❌
(secondary)
5.4%[5.2%, 5.6%]2
Improvements ✅
(primary)
--0
Improvements ✅
(secondary)
--0
All ❌✅ (primary)--0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 648.428s -> 648.095s (-0.05%)

@lqd

lqd commented Jun 14, 2023

Copy link
Copy Markdown
Member

I'm pretty sure the perf changes are noise 😅

@kornelski

Copy link
Copy Markdown
Contributor

Is this meant to display sizes for traits that are never used with dyn? I have a crate-private trait that is only ever used with a monomorphic implementation, but I still see print-vtable-sizes line for it.

@WaffleLapkin

Copy link
Copy Markdown
MemberAuthor

@kornelski yes, this is expected. theoretically it'd be possible to filter out private & not used with dyn traits, but it's not trivial...

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

Labels

merged-by-borsThis PR was explicitly merged by bors.S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants

@WaffleLapkin@rustbot@rust-log-analyzer@compiler-errors@bors@rust-timer@lqd@kornelski@Noratrieb@b-naber