Skip to content

Commit 8e6a193

Browse files
committed
Tweak names and docs for vtable stats
1 parent 5008a08 commit 8e6a193

File tree

2 files changed

+43
-29
lines changed

2 files changed

+43
-29
lines changed

compiler/rustc_interface/src/passes.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -880,9 +880,9 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
880880
let mut first_dsa = true;
881881

882882
// Number of vtable entries, if we didn't have upcasting
883-
let mut unupcasted_cost = 0;
883+
let mut entries_ignoring_upcasting = 0;
884884
// Number of vtable entries needed solely for upcasting
885-
let mut upcast_cost = 0;
885+
let mut entries_for_upcasting = 0;
886886

887887
let trait_ref = ty::Binder::dummy(ty::TraitRef::identity(tcx, tr));
888888

@@ -911,9 +911,9 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
911911
// If this is the first dsa, it would be included either way,
912912
// otherwise it's needed for upcasting
913913
if std::mem::take(&mut first_dsa) {
914-
unupcasted_cost += 3;
914+
entries_ignoring_upcasting += 3;
915915
} else {
916-
upcast_cost += 3;
916+
entries_for_upcasting += 3;
917917
}
918918
}
919919

@@ -926,10 +926,10 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
926926
// We can't really do that as, for example, all not trivial bounds on generic
927927
// parameters are impossible (since we don't know the parameters...),
928928
// see the comment above.
929-
unupcasted_cost += own_existential_entries.len();
929+
entries_ignoring_upcasting += own_existential_entries.len();
930930

931931
if emit_vptr {
932-
upcast_cost += 1;
932+
entries_for_upcasting += 1;
933933
}
934934
}
935935
}
@@ -942,10 +942,12 @@ fn analysis(tcx: TyCtxt<'_>, (): ()) -> Result<()> {
942942
&name,
943943
VTableSizeInfo {
944944
trait_name: name.clone(),
945-
size_words_without_upcasting: unupcasted_cost,
946-
size_words_with_upcasting: unupcasted_cost + upcast_cost,
947-
difference_words: upcast_cost,
948-
difference_percent: upcast_cost as f64 / unupcasted_cost as f64 * 100.,
945+
entries: entries_ignoring_upcasting + entries_for_upcasting,
946+
entries_ignoring_upcasting,
947+
entries_for_upcasting,
948+
upcasting_cost_percent: entries_for_upcasting as f64
949+
/ entries_ignoring_upcasting as f64
950+
* 100.,
949951
},
950952
)
951953
}

compiler/rustc_session/src/code_stats.rs

+31-19
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,21 @@ pub struct TypeSizeInfo {
6868

6969
pub struct VTableSizeInfo {
7070
pub trait_name: String,
71-
pub size_words_without_upcasting: usize,
72-
pub size_words_with_upcasting: usize,
73-
pub difference_words: usize,
74-
pub difference_percent: f64,
71+
72+
/// Number of entries in a vtable with the current algorithm
73+
/// (i.e. with upcasting).
74+
pub entries: usize,
75+
76+
/// Number of entries in a vtable, as-if we did not have trait upcasting.
77+
pub entries_ignoring_upcasting: usize,
78+
79+
/// Number of entries in a vtable needed solely for upcasting
80+
/// (i.e. `entries - entries_ignoring_upcasting`).
81+
pub entries_for_upcasting: usize,
82+
83+
/// Cost of having upcasting in % relative to the number of entries without
84+
/// upcasting (i.e. `entries_for_upcasting / entries_ignoring_upcasting * 100%`).
85+
pub upcasting_cost_percent: f64,
7586
}
7687

7788
#[derive(Default)]
@@ -216,25 +227,26 @@ impl CodeStats {
216227
}
217228

218229
pub fn print_vtable_sizes(&self, crate_name: &str) {
219-
let mut rr = std::mem::take(&mut *self.vtable_sizes.lock()).into_iter().collect::<Vec<_>>();
220-
221-
rr.sort_by(|(_, stats_a), (_, stats_b)| {
222-
stats_b.difference_percent.total_cmp(&stats_a.difference_percent)
230+
let mut infos = std::mem::take(&mut *self.vtable_sizes.lock())
231+
.into_iter()
232+
.map(|(_did, stats)| stats)
233+
.collect::<Vec<_>>();
234+
235+
// Sort by the cost % in reverse order (from biggest to smallest)
236+
infos.sort_by(|a, b| {
237+
a.upcasting_cost_percent.total_cmp(&b.upcasting_cost_percent).reverse()
223238
});
224239

225-
for (
226-
_,
227-
VTableSizeInfo {
228-
trait_name,
229-
size_words_without_upcasting,
230-
size_words_with_upcasting,
231-
difference_words,
232-
difference_percent,
233-
},
234-
) in rr
240+
for VTableSizeInfo {
241+
trait_name,
242+
entries,
243+
entries_ignoring_upcasting,
244+
entries_for_upcasting,
245+
upcasting_cost_percent,
246+
} in infos
235247
{
236248
println!(
237-
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}" }}"#
249+
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}" }}"#
238250
);
239251
}
240252
}

0 commit comments

Comments
 (0)