Skip to content

Commit ece3c13

Browse files
authored
Unrolled build for #142944
Rollup merge of #142944 - nnethercote:stats-tweaks, r=lqd Stats output tweaks Some improvements to `-Zinput-stats` and `-Zmeta-stat` inspired by the new `-Zmacro-stats`. r? `@lqd`
2 parents 36b2163 + b2a57e6 commit ece3c13

File tree

4 files changed

+167
-129
lines changed

4 files changed

+167
-129
lines changed

compiler/rustc_interface/src/passes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) {
371371
let mut lint_buffer = resolver.lint_buffer.steal();
372372

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

377377
// Needs to go *after* expansion to be able to check the results of macro expansion.

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
762762
assert_eq!(total_bytes, computed_total_bytes);
763763

764764
if tcx.sess.opts.unstable_opts.meta_stats {
765+
use std::fmt::Write;
766+
765767
self.opaque.flush();
766768

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

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

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

784-
eprintln!("{prefix} METADATA STATS");
785-
eprintln!("{} {:<23}{:>10}", prefix, "Section", "Size");
786-
eprintln!("{prefix} ----------------------------------------------------------------");
787+
let section_w = 23;
788+
let size_w = 10;
789+
let banner_w = 64;
790+
791+
// We write all the text into a string and print it with a single
792+
// `eprint!`. This is an attempt to minimize interleaved text if multiple
793+
// rustc processes are printing macro-stats at the same time (e.g. with
794+
// `RUSTFLAGS='-Zmeta-stats' cargo build`). It still doesn't guarantee
795+
// non-interleaving, though.
796+
let mut s = String::new();
797+
_ = writeln!(s, "{prefix} {}", "=".repeat(banner_w));
798+
_ = writeln!(s, "{prefix} METADATA STATS: {}", tcx.crate_name(LOCAL_CRATE));
799+
_ = writeln!(s, "{prefix} {:<section_w$}{:>size_w$}", "Section", "Size");
800+
_ = writeln!(s, "{prefix} {}", "-".repeat(banner_w));
787801
for (label, size) in stats {
788-
eprintln!(
789-
"{} {:<23}{:>10} ({:4.1}%)",
790-
prefix,
802+
_ = writeln!(
803+
s,
804+
"{prefix} {:<section_w$}{:>size_w$} ({:4.1}%)",
791805
label,
792806
usize_with_underscores(size),
793807
perc(size)
794808
);
795809
}
796-
eprintln!("{prefix} ----------------------------------------------------------------");
797-
eprintln!(
798-
"{} {:<23}{:>10} (of which {:.1}% are zero bytes)",
799-
prefix,
810+
_ = writeln!(s, "{prefix} {}", "-".repeat(banner_w));
811+
_ = writeln!(
812+
s,
813+
"{prefix} {:<section_w$}{:>size_w$} (of which {:.1}% are zero bytes)",
800814
"Total",
801815
usize_with_underscores(total_bytes),
802816
perc(zero_bytes)
803817
);
804-
eprintln!("{prefix}");
818+
_ = writeln!(s, "{prefix} {}", "=".repeat(banner_w));
819+
eprint!("{s}");
805820
}
806821

807822
root

compiler/rustc_passes/src/input_stats.rs

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ pub fn print_hir_stats(tcx: TyCtxt<'_>) {
6565
StatCollector { tcx: Some(tcx), nodes: FxHashMap::default(), seen: FxHashSet::default() };
6666
tcx.hir_walk_toplevel_module(&mut collector);
6767
tcx.hir_walk_attributes(&mut collector);
68-
collector.print("HIR STATS", "hir-stats");
68+
collector.print(tcx, "HIR STATS", "hir-stats");
6969
}
7070

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

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

8080
impl<'k> StatCollector<'k> {
@@ -116,29 +116,48 @@ impl<'k> StatCollector<'k> {
116116
}
117117
}
118118

119-
fn print(&self, title: &str, prefix: &str) {
119+
fn print(&self, tcx: TyCtxt<'_>, title: &str, prefix: &str) {
120+
use std::fmt::Write;
121+
120122
// We will soon sort, so the initial order does not matter.
121123
#[allow(rustc::potential_query_instability)]
122124
let mut nodes: Vec<_> = self.nodes.iter().collect();
123125
nodes.sort_by_cached_key(|(label, node)| (node.stats.accum_size(), label.to_owned()));
126+
nodes.reverse(); // bigger items first
127+
128+
let name_w = 18;
129+
let acc_size1_w = 10;
130+
let acc_size2_w = 8; // " (NN.N%)"
131+
let acc_size_w = acc_size1_w + acc_size2_w;
132+
let count_w = 14;
133+
let item_size_w = 14;
134+
let banner_w = name_w + acc_size_w + count_w + item_size_w;
124135

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

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

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

137156
for (label, node) in nodes {
138157
let size = node.stats.accum_size();
139-
eprintln!(
140-
"{} {:<18}{:>10} ({:4.1}%){:>14}{:>14}",
141-
prefix,
158+
_ = writeln!(
159+
s,
160+
"{prefix} {:<name_w$}{:>acc_size1_w$} ({:4.1}%){:>count_w$}{:>item_size_w$}",
142161
label,
143162
usize_with_underscores(size),
144163
percent(size, total_size),
@@ -155,9 +174,9 @@ impl<'k> StatCollector<'k> {
155174

156175
for (label, subnode) in subnodes {
157176
let size = subnode.accum_size();
158-
eprintln!(
159-
"{} - {:<18}{:>10} ({:4.1}%){:>14}",
160-
prefix,
177+
_ = writeln!(
178+
s,
179+
"{prefix} - {:<name_w$}{:>acc_size1_w$} ({:4.1}%){:>count_w$}",
161180
label,
162181
usize_with_underscores(size),
163182
percent(size, total_size),
@@ -166,15 +185,17 @@ impl<'k> StatCollector<'k> {
166185
}
167186
}
168187
}
169-
eprintln!("{prefix} ----------------------------------------------------------------");
170-
eprintln!(
171-
"{} {:<18}{:>10} {:>14}",
172-
prefix,
188+
_ = writeln!(s, "{prefix} {}", "-".repeat(banner_w));
189+
_ = writeln!(
190+
s,
191+
"{prefix} {:<name_w$}{:>acc_size1_w$}{:>acc_size2_w$}{:>count_w$}",
173192
"Total",
174193
usize_with_underscores(total_size),
194+
"",
175195
usize_with_underscores(total_count),
176196
);
177-
eprintln!("{prefix}");
197+
_ = writeln!(s, "{prefix} {}", "=".repeat(banner_w));
198+
eprint!("{s}");
178199
}
179200
}
180201

0 commit comments

Comments
 (0)