Skip to content

Stats output tweaks #142944

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

Merged
merged 3 commits into from
Jun 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_interface/src/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
let mut lint_buffer = resolver.lint_buffer.steal();

if sess.opts.unstable_opts.input_stats {
input_stats::print_ast_stats(krate, "POST EXPANSION AST STATS", "ast-stats");
input_stats::print_ast_stats(tcx, krate);
}

// Needs to go *after* expansion to be able to check the results of macro expansion.
Expand Down
37 changes: 26 additions & 11 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
assert_eq!(total_bytes, computed_total_bytes);

if tcx.sess.opts.unstable_opts.meta_stats {
use std::fmt::Write;

self.opaque.flush();

// Rewind and re-read all the metadata to count the zero bytes we wrote.
Expand All @@ -777,31 +779,44 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
assert_eq!(self.opaque.file().stream_position().unwrap(), pos_before_rewind);

stats.sort_by_key(|&(_, usize)| usize);
stats.reverse(); // bigger items first

let prefix = "meta-stats";
let perc = |bytes| (bytes * 100) as f64 / total_bytes as f64;

eprintln!("{prefix} METADATA STATS");
eprintln!("{} {:<23}{:>10}", prefix, "Section", "Size");
eprintln!("{prefix} ----------------------------------------------------------------");
let section_w = 23;
let size_w = 10;
let banner_w = 64;

// We write all the text into a string and print it with a single
// `eprint!`. This is an attempt to minimize interleaved text if multiple
// rustc processes are printing macro-stats at the same time (e.g. with
// `RUSTFLAGS='-Zmeta-stats' cargo build`). It still doesn't guarantee
// non-interleaving, though.
let mut s = String::new();
_ = writeln!(s, "{prefix} {}", "=".repeat(banner_w));
_ = writeln!(s, "{prefix} METADATA STATS: {}", tcx.crate_name(LOCAL_CRATE));
_ = writeln!(s, "{prefix} {:<section_w$}{:>size_w$}", "Section", "Size");
_ = writeln!(s, "{prefix} {}", "-".repeat(banner_w));
for (label, size) in stats {
eprintln!(
"{} {:<23}{:>10} ({:4.1}%)",
prefix,
_ = writeln!(
s,
"{prefix} {:<section_w$}{:>size_w$} ({:4.1}%)",
label,
usize_with_underscores(size),
perc(size)
);
}
eprintln!("{prefix} ----------------------------------------------------------------");
eprintln!(
"{} {:<23}{:>10} (of which {:.1}% are zero bytes)",
prefix,
_ = writeln!(s, "{prefix} {}", "-".repeat(banner_w));
_ = writeln!(
s,
"{prefix} {:<section_w$}{:>size_w$} (of which {:.1}% are zero bytes)",
"Total",
usize_with_underscores(total_bytes),
perc(zero_bytes)
);
eprintln!("{prefix}");
_ = writeln!(s, "{prefix} {}", "=".repeat(banner_w));
eprint!("{s}");
}

root
Expand Down
61 changes: 41 additions & 20 deletions compiler/rustc_passes/src/input_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ pub fn print_hir_stats(tcx: TyCtxt<'_>) {
StatCollector { tcx: Some(tcx), nodes: FxHashMap::default(), seen: FxHashSet::default() };
tcx.hir_walk_toplevel_module(&mut collector);
tcx.hir_walk_attributes(&mut collector);
collector.print("HIR STATS", "hir-stats");
collector.print(tcx, "HIR STATS", "hir-stats");
}

pub fn print_ast_stats(krate: &ast::Crate, title: &str, prefix: &str) {
pub fn print_ast_stats(tcx: TyCtxt<'_>, krate: &ast::Crate) {
use rustc_ast::visit::Visitor;

let mut collector =
StatCollector { tcx: None, nodes: FxHashMap::default(), seen: FxHashSet::default() };
collector.visit_crate(krate);
collector.print(title, prefix);
collector.print(tcx, "POST EXPANSION AST STATS", "ast-stats");
}

impl<'k> StatCollector<'k> {
Expand Down Expand Up @@ -116,29 +116,48 @@ impl<'k> StatCollector<'k> {
}
}

fn print(&self, title: &str, prefix: &str) {
fn print(&self, tcx: TyCtxt<'_>, title: &str, prefix: &str) {
use std::fmt::Write;

// We will soon sort, so the initial order does not matter.
#[allow(rustc::potential_query_instability)]
let mut nodes: Vec<_> = self.nodes.iter().collect();
nodes.sort_by_cached_key(|(label, node)| (node.stats.accum_size(), label.to_owned()));
nodes.reverse(); // bigger items first

let name_w = 18;
let acc_size1_w = 10;
let acc_size2_w = 8; // " (NN.N%)"
let acc_size_w = acc_size1_w + acc_size2_w;
let count_w = 14;
let item_size_w = 14;
let banner_w = name_w + acc_size_w + count_w + item_size_w;

let total_size = nodes.iter().map(|(_, node)| node.stats.accum_size()).sum();
let total_count = nodes.iter().map(|(_, node)| node.stats.count).sum();

eprintln!("{prefix} {title}");
eprintln!(
"{} {:<18}{:>18}{:>14}{:>14}",
prefix, "Name", "Accumulated Size", "Count", "Item Size"
// We write all the text into a string and print it with a single
// `eprint!`. This is an attempt to minimize interleaved text if multiple
// rustc processes are printing macro-stats at the same time (e.g. with
// `RUSTFLAGS='-Zinput-stats' cargo build`). It still doesn't guarantee
// non-interleaving, though.
let mut s = String::new();
_ = writeln!(s, "{prefix} {}", "=".repeat(banner_w));
_ = writeln!(s, "{prefix} {title}: {}", tcx.crate_name(hir::def_id::LOCAL_CRATE));
_ = writeln!(
s,
"{prefix} {:<name_w$}{:>acc_size_w$}{:>count_w$}{:>item_size_w$}",
"Name", "Accumulated Size", "Count", "Item Size"
);
eprintln!("{prefix} ----------------------------------------------------------------");
_ = writeln!(s, "{prefix} {}", "-".repeat(banner_w));

let percent = |m, n| (m * 100) as f64 / n as f64;

for (label, node) in nodes {
let size = node.stats.accum_size();
eprintln!(
"{} {:<18}{:>10} ({:4.1}%){:>14}{:>14}",
prefix,
_ = writeln!(
s,
"{prefix} {:<name_w$}{:>acc_size1_w$} ({:4.1}%){:>count_w$}{:>item_size_w$}",
label,
usize_with_underscores(size),
percent(size, total_size),
Expand All @@ -155,9 +174,9 @@ impl<'k> StatCollector<'k> {

for (label, subnode) in subnodes {
let size = subnode.accum_size();
eprintln!(
"{} - {:<18}{:>10} ({:4.1}%){:>14}",
prefix,
_ = writeln!(
s,
"{prefix} - {:<name_w$}{:>acc_size1_w$} ({:4.1}%){:>count_w$}",
label,
usize_with_underscores(size),
percent(size, total_size),
Expand All @@ -166,15 +185,17 @@ impl<'k> StatCollector<'k> {
}
}
}
eprintln!("{prefix} ----------------------------------------------------------------");
eprintln!(
"{} {:<18}{:>10} {:>14}",
prefix,
_ = writeln!(s, "{prefix} {}", "-".repeat(banner_w));
_ = writeln!(
s,
"{prefix} {:<name_w$}{:>acc_size1_w$}{:>acc_size2_w$}{:>count_w$}",
"Total",
usize_with_underscores(total_size),
"",
usize_with_underscores(total_count),
);
eprintln!("{prefix}");
_ = writeln!(s, "{prefix} {}", "=".repeat(banner_w));
eprint!("{s}");
}
}

Expand Down
Loading
Loading