diff options
| author | Maybe Waffle <waffle.lapkin@gmail.com> | 2023-05-17 12:28:04 +0000 |
|---|---|---|
| committer | Maybe Waffle <waffle.lapkin@gmail.com> | 2023-06-12 15:58:35 +0000 |
| commit | f2545fb22554eb1b04528d490c681e583fdc31d0 (patch) | |
| tree | ea8549a633134a21e24d3d44a881a025d0fa229a /compiler/rustc_session/src/code_stats.rs | |
| parent | cb882fa998571e8a7ec1c06bb5d9dd9bc3423629 (diff) | |
| download | rust-f2545fb22554eb1b04528d490c681e583fdc31d0.tar.gz rust-f2545fb22554eb1b04528d490c681e583fdc31d0.zip | |
Collect VTable stats & add `-Zprint-vtable-sizes`
Diffstat (limited to 'compiler/rustc_session/src/code_stats.rs')
| -rw-r--r-- | compiler/rustc_session/src/code_stats.rs | 44 |
1 files changed, 43 insertions, 1 deletions
diff --git a/compiler/rustc_session/src/code_stats.rs b/compiler/rustc_session/src/code_stats.rs index 0dfee92f404..f76263de13f 100644 --- a/compiler/rustc_session/src/code_stats.rs +++ b/compiler/rustc_session/src/code_stats.rs @@ -1,5 +1,6 @@ -use rustc_data_structures::fx::FxHashSet; +use rustc_data_structures::fx::{FxHashMap, FxHashSet}; use rustc_data_structures::sync::Lock; +use rustc_span::def_id::DefId; use rustc_span::Symbol; use rustc_target::abi::{Align, Size}; use std::cmp; @@ -65,9 +66,18 @@ pub struct TypeSizeInfo { pub variants: Vec<VariantInfo>, } +pub struct VTableSizeInfo { + pub trait_name: String, + pub size_words_without_upcasting: usize, + pub size_words_with_upcasting: usize, + pub difference_words: usize, + pub difference_percent: f64, +} + #[derive(Default)] pub struct CodeStats { type_sizes: Lock<FxHashSet<TypeSizeInfo>>, + vtable_sizes: Lock<FxHashMap<DefId, VTableSizeInfo>>, } impl CodeStats { @@ -101,6 +111,14 @@ impl CodeStats { self.type_sizes.borrow_mut().insert(info); } + pub fn record_vtable_size(&self, trait_did: DefId, trait_name: &str, info: VTableSizeInfo) { + let prev = self.vtable_sizes.lock().insert(trait_did, info); + assert!( + prev.is_none(), + "size of vtable for `{trait_name}` ({trait_did:?}) is already recorded" + ); + } + pub fn print_type_sizes(&self) { let type_sizes = self.type_sizes.borrow(); let mut sorted: Vec<_> = type_sizes.iter().collect(); @@ -196,4 +214,28 @@ impl CodeStats { } } } + + pub fn print_vtable_sizes(&self, crate_name: &str) { + let mut rr = std::mem::take(&mut *self.vtable_sizes.lock()).into_iter().collect::<Vec<_>>(); + + rr.sort_by(|(_, stats_a), (_, stats_b)| { + stats_b.difference_percent.total_cmp(&stats_a.difference_percent) + }); + + for ( + _, + VTableSizeInfo { + trait_name, + size_words_without_upcasting, + size_words_with_upcasting, + difference_words, + difference_percent, + }, + ) in rr + { + println!( + r#"print-vtable-sizes {{ "crate_name": "{crate_name}", "trait_name": "{trait_name}", "size_unupcastable_words": "{size_words_without_upcasting}", "size_upcastable_words": "{size_words_with_upcasting}", diff: "{difference_words}", diff_p: "{difference_percent}" }}"# + ); + } + } } |
