Skip to content

Commit 8b1abd6

Browse files
committed
Make stats code nicer.
Taking inspiration from `-Zmacro-stats`: - Use "{prefix}" consistently. - Use names for column widths. - Write output in a single `eprint!` call, in an attempt to minimize interleaving of output from different rustc processes. - Use `repeat` for the long `---` banners.
1 parent 111e9bc commit 8b1abd6

File tree

3 files changed

+62
-30
lines changed

3 files changed

+62
-30
lines changed

compiler/rustc_interface/src/passes.rs

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

372372
if sess.opts.unstable_opts.input_stats {
373-
input_stats::print_ast_stats(krate, "POST EXPANSION AST STATS", "ast-stats");
373+
input_stats::print_ast_stats(krate);
374374
}
375375

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

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 24 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.
@@ -781,27 +783,38 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
781783
let prefix = "meta-stats";
782784
let perc = |bytes| (bytes * 100) as f64 / total_bytes as f64;
783785

784-
eprintln!("{prefix} METADATA STATS");
785-
eprintln!("{} {:<23}{:>10}", prefix, "Section", "Size");
786-
eprintln!("{prefix} ----------------------------------------------------------------");
786+
let section_w = 23;
787+
let size_w = 10;
788+
let banner_w = 64;
789+
790+
// We write all the text into a string and print it with a single
791+
// `eprint!`. This is an attempt to minimize interleaved text if multiple
792+
// rustc processes are printing macro-stats at the same time (e.g. with
793+
// `RUSTFLAGS='-Zmeta-stats' cargo build`). It still doesn't guarantee
794+
// non-interleaving, though.
795+
let mut s = String::new();
796+
_ = writeln!(s, "{prefix} METADATA STATS");
797+
_ = writeln!(s, "{prefix} {:<section_w$}{:>size_w$}", "Section", "Size");
798+
_ = writeln!(s, "{prefix} {}", "-".repeat(banner_w));
787799
for (label, size) in stats {
788-
eprintln!(
789-
"{} {:<23}{:>10} ({:4.1}%)",
790-
prefix,
800+
_ = writeln!(
801+
s,
802+
"{prefix} {:<section_w$}{:>size_w$} ({:4.1}%)",
791803
label,
792804
usize_with_underscores(size),
793805
perc(size)
794806
);
795807
}
796-
eprintln!("{prefix} ----------------------------------------------------------------");
797-
eprintln!(
798-
"{} {:<23}{:>10} (of which {:.1}% are zero bytes)",
799-
prefix,
808+
_ = writeln!(s, "{prefix} {}", "-".repeat(banner_w));
809+
_ = writeln!(
810+
s,
811+
"{prefix} {:<section_w$}{:>size_w$} (of which {:.1}% are zero bytes)",
800812
"Total",
801813
usize_with_underscores(total_bytes),
802814
perc(zero_bytes)
803815
);
804-
eprintln!("{prefix}");
816+
_ = writeln!(s, "{prefix}");
817+
eprint!("{s}");
805818
}
806819

807820
root

compiler/rustc_passes/src/input_stats.rs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ pub fn print_hir_stats(tcx: TyCtxt<'_>) {
6868
collector.print("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(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("POST EXPANSION AST STATS", "ast-stats");
7878
}
7979

8080
impl<'k> StatCollector<'k> {
@@ -117,28 +117,45 @@ impl<'k> StatCollector<'k> {
117117
}
118118

119119
fn print(&self, 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()));
124126

127+
let name_w = 18;
128+
let acc_size1_w = 10;
129+
let acc_size2_w = 8; // " (NN.N%)"
130+
let acc_size_w = acc_size1_w + acc_size2_w;
131+
let count_w = 14;
132+
let item_size_w = 14;
133+
let banner_w = name_w + acc_size_w + count_w + item_size_w;
134+
125135
let total_size = nodes.iter().map(|(_, node)| node.stats.accum_size()).sum();
126136
let total_count = nodes.iter().map(|(_, node)| node.stats.count).sum();
127137

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

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

137154
for (label, node) in nodes {
138155
let size = node.stats.accum_size();
139-
eprintln!(
140-
"{} {:<18}{:>10} ({:4.1}%){:>14}{:>14}",
141-
prefix,
156+
_ = writeln!(
157+
s,
158+
"{prefix} {:<name_w$}{:>acc_size1_w$} ({:4.1}%){:>count_w$}{:>item_size_w$}",
142159
label,
143160
usize_with_underscores(size),
144161
percent(size, total_size),
@@ -155,9 +172,9 @@ impl<'k> StatCollector<'k> {
155172

156173
for (label, subnode) in subnodes {
157174
let size = subnode.accum_size();
158-
eprintln!(
159-
"{} - {:<18}{:>10} ({:4.1}%){:>14}",
160-
prefix,
175+
_ = writeln!(
176+
s,
177+
"{prefix} - {:<name_w$}{:>acc_size1_w$} ({:4.1}%){:>count_w$}",
161178
label,
162179
usize_with_underscores(size),
163180
percent(size, total_size),
@@ -166,15 +183,17 @@ impl<'k> StatCollector<'k> {
166183
}
167184
}
168185
}
169-
eprintln!("{prefix} ----------------------------------------------------------------");
170-
eprintln!(
171-
"{} {:<18}{:>10} {:>14}",
172-
prefix,
186+
_ = writeln!(s, "{prefix} {}", "-".repeat(banner_w));
187+
_ = writeln!(
188+
s,
189+
"{prefix} {:<name_w$}{:>acc_size1_w$}{:>acc_size2_w$}{:>count_w$}",
173190
"Total",
174191
usize_with_underscores(total_size),
192+
"",
175193
usize_with_underscores(total_count),
176194
);
177-
eprintln!("{prefix}");
195+
_ = writeln!(s, "{prefix}");
196+
eprint!("{s}");
178197
}
179198
}
180199

0 commit comments

Comments
 (0)