diff options
| author | Nicholas Nethercote <n.nethercote@gmail.com> | 2025-06-24 10:11:52 +1000 |
|---|---|---|
| committer | Nicholas Nethercote <n.nethercote@gmail.com> | 2025-06-24 13:05:51 +1000 |
| commit | 8b1abd6578dea3e7632014b38dbc0acadb5f0861 (patch) | |
| tree | 3dd875e6bdf5f258df6c2d53b0f29e799df077d4 /compiler/rustc_metadata/src/rmeta/encoder.rs | |
| parent | 111e9bc64bbdce14122e3676978f2ceefa5bff1a (diff) | |
| download | rust-8b1abd6578dea3e7632014b38dbc0acadb5f0861.tar.gz rust-8b1abd6578dea3e7632014b38dbc0acadb5f0861.zip | |
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.
Diffstat (limited to 'compiler/rustc_metadata/src/rmeta/encoder.rs')
| -rw-r--r-- | compiler/rustc_metadata/src/rmeta/encoder.rs | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/compiler/rustc_metadata/src/rmeta/encoder.rs b/compiler/rustc_metadata/src/rmeta/encoder.rs index 00bd32eb0eb..53cf69b6ac2 100644 --- a/compiler/rustc_metadata/src/rmeta/encoder.rs +++ b/compiler/rustc_metadata/src/rmeta/encoder.rs @@ -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. @@ -781,27 +783,38 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { 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} METADATA STATS"); + _ = 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}"); + eprint!("{s}"); } root |
