Tweak names and docs for vtable stats

This commit is contained in:
Maybe Waffle 2023-06-13 12:07:12 +00:00
parent 5008a08acf
commit 8e6a193946
2 changed files with 42 additions and 28 deletions

View file

@ -880,9 +880,9 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
let mut first_dsa = true;
// Number of vtable entries, if we didn't have upcasting
let mut unupcasted_cost = 0;
let mut entries_ignoring_upcasting = 0;
// Number of vtable entries needed solely for upcasting
let mut upcast_cost = 0;
let mut entries_for_upcasting = 0;
let trait_ref = ty::Binder::dummy(ty::TraitRef::identity(tcx, tr));
@ -911,9 +911,9 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
// If this is the first dsa, it would be included either way,
// otherwise it's needed for upcasting
if std::mem::take(&mut first_dsa) {
unupcasted_cost += 3;
entries_ignoring_upcasting += 3;
} else {
upcast_cost += 3;
entries_for_upcasting += 3;
}
}
@ -926,10 +926,10 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
// We can't really do that as, for example, all not trivial bounds on generic
// parameters are impossible (since we don't know the parameters...),
// see the comment above.
unupcasted_cost += own_existential_entries.len();
entries_ignoring_upcasting += own_existential_entries.len();
if emit_vptr {
upcast_cost += 1;
entries_for_upcasting += 1;
}
}
}
@ -942,10 +942,12 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
&name,
VTableSizeInfo {
trait_name: name.clone(),
size_words_without_upcasting: unupcasted_cost,
size_words_with_upcasting: unupcasted_cost + upcast_cost,
difference_words: upcast_cost,
difference_percent: upcast_cost as f64 / unupcasted_cost as f64 * 100.,
entries: entries_ignoring_upcasting + entries_for_upcasting,
entries_ignoring_upcasting,
entries_for_upcasting,
upcasting_cost_percent: entries_for_upcasting as f64
/ entries_ignoring_upcasting as f64
* 100.,
},
)
}

View file

@ -68,10 +68,21 @@ pub struct TypeSizeInfo {
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,
/// Number of entries in a vtable with the current algorithm
/// (i.e. with upcasting).
pub entries: usize,
/// Number of entries in a vtable, as-if we did not have trait upcasting.
pub entries_ignoring_upcasting: usize,
/// Number of entries in a vtable needed solely for upcasting
/// (i.e. `entries - entries_ignoring_upcasting`).
pub entries_for_upcasting: usize,
/// Cost of having upcasting in % relative to the number of entries without
/// upcasting (i.e. `entries_for_upcasting / entries_ignoring_upcasting * 100%`).
pub upcasting_cost_percent: f64,
}
#[derive(Default)]
@ -216,25 +227,26 @@ 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<_>>();
let mut infos = std::mem::take(&mut *self.vtable_sizes.lock())
.into_iter()
.map(|(_did, stats)| stats)
.collect::<Vec<_>>();
rr.sort_by(|(_, stats_a), (_, stats_b)| {
stats_b.difference_percent.total_cmp(&stats_a.difference_percent)
// Sort by the cost % in reverse order (from biggest to smallest)
infos.sort_by(|a, b| {
a.upcasting_cost_percent.total_cmp(&b.upcasting_cost_percent).reverse()
});
for (
_,
VTableSizeInfo {
trait_name,
size_words_without_upcasting,
size_words_with_upcasting,
difference_words,
difference_percent,
},
) in rr
for VTableSizeInfo {
trait_name,
entries,
entries_ignoring_upcasting,
entries_for_upcasting,
upcasting_cost_percent,
} in infos
{
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}" }}"#
r#"print-vtable-sizes {{ "crate_name": "{crate_name}", "trait_name": "{trait_name}", "entries": "{entries}", "entries_ignoring_upcasting": "{entries_ignoring_upcasting}", "entries_for_upcasting": "{entries_for_upcasting}", "upcasting_cost_percent": "{upcasting_cost_percent}" }}"#
);
}
}