Skip to content

Commit e30b683

Browse files
committed
Auto merge of #88552 - nbdd0121:vtable, r=nagisa
Stop allocating vtable entries for non-object-safe methods Current a vtable entry is allocated for all associated fns, even if the method is not object-safe: https://godbolt.org/z/h7vx6f35T As a result, each vtable for `Iterator`' currently consumes 74 `usize`s. This PR stops allocating vtable entries for those methods, reducing vtable size of each `Iterator` vtable to 7 `usize`s. Note that this PR introduces will cause more invocations of `is_vtable_safe_method`. So a perf run might be needed. If result isn't favorable then we might need to query-ify `is_vtable_safe_method`.
2 parents e2750ba + 97214ee commit e30b683

File tree

8 files changed

+109
-42
lines changed

8 files changed

+109
-42
lines changed

compiler/rustc_middle/src/query/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,12 @@ rustc_queries! {
996996
desc { |tcx| "checking if item has mir available: `{}`", tcx.def_path_str(key) }
997997
}
998998

999+
query own_existential_vtable_entries(
1000+
key: ty::PolyExistentialTraitRef<'tcx>
1001+
) -> &'tcx [DefId] {
1002+
desc { |tcx| "finding all existential vtable entries for trait {}", tcx.def_path_str(key.def_id()) }
1003+
}
1004+
9991005
query vtable_entries(key: ty::PolyTraitRef<'tcx>)
10001006
-> &'tcx [ty::VtblEntry<'tcx>] {
10011007
desc { |tcx| "finding all vtable entries for trait {}", tcx.def_path_str(key.def_id()) }

compiler/rustc_query_impl/src/keys.rs

+10
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,16 @@ impl<'tcx> Key for ty::PolyTraitRef<'tcx> {
294294
}
295295
}
296296

297+
impl<'tcx> Key for ty::PolyExistentialTraitRef<'tcx> {
298+
#[inline(always)]
299+
fn query_crate_is_local(&self) -> bool {
300+
self.def_id().krate == LOCAL_CRATE
301+
}
302+
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
303+
tcx.def_span(self.def_id())
304+
}
305+
}
306+
297307
impl<'tcx> Key for (ty::PolyTraitRef<'tcx>, ty::PolyTraitRef<'tcx>) {
298308
#[inline(always)]
299309
fn query_crate_is_local(&self) -> bool {

compiler/rustc_trait_selection/src/traits/mod.rs

+35-15
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,31 @@ fn dump_vtable_entries<'tcx>(
625625
tcx.sess.struct_span_err(sp, &msg).emit();
626626
}
627627

628+
fn own_existential_vtable_entries<'tcx>(
629+
tcx: TyCtxt<'tcx>,
630+
trait_ref: ty::PolyExistentialTraitRef<'tcx>,
631+
) -> &'tcx [DefId] {
632+
let trait_methods = tcx
633+
.associated_items(trait_ref.def_id())
634+
.in_definition_order()
635+
.filter(|item| item.kind == ty::AssocKind::Fn);
636+
// Now list each method's DefId (for within its trait).
637+
let own_entries = trait_methods.filter_map(move |trait_method| {
638+
debug!("own_existential_vtable_entry: trait_method={:?}", trait_method);
639+
let def_id = trait_method.def_id;
640+
641+
// Some methods cannot be called on an object; skip those.
642+
if !is_vtable_safe_method(tcx, trait_ref.def_id(), &trait_method) {
643+
debug!("own_existential_vtable_entry: not vtable safe");
644+
return None;
645+
}
646+
647+
Some(def_id)
648+
});
649+
650+
tcx.arena.alloc_from_iter(own_entries.into_iter())
651+
}
652+
628653
/// Given a trait `trait_ref`, iterates the vtable entries
629654
/// that come from `trait_ref`, including its supertraits.
630655
fn vtable_entries<'tcx>(
@@ -641,21 +666,15 @@ fn vtable_entries<'tcx>(
641666
entries.extend(COMMON_VTABLE_ENTRIES);
642667
}
643668
VtblSegment::TraitOwnEntries { trait_ref, emit_vptr } => {
644-
let trait_methods = tcx
645-
.associated_items(trait_ref.def_id())
646-
.in_definition_order()
647-
.filter(|item| item.kind == ty::AssocKind::Fn);
648-
// Now list each method's DefId and InternalSubsts (for within its trait).
649-
// If the method can never be called from this object, produce `Vacant`.
650-
let own_entries = trait_methods.map(move |trait_method| {
651-
debug!("vtable_entries: trait_method={:?}", trait_method);
652-
let def_id = trait_method.def_id;
653-
654-
// Some methods cannot be called on an object; skip those.
655-
if !is_vtable_safe_method(tcx, trait_ref.def_id(), &trait_method) {
656-
debug!("vtable_entries: not vtable safe");
657-
return VtblEntry::Vacant;
658-
}
669+
let existential_trait_ref = trait_ref
670+
.map_bound(|trait_ref| ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref));
671+
672+
// Lookup the shape of vtable for the trait.
673+
let own_existential_entries =
674+
tcx.own_existential_vtable_entries(existential_trait_ref);
675+
676+
let own_entries = own_existential_entries.iter().copied().map(|def_id| {
677+
debug!("vtable_entries: trait_method={:?}", def_id);
659678

660679
// The method may have some early-bound lifetimes; add regions for those.
661680
let substs = trait_ref.map_bound(|trait_ref| {
@@ -804,6 +823,7 @@ pub fn provide(providers: &mut ty::query::Providers) {
804823
specialization_graph_of: specialize::specialization_graph_provider,
805824
specializes: specialize::specializes,
806825
codegen_fulfill_obligation: codegen::codegen_fulfill_obligation,
826+
own_existential_vtable_entries,
807827
vtable_entries,
808828
vtable_trait_upcasting_coercion_new_vptr_slot,
809829
subst_and_check_impossible_predicates,

compiler/rustc_trait_selection/src/traits/util.rs

+17-23
Original file line numberDiff line numberDiff line change
@@ -285,15 +285,10 @@ pub fn upcast_choices(
285285
/// that come from `trait_ref`, excluding its supertraits. Used in
286286
/// computing the vtable base for an upcast trait of a trait object.
287287
pub fn count_own_vtable_entries(tcx: TyCtxt<'tcx>, trait_ref: ty::PolyTraitRef<'tcx>) -> usize {
288-
let mut entries = 0;
289-
// Count number of methods and add them to the total offset.
290-
// Skip over associated types and constants.
291-
for trait_item in tcx.associated_items(trait_ref.def_id()).in_definition_order() {
292-
if trait_item.kind == ty::AssocKind::Fn {
293-
entries += 1;
294-
}
295-
}
296-
entries
288+
let existential_trait_ref =
289+
trait_ref.map_bound(|trait_ref| ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref));
290+
let existential_trait_ref = tcx.erase_regions(existential_trait_ref);
291+
tcx.own_existential_vtable_entries(existential_trait_ref).len()
297292
}
298293

299294
/// Given an upcast trait object described by `object`, returns the
@@ -304,22 +299,21 @@ pub fn get_vtable_index_of_object_method<N>(
304299
object: &super::ImplSourceObjectData<'tcx, N>,
305300
method_def_id: DefId,
306301
) -> usize {
302+
let existential_trait_ref = object
303+
.upcast_trait_ref
304+
.map_bound(|trait_ref| ty::ExistentialTraitRef::erase_self_ty(tcx, trait_ref));
305+
let existential_trait_ref = tcx.erase_regions(existential_trait_ref);
307306
// Count number of methods preceding the one we are selecting and
308307
// add them to the total offset.
309-
// Skip over associated types and constants, as those aren't stored in the vtable.
310-
let mut entries = object.vtable_base;
311-
for trait_item in tcx.associated_items(object.upcast_trait_ref.def_id()).in_definition_order() {
312-
if trait_item.def_id == method_def_id {
313-
// The item with the ID we were given really ought to be a method.
314-
assert_eq!(trait_item.kind, ty::AssocKind::Fn);
315-
return entries;
316-
}
317-
if trait_item.kind == ty::AssocKind::Fn {
318-
entries += 1;
319-
}
320-
}
321-
322-
bug!("get_vtable_index_of_object_method: {:?} was not found", method_def_id);
308+
let index = tcx
309+
.own_existential_vtable_entries(existential_trait_ref)
310+
.iter()
311+
.copied()
312+
.position(|def_id| def_id == method_def_id)
313+
.unwrap_or_else(|| {
314+
bug!("get_vtable_index_of_object_method: {:?} was not found", method_def_id);
315+
});
316+
object.vtable_base + index
323317
}
324318

325319
pub fn closure_trait_ref_and_return_type(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// build-fail
2+
#![feature(rustc_attrs)]
3+
4+
// Ensure that non-object-safe methods in Iterator does not generate
5+
// vtable entries.
6+
7+
#[rustc_dump_vtable]
8+
trait A: Iterator {}
9+
//~^ error Vtable
10+
11+
impl<T> A for T where T: Iterator {}
12+
13+
fn foo(_a: &mut dyn A<Item=u8>) {
14+
}
15+
16+
fn main() {
17+
foo(&mut vec![0, 1, 2, 3].into_iter());
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error: Vtable entries for `<std::vec::IntoIter<u8> as A>`: [
2+
MetadataDropInPlace,
3+
MetadataSize,
4+
MetadataAlign,
5+
Method(<std::vec::IntoIter<u8> as Iterator>::next),
6+
Method(<std::vec::IntoIter<u8> as Iterator>::size_hint),
7+
Method(<std::vec::IntoIter<u8> as Iterator>::advance_by),
8+
Method(<std::vec::IntoIter<u8> as Iterator>::nth),
9+
]
10+
--> $DIR/vtable-non-object-safe.rs:8:1
11+
|
12+
LL | trait A: Iterator {}
13+
| ^^^^^^^^^^^^^^^^^^^^
14+
15+
error: aborting due to previous error
16+

src/test/ui/traits/vtable/vtable-vacant.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
// build-fail
22
#![feature(rustc_attrs)]
3+
#![feature(negative_impls)]
4+
#![allow(where_clauses_object_safety)]
35

46
// B --> A
57

68
#[rustc_dump_vtable]
79
trait A {
810
fn foo_a1(&self) {}
9-
fn foo_a2(&self) where Self: Sized {}
11+
fn foo_a2(&self) where Self: Send {}
1012
}
1113

1214
#[rustc_dump_vtable]
1315
trait B: A {
1416
//~^ error Vtable
1517
fn foo_b1(&self) {}
16-
fn foo_b2() where Self: Sized {}
18+
fn foo_b2(&self) where Self: Send {}
1719
}
1820

1921
struct S;
22+
impl !Send for S {}
2023

2124
impl A for S {}
2225
impl B for S {}

src/test/ui/traits/vtable/vtable-vacant.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ error: Vtable entries for `<S as B>`: [
77
Method(<S as B>::foo_b1),
88
Vacant,
99
]
10-
--> $DIR/vtable-vacant.rs:13:1
10+
--> $DIR/vtable-vacant.rs:15:1
1111
|
1212
LL | / trait B: A {
1313
LL | |
1414
LL | | fn foo_b1(&self) {}
15-
LL | | fn foo_b2() where Self: Sized {}
15+
LL | | fn foo_b2(&self) where Self: Send {}
1616
LL | | }
1717
| |_^
1818

0 commit comments

Comments
 (0)