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

Start improving monomorphization items stats #105481

Merged
merged 4 commits into from
Dec 16, 2022
Merged

Conversation

lqd
Copy link
Member

@lqd lqd commented Dec 9, 2022

As described in this zulip discussion, some stats about monomorphization collection would be interesting to have, in a different form than -Zprint-mono-items: to have some visibility into the cost of the mono items, we'd like to know how many are instantiated and what is their estimated size.

That can be a proxy to analyze sources of slow compile times, although in the future, we'd also like to add more realistic stats from the actual backend's lowering.

This PR adds a new -Z dump-mono-stats flag which will output some stats in a {crate_name}.mono-items.md file (the flag optionally takes an output directory parameter, for easier use within a workspace than printing to stdout).

For example,

fn compute<T>(collection: Vec<T>) -> usize {
    collection.len() + 19 - 0 * 9 - 18 - 1 * 1 // random code to increase the function's size
}

fn main() {
    dbg!(compute(vec![0u8, 1, 2]));
    dbg!(compute(vec![0u64, 1, 2]));
    dbg!(compute(vec!["0", "1", "2", "3"]));
}

will output a file with this markdown table (abridged for readability), for a debug build:

Item Instantiation count Estimated Cost Per Instantiation Total Estimated Cost
alloc::alloc::box_free 3 122 366
std::alloc::Global::alloc_impl 1 284 284
alloc::raw_vec::RawVec::<T, A>::current_memory 3 82 246
std::ptr::align_offset 1 222 222
std::slice::hack::into_vec 3 67 201
<std::vec::Vec<T, A> as std::ops::Drop>::drop 3 66 198
std::ptr::mut_ptr::<impl *mut T>::is_null 4 47 188
main 1 163 163
std::ptr::NonNull::::new_unchecked 4 37 148
...
Click for full output
Item Instantiation count Estimated Cost Per Instantiation Total Estimated Cost
alloc::alloc::box_free 3 122 366
std::alloc::Global::alloc_impl 1 284 284
alloc::raw_vec::RawVec::<T, A>::current_memory 3 82 246
std::ptr::align_offset 1 222 222
std::slice::hack::into_vec 3 67 201
<std::vec::Vec<T, A> as std::ops::Drop>::drop 3 66 198
std::ptr::mut_ptr::<impl *mut T>::is_null 4 47 188
main 1 163 163
std::ptr::NonNull::::new_unchecked 4 37 148
std::boxed::Box::<T, A>::into_unique 3 48 144
std::boxed::Box::<T, A>::leak 3 39 117
std::alloc::Layout::array::inner 1 107 107
std::ptr::align_offset::mod_inv 1 103 103
std::boxed::Box::<T, A>::into_raw_with_allocator 3 31 93
std::fmt::Arguments::<'a>::new_v1 1 80 80
<alloc::raw_vec::RawVec<T, A> as std::ops::Drop>::drop 3 26 78
alloc::raw_vec::RawVec::<T, A>::from_raw_parts_in 3 26 78
alloc::alloc::exchange_malloc 1 75 75
std::ptr::const_ptr::<impl *const T>::is_null 1 75 75
std::ptr::const_ptr::<impl *const T>::is_aligned_to 1 64 64
compute 3 20 60
std::ptr::const_ptr::<impl *const T>::align_offset 1 55 55
std::ptr::read 1 52 52
<std::alloc::Global as std::alloc::Allocator>::deallocate 1 50 50
std::ptr::mut_ptr::<impl *mut T>::guaranteed_eq 1 48 48
std::fmt::ArgumentV1::<'a>::new_display 2 22 44
std::ptr::Alignment::new_unchecked 1 42 42
core::fmt::num::::fmt 1 40 40
std::result::Result::<T, E>::unwrap_unchecked 1 37 37
std::cmp::Ord::min 1 32 32
std::cmp::impls::::cmp 1 31 31
std::intrinsics::is_aligned_and_not_null 1 27 27
std::rt::lang_start 1 27 27
std::ptr::NonNull::::new 1 24 24
std::fmt::ArgumentV1::<'a>::new_debug 1 22 22
std::fmt::Arguments::<'a>::new_v1_formatted 1 19 19
std::rt::lang_start::{closure#0} 1 17 17
std::sys_common::backtrace::__rust_begin_short_backtrace 1 16 16
std::slice::<impl [T]>::into_vec 3 5 15
<std::ptr::NonNull as std::convert::From<std::ptr::Unique>>::from 1 14 14
<&T as std::fmt::Debug>::fmt 1 12 12
<&T as std::fmt::Display>::fmt 1 12 12
std::vec::Vec::<T, A>::len 3 2 6
<T as std::convert::Into>::into 1 5 5
<T as std::convert::From>::from 1 2 2
<() as std::process::Termination>::report 1 2 2
std::hint::unreachable_unchecked 1 2 2
core::fmt::UnsafeArg::new 1 1 1

Since we discussed it together, r? @wesleywiser.

@rustbot rustbot added A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic 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 Dec 9, 2022
@rustbot
Copy link
Collaborator

rustbot commented Dec 9, 2022

rustc_error_messages was changed

cc @davidtwco, @compiler-errors, @JohnTitor, @estebank, @TaKO8Ki

@jyn514
Copy link
Member

jyn514 commented Dec 10, 2022

Outputting as markdown doesn't seem very extensible, it's hard to parse. Could we output JSON instead (and document a simple JQ command to turn it into markdown)?

@klensy
Copy link
Contributor

klensy commented Dec 10, 2022

Outputting as markdown doesn't seem very extensible, it's hard to parse. Could we output JSON instead (and document a simple JQ command to turn it into markdown)?

Let csv win in that case.

@wesleywiser
Copy link
Member

Outputting as markdown doesn't seem very extensible, it's hard to parse. Could we output JSON instead (and document a simple JQ command to turn it into markdown)?

@jyn514 Do you have use cases in mind where you'd want to parse the output?

@jyn514
Copy link
Member

jyn514 commented Dec 10, 2022

@wesleywiser if print-mono-items were extensible / used JSON, we could have added to that flag instead of adding a new one. I don't have concrete use cases in mind, but it seems nice to make the output reusable instead of hardcoding markdown.

lqd added 4 commits December 14, 2022 20:17
This option will output some stats from the monomorphization collection
pass to a file, to show estimated sizes from each instantiation.
@lqd
Copy link
Member Author

lqd commented Dec 14, 2022

You could add the same information to -Zprint-mono-items after the existing output, add some information to the per-line output, or change it altogether with a different argument than "lazy/eager", it's still extensible. Having a file with the crate name, and in a human-readable format, seems easier to use though.

@wesleywiser what do you want to do here ?

@jyn514
Copy link
Member

jyn514 commented Dec 14, 2022

Ok, makes sense. I don't have strong objections to markdown.

@wesleywiser
Copy link
Member

I used this a bit yesterday to look at some codesize issues and found the current format helpful as it was easy to just set RUSTFLAGS, run the normal compilation and then look at the various files that were created in the working directory. I see the value of having this available as json as well but that seems like an improvement that can come later as people want it.

@bors r+

@bors
Copy link
Contributor

bors commented Dec 15, 2022

📌 Commit b720847 has been approved by wesleywiser

It is now in the queue for this repository.

@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 Dec 15, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 15, 2022
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#105481 (Start improving monomorphization items stats)
 - rust-lang#105674 (Point at method chains on `E0271` errors)
 - rust-lang#105679 (Suggest constraining type parameter with `Clone`)
 - rust-lang#105694 (Don't create dummy if val has escaping bounds var)
 - rust-lang#105727 (Tweak output for bare `dyn Trait` in arguments)
 - rust-lang#105739 (Migrate Jump to def links background to CSS variable)
 - rust-lang#105743 (`SimplifiedType` cleanups)
 - rust-lang#105758 (Move `TypeckResults` to separate module)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 863d1f6 into rust-lang:master Dec 16, 2022
@rustbot rustbot added this to the 1.68.0 milestone Dec 16, 2022
@nnethercote
Copy link
Contributor

How does everyone view markdown files?

BTW, I was a little surprised that the markdown files ended up in the root directory. I went looking in the build directory first, where things like .ll files are put...

@jyn514
Copy link
Member

jyn514 commented Dec 16, 2022

@nnethercote you can use pulldown-cmark to convert them to html and view them in a browser, or paste them into a gist and have GitHub render them.

@lqd lqd deleted the mono-stats branch December 16, 2022 08:15
@lqd
Copy link
Member Author

lqd commented Dec 16, 2022

@nnethercote btw, similarly to -Zself-profiler, you can also pass a directory as the flag’s parameter and it’ll dump files there instead of the current directory.

Some editors also support markdown rendering, either natively or via plugins, VSCode for example, and I often use that or hackmd.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jan 4, 2023
Add JSON output to -Zdump-mono-stats

Follow-up to rust-lang#105481

r? `@lqd` cc `@wesleywiser`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-translation Area: Translation infrastructure, and migrating existing diagnostics to SessionDiagnostic S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant 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.

8 participants