Skip to content

Commit a1c921e

Browse files
Ariel Ben-Yehudaarielb1
authored andcommitted
don't duplicate item types between encoder <-> astencode
a 5% win on libcore 512576 liballoc-bb943c5a.rlib 1425106 liballoc_jemalloc-bb943c5a.rlib 10070 liballoc_system-bb943c5a.rlib 141332 libarena-bb943c5a.rlib 3611586 libcollections-bb943c5a.rlib 16293400 libcore-bb943c5a.rlib 195018 libflate-bb943c5a.rlib 231940 libfmt_macros-bb943c5a.rlib 532704 libgetopts-bb943c5a.rlib 208094 libgraphviz-bb943c5a.rlib 383522 liblibc-bb943c5a.rlib 183786 liblog-bb943c5a.rlib 658332 librand-bb943c5a.rlib 567676 librbml-bb943c5a.rlib 1376114 librustc_back-bb943c5a.rlib 37134688 librustc-bb943c5a.rlib 12826 librustc_bitflags-bb943c5a.rlib 2241942 librustc_borrowck-bb943c5a.rlib 513598 librustc_data_structures-bb943c5a.rlib 9340348 librustc_driver-bb943c5a.rlib 8880472 librustc_front-bb943c5a.rlib 1590548 librustc_lint-bb943c5a.rlib 79149202 librustc_llvm-bb943c5a.rlib 4536740 librustc_mir-bb943c5a.rlib 3528866 librustc_platform_intrinsics-bb943c5a.rlib 588514 librustc_privacy-bb943c5a.rlib 3068562 librustc_resolve-bb943c5a.rlib 13982508 librustc_trans-bb943c5a.rlib 11799790 librustc_typeck-bb943c5a.rlib 1637532 librustc_unicode-bb943c5a.rlib 15611582 librustdoc-bb943c5a.rlib 2649520 libserialize-bb943c5a.rlib 8095050 libstd-bb943c5a.rlib 29391260 libsyntax-bb943c5a.rlib 891210 libterm-bb943c5a.rlib
1 parent b742199 commit a1c921e

File tree

2 files changed

+52
-119
lines changed

2 files changed

+52
-119
lines changed

src/librustc/metadata/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ enum_from_u32! {
125125
tag_table_node_type = 0x57,
126126
tag_table_item_subst = 0x58,
127127
tag_table_freevars = 0x59,
128-
tag_table_tcache = 0x5a,
128+
// GAP 0x5a
129129
tag_table_param_defs = 0x5b,
130130
// GAP 0x5c, 0x5d, 0x5e
131131
tag_table_method_map = 0x5f,

src/librustc/middle/astencode.rs

Lines changed: 51 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use middle::def_id::{DefId, LOCAL_CRATE};
3535
use middle::privacy::{AllPublic, LastMod};
3636
use middle::region;
3737
use middle::subst;
38-
use middle::subst::VecPerParamSpace;
3938
use middle::ty::{self, Ty};
4039

4140
use syntax::{ast, ast_util, codemap};
@@ -167,6 +166,7 @@ pub fn decode_inlined_item<'tcx>(cdata: &cstore::crate_metadata,
167166
name);
168167
region::resolve_inlined_item(&tcx.sess, &tcx.region_maps, ii);
169168
decode_side_tables(dcx, ast_doc);
169+
copy_item_types(dcx, ii);
170170
match *ii {
171171
InlinedItem::Item(ref i) => {
172172
debug!(">>> DECODED ITEM >>>\n{}\n<<< DECODED ITEM <<<",
@@ -205,6 +205,17 @@ impl<'a, 'b, 'tcx> DecodeContext<'a, 'b, 'tcx> {
205205
(id.wrapping_sub(self.from_id_range.min).wrapping_add(self.to_id_range.min))
206206
}
207207

208+
/// Gets the original crate's DefId from a translated internal
209+
/// def-id.
210+
pub fn reverse_tr_id(&self, id: ast::NodeId) -> DefId {
211+
// from_id_range should be non-empty
212+
assert!(!self.from_id_range.empty());
213+
// Use wrapping arithmetic because otherwise it introduces control flow.
214+
// Maybe we should just have the control flow? -- aatch
215+
let node = id.wrapping_sub(self.to_id_range.min).wrapping_add(self.from_id_range.min);
216+
DefId { krate: self.cdata.cnum, node: node }
217+
}
218+
208219
/// Translates an EXTERNAL def-id, converting the crate number from the one used in the encoded
209220
/// data to the current crate numbers.. By external, I mean that it be translated to a
210221
/// reference to the item in its original crate, as opposed to being translated to a reference
@@ -576,36 +587,6 @@ pub fn encode_cast_kind(ebml_w: &mut Encoder, kind: cast::CastKind) {
576587
kind.encode(ebml_w).unwrap();
577588
}
578589

579-
pub trait vtable_decoder_helpers<'tcx> {
580-
fn read_vec_per_param_space<T, F>(&mut self, f: F) -> VecPerParamSpace<T> where
581-
F: FnMut(&mut Self) -> T;
582-
}
583-
584-
impl<'tcx, 'a> vtable_decoder_helpers<'tcx> for reader::Decoder<'a> {
585-
fn read_vec_per_param_space<T, F>(&mut self, mut f: F) -> VecPerParamSpace<T> where
586-
F: FnMut(&mut reader::Decoder<'a>) -> T,
587-
{
588-
let types = self.read_to_vec(|this| Ok(f(this))).unwrap();
589-
let selfs = self.read_to_vec(|this| Ok(f(this))).unwrap();
590-
let fns = self.read_to_vec(|this| Ok(f(this))).unwrap();
591-
VecPerParamSpace::new(types, selfs, fns)
592-
}
593-
}
594-
595-
// ___________________________________________________________________________
596-
//
597-
598-
fn encode_vec_per_param_space<T, F>(rbml_w: &mut Encoder,
599-
v: &subst::VecPerParamSpace<T>,
600-
mut f: F) where
601-
F: FnMut(&mut Encoder, &T),
602-
{
603-
for &space in &subst::ParamSpace::all() {
604-
rbml_w.emit_from_vec(v.get_slice(space),
605-
|rbml_w, n| Ok(f(rbml_w, n))).unwrap();
606-
}
607-
}
608-
609590
// ______________________________________________________________________
610591
// Encoding and decoding the side tables
611592

@@ -632,14 +613,10 @@ trait rbml_writer_helpers<'tcx> {
632613
fn emit_tys<'a>(&mut self, ecx: &e::EncodeContext<'a, 'tcx>, tys: &[Ty<'tcx>]);
633614
fn emit_type_param_def<'a>(&mut self, ecx: &e::EncodeContext<'a, 'tcx>,
634615
type_param_def: &ty::TypeParameterDef<'tcx>);
635-
fn emit_region_param_def(&mut self, ecx: &e::EncodeContext,
636-
region_param_def: &ty::RegionParameterDef);
637616
fn emit_predicate<'a>(&mut self, ecx: &e::EncodeContext<'a, 'tcx>,
638617
predicate: &ty::Predicate<'tcx>);
639618
fn emit_trait_ref<'a>(&mut self, ecx: &e::EncodeContext<'a, 'tcx>,
640619
ty: &ty::TraitRef<'tcx>);
641-
fn emit_type_scheme<'a>(&mut self, ecx: &e::EncodeContext<'a, 'tcx>,
642-
type_scheme: ty::TypeScheme<'tcx>);
643620
fn emit_substs<'a>(&mut self, ecx: &e::EncodeContext<'a, 'tcx>,
644621
substs: &subst::Substs<'tcx>);
645622
fn emit_existential_bounds<'b>(&mut self, ecx: &e::EncodeContext<'b,'tcx>,
@@ -688,14 +665,7 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> {
688665
type_param_def))
689666
});
690667
}
691-
fn emit_region_param_def(&mut self, ecx: &e::EncodeContext,
692-
region_param_def: &ty::RegionParameterDef) {
693-
self.emit_opaque(|this| {
694-
Ok(tyencode::enc_region_param_def(this,
695-
&ecx.ty_str_ctxt(),
696-
region_param_def))
697-
});
698-
}
668+
699669
fn emit_predicate<'b>(&mut self, ecx: &e::EncodeContext<'b, 'tcx>,
700670
predicate: &ty::Predicate<'tcx>) {
701671
self.emit_opaque(|this| {
@@ -705,32 +675,6 @@ impl<'a, 'tcx> rbml_writer_helpers<'tcx> for Encoder<'a> {
705675
});
706676
}
707677

708-
fn emit_type_scheme<'b>(&mut self,
709-
ecx: &e::EncodeContext<'b, 'tcx>,
710-
type_scheme: ty::TypeScheme<'tcx>) {
711-
use serialize::Encoder;
712-
713-
self.emit_struct("TypeScheme", 2, |this| {
714-
this.emit_struct_field("generics", 0, |this| {
715-
this.emit_struct("Generics", 2, |this| {
716-
this.emit_struct_field("types", 0, |this| {
717-
Ok(encode_vec_per_param_space(
718-
this, &type_scheme.generics.types,
719-
|this, def| this.emit_type_param_def(ecx, def)))
720-
});
721-
this.emit_struct_field("regions", 1, |this| {
722-
Ok(encode_vec_per_param_space(
723-
this, &type_scheme.generics.regions,
724-
|this, def| this.emit_region_param_def(ecx, def)))
725-
})
726-
})
727-
});
728-
this.emit_struct_field("ty", 1, |this| {
729-
Ok(this.emit_ty(ecx, type_scheme.ty))
730-
})
731-
});
732-
}
733-
734678
fn emit_existential_bounds<'b>(&mut self, ecx: &e::EncodeContext<'b,'tcx>,
735679
bounds: &ty::ExistentialBounds<'tcx>) {
736680
self.emit_opaque(|this| Ok(tyencode::enc_existential_bounds(this,
@@ -950,14 +894,6 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext,
950894
}
951895
}
952896

953-
let lid = DefId { krate: LOCAL_CRATE, node: id };
954-
if let Some(type_scheme) = tcx.tcache.borrow().get(&lid) {
955-
rbml_w.tag(c::tag_table_tcache, |rbml_w| {
956-
rbml_w.id(id);
957-
rbml_w.emit_type_scheme(ecx, type_scheme.clone());
958-
})
959-
}
960-
961897
if let Some(type_param_def) = tcx.ty_param_defs.borrow().get(&id) {
962898
rbml_w.tag(c::tag_table_param_defs, |rbml_w| {
963899
rbml_w.id(id);
@@ -1051,12 +987,8 @@ trait rbml_decoder_decoder_helpers<'tcx> {
1051987
-> ty::PolyTraitRef<'tcx>;
1052988
fn read_type_param_def<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
1053989
-> ty::TypeParameterDef<'tcx>;
1054-
fn read_region_param_def(&mut self, dcx: &DecodeContext)
1055-
-> ty::RegionParameterDef;
1056990
fn read_predicate<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
1057991
-> ty::Predicate<'tcx>;
1058-
fn read_type_scheme<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
1059-
-> ty::TypeScheme<'tcx>;
1060992
fn read_existential_bounds<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
1061993
-> ty::ExistentialBounds<'tcx>;
1062994
fn read_substs<'a, 'b>(&mut self, dcx: &DecodeContext<'a, 'b, 'tcx>)
@@ -1177,44 +1109,13 @@ impl<'a, 'tcx> rbml_decoder_decoder_helpers<'tcx> for reader::Decoder<'a> {
11771109
-> ty::TypeParameterDef<'tcx> {
11781110
self.read_ty_encoded(dcx, |decoder| decoder.parse_type_param_def())
11791111
}
1180-
fn read_region_param_def(&mut self, dcx: &DecodeContext)
1181-
-> ty::RegionParameterDef {
1182-
self.read_ty_encoded(dcx, |decoder| decoder.parse_region_param_def())
1183-
}
1112+
11841113
fn read_predicate<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
11851114
-> ty::Predicate<'tcx>
11861115
{
11871116
self.read_ty_encoded(dcx, |decoder| decoder.parse_predicate())
11881117
}
11891118

1190-
fn read_type_scheme<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
1191-
-> ty::TypeScheme<'tcx> {
1192-
self.read_struct("TypeScheme", 3, |this| {
1193-
Ok(ty::TypeScheme {
1194-
generics: this.read_struct_field("generics", 0, |this| {
1195-
this.read_struct("Generics", 2, |this| {
1196-
Ok(ty::Generics {
1197-
types:
1198-
this.read_struct_field("types", 0, |this| {
1199-
Ok(this.read_vec_per_param_space(
1200-
|this| this.read_type_param_def(dcx)))
1201-
}).unwrap(),
1202-
1203-
regions:
1204-
this.read_struct_field("regions", 1, |this| {
1205-
Ok(this.read_vec_per_param_space(
1206-
|this| this.read_region_param_def(dcx)))
1207-
}).unwrap(),
1208-
})
1209-
})
1210-
}).unwrap(),
1211-
ty: this.read_struct_field("ty", 1, |this| {
1212-
Ok(this.read_ty(dcx))
1213-
}).unwrap()
1214-
})
1215-
}).unwrap()
1216-
}
1217-
12181119
fn read_existential_bounds<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>)
12191120
-> ty::ExistentialBounds<'tcx>
12201121
{
@@ -1450,11 +1351,6 @@ fn decode_side_tables(dcx: &DecodeContext,
14501351
let ub = val_dsr.read_upvar_capture(dcx);
14511352
dcx.tcx.tables.borrow_mut().upvar_capture_map.insert(upvar_id, ub);
14521353
}
1453-
c::tag_table_tcache => {
1454-
let type_scheme = val_dsr.read_type_scheme(dcx);
1455-
let lid = DefId { krate: LOCAL_CRATE, node: id };
1456-
dcx.tcx.register_item_type(lid, type_scheme);
1457-
}
14581354
c::tag_table_param_defs => {
14591355
let bounds = val_dsr.read_type_param_def(dcx);
14601356
dcx.tcx.ty_param_defs.borrow_mut().insert(id, bounds);
@@ -1506,6 +1402,43 @@ fn decode_side_tables(dcx: &DecodeContext,
15061402
}
15071403
}
15081404

1405+
// copy the tcache entries from the original item to the new
1406+
// inlined item
1407+
fn copy_item_types(dcx: &DecodeContext, ii: &InlinedItem) {
1408+
fn copy_item_type(dcx: &DecodeContext, inlined_node: ast::NodeId) {
1409+
let inlined_did = DefId::local(inlined_node);
1410+
let remote_did = dcx.reverse_tr_id(inlined_node);
1411+
dcx.tcx.register_item_type(inlined_did,
1412+
dcx.tcx.lookup_item_type(remote_did));
1413+
1414+
}
1415+
// copy the entry for the item itself
1416+
let item_node_id = match ii {
1417+
&InlinedItem::Item(ref i) => i.id,
1418+
&InlinedItem::TraitItem(_, ref ti) => ti.id,
1419+
&InlinedItem::ImplItem(_, ref ii) => ii.id,
1420+
&InlinedItem::Foreign(ref fi) => fi.id
1421+
};
1422+
copy_item_type(dcx, item_node_id);
1423+
1424+
// copy the entries of inner items
1425+
if let &InlinedItem::Item(ref item) = ii {
1426+
match item.node {
1427+
hir::ItemEnum(ref def, _) => {
1428+
for variant in &def.variants {
1429+
copy_item_type(dcx, variant.node.id);
1430+
}
1431+
}
1432+
hir::ItemStruct(ref def, _) => {
1433+
if let Some(ctor_id) = def.ctor_id {
1434+
copy_item_type(dcx, ctor_id);
1435+
}
1436+
}
1437+
_ => {}
1438+
}
1439+
}
1440+
}
1441+
15091442
// ______________________________________________________________________
15101443
// Testing of astencode_gen
15111444

0 commit comments

Comments
 (0)