Skip to content

Commit

Permalink
metadata: Flatten tag_table_id and tag_table_val tags.
Browse files Browse the repository at this point in the history
This avoids a biggish eight-byte `tag_table_id` tag in favor of
autoserialized integer tags, which are smaller and can be later
used to encode them in the optimal number of bytes. `NodeId` was
u32 after all.

Previously:

                       <------------- len1 -------------->
    tag_table_* <len1> tag_table_id 88 <nodeid in 8 bytes>
                       tag_table_val <len2> <actual data>
                                            <-- len2 --->

Now:

                      <--------------- len --------------->
    tag_table_* <len> U32 <nodeid in 4 bytes> <actual data>
  • Loading branch information
lifthrasiir committed Mar 3, 2015
1 parent 7b6e43c commit 36a09a1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 65 deletions.
3 changes: 1 addition & 2 deletions src/librustc/metadata/common.rs
Expand Up @@ -127,8 +127,7 @@ pub enum astencode_tag { // Reserves 0x50 -- 0x6f
tag_id_range = 0x52,

tag_table = 0x53,
tag_table_id = 0x54,
tag_table_val = 0x55,
// GAP 0x54, 0x55
tag_table_def = 0x56,
tag_table_node_type = 0x57,
tag_table_item_subst = 0x58,
Expand Down
97 changes: 34 additions & 63 deletions src/librustc/middle/astencode.rs
Expand Up @@ -413,9 +413,8 @@ fn decode_ast(par_doc: rbml::Doc) -> ast::InlinedItem {
// ______________________________________________________________________
// Encoding and decoding of ast::def

fn decode_def(dcx: &DecodeContext, doc: rbml::Doc) -> def::Def {
let mut dsr = reader::Decoder::new(doc);
let def: def::Def = Decodable::decode(&mut dsr).unwrap();
fn decode_def(dcx: &DecodeContext, dsr: &mut reader::Decoder) -> def::Def {
let def: def::Def = Decodable::decode(dsr).unwrap();
def.tr(dcx)
}

Expand Down Expand Up @@ -1114,7 +1113,7 @@ impl<'a> write_tag_and_id for Encoder<'a> {
}

fn id(&mut self, id: ast::NodeId) {
self.wr_tagged_u64(c::tag_table_id as uint, id as u64);
id.encode(self).unwrap();
}
}

Expand Down Expand Up @@ -1151,51 +1150,44 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext,
if let Some(def) = tcx.def_map.borrow().get(&id).map(|d| d.full_def()) {
rbml_w.tag(c::tag_table_def, |rbml_w| {
rbml_w.id(id);
rbml_w.tag(c::tag_table_val, |rbml_w| def.encode(rbml_w).unwrap());
def.encode(rbml_w).unwrap();
})
}

if let Some(ty) = tcx.node_types.borrow().get(&id) {
rbml_w.tag(c::tag_table_node_type, |rbml_w| {
rbml_w.id(id);
rbml_w.tag(c::tag_table_val, |rbml_w| {
rbml_w.emit_ty(ecx, *ty);
})
rbml_w.emit_ty(ecx, *ty);
})
}

if let Some(item_substs) = tcx.item_substs.borrow().get(&id) {
rbml_w.tag(c::tag_table_item_subst, |rbml_w| {
rbml_w.id(id);
rbml_w.tag(c::tag_table_val, |rbml_w| {
rbml_w.emit_substs(ecx, &item_substs.substs);
})
rbml_w.emit_substs(ecx, &item_substs.substs);
})
}

if let Some(fv) = tcx.freevars.borrow().get(&id) {
rbml_w.tag(c::tag_table_freevars, |rbml_w| {
rbml_w.id(id);
rbml_w.tag(c::tag_table_val, |rbml_w| {
rbml_w.emit_from_vec(fv, |rbml_w, fv_entry| {
Ok(encode_freevar_entry(rbml_w, fv_entry))
});
})
rbml_w.emit_from_vec(fv, |rbml_w, fv_entry| {
Ok(encode_freevar_entry(rbml_w, fv_entry))
});
});

for freevar in fv {
rbml_w.tag(c::tag_table_upvar_capture_map, |rbml_w| {
rbml_w.id(id);
rbml_w.tag(c::tag_table_val, |rbml_w| {
let var_id = freevar.def.def_id().node;
let upvar_id = ty::UpvarId {
var_id: var_id,
closure_expr_id: id
};
let upvar_capture = tcx.upvar_capture_map.borrow()[upvar_id].clone();
var_id.encode(rbml_w);
upvar_capture.encode(rbml_w);
})

let var_id = freevar.def.def_id().node;
let upvar_id = ty::UpvarId {
var_id: var_id,
closure_expr_id: id
};
let upvar_capture = tcx.upvar_capture_map.borrow()[upvar_id].clone();
var_id.encode(rbml_w);
upvar_capture.encode(rbml_w);
})
}
}
Expand All @@ -1204,37 +1196,29 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext,
if let Some(type_scheme) = tcx.tcache.borrow().get(&lid) {
rbml_w.tag(c::tag_table_tcache, |rbml_w| {
rbml_w.id(id);
rbml_w.tag(c::tag_table_val, |rbml_w| {
rbml_w.emit_type_scheme(ecx, type_scheme.clone());
})
rbml_w.emit_type_scheme(ecx, type_scheme.clone());
})
}

if let Some(type_param_def) = tcx.ty_param_defs.borrow().get(&id) {
rbml_w.tag(c::tag_table_param_defs, |rbml_w| {
rbml_w.id(id);
rbml_w.tag(c::tag_table_val, |rbml_w| {
rbml_w.emit_type_param_def(ecx, type_param_def)
})
rbml_w.emit_type_param_def(ecx, type_param_def)
})
}

let method_call = MethodCall::expr(id);
if let Some(method) = tcx.method_map.borrow().get(&method_call) {
rbml_w.tag(c::tag_table_method_map, |rbml_w| {
rbml_w.id(id);
rbml_w.tag(c::tag_table_val, |rbml_w| {
encode_method_callee(ecx, rbml_w, method_call.adjustment, method)
})
encode_method_callee(ecx, rbml_w, method_call.adjustment, method)
})
}

if let Some(trait_ref) = tcx.object_cast_map.borrow().get(&id) {
rbml_w.tag(c::tag_table_object_cast_map, |rbml_w| {
rbml_w.id(id);
rbml_w.tag(c::tag_table_val, |rbml_w| {
rbml_w.emit_trait_ref(ecx, &*trait_ref.0);
})
rbml_w.emit_trait_ref(ecx, &*trait_ref.0);
})
}

Expand All @@ -1245,9 +1229,7 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext,
if let Some(method) = tcx.method_map.borrow().get(&method_call) {
rbml_w.tag(c::tag_table_method_map, |rbml_w| {
rbml_w.id(id);
rbml_w.tag(c::tag_table_val, |rbml_w| {
encode_method_callee(ecx, rbml_w, method_call.adjustment, method)
})
encode_method_callee(ecx, rbml_w, method_call.adjustment, method)
})
}
}
Expand All @@ -1258,10 +1240,8 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext,
if let Some(method) = tcx.method_map.borrow().get(&method_call) {
rbml_w.tag(c::tag_table_method_map, |rbml_w| {
rbml_w.id(id);
rbml_w.tag(c::tag_table_val, |rbml_w| {
encode_method_callee(ecx, rbml_w,
method_call.adjustment, method)
})
encode_method_callee(ecx, rbml_w,
method_call.adjustment, method)
})
}
}
Expand All @@ -1273,36 +1253,28 @@ fn encode_side_tables_for_id(ecx: &e::EncodeContext,

rbml_w.tag(c::tag_table_adjustments, |rbml_w| {
rbml_w.id(id);
rbml_w.tag(c::tag_table_val, |rbml_w| {
rbml_w.emit_auto_adjustment(ecx, adjustment);
})
rbml_w.emit_auto_adjustment(ecx, adjustment);
})
}

if let Some(closure_type) = tcx.closure_tys.borrow().get(&ast_util::local_def(id)) {
rbml_w.tag(c::tag_table_closure_tys, |rbml_w| {
rbml_w.id(id);
rbml_w.tag(c::tag_table_val, |rbml_w| {
rbml_w.emit_closure_type(ecx, closure_type);
})
rbml_w.emit_closure_type(ecx, closure_type);
})
}

if let Some(closure_kind) = tcx.closure_kinds.borrow().get(&ast_util::local_def(id)) {
rbml_w.tag(c::tag_table_closure_kinds, |rbml_w| {
rbml_w.id(id);
rbml_w.tag(c::tag_table_val, |rbml_w| {
encode_closure_kind(rbml_w, *closure_kind)
})
encode_closure_kind(rbml_w, *closure_kind)
})
}

for &qualif in tcx.const_qualif_map.borrow().get(&id).iter() {
rbml_w.tag(c::tag_table_const_qualif, |rbml_w| {
rbml_w.id(id);
rbml_w.tag(c::tag_table_val, |rbml_w| {
qualif.encode(rbml_w).unwrap()
})
qualif.encode(rbml_w).unwrap()
})
}
}
Expand Down Expand Up @@ -1830,8 +1802,9 @@ fn decode_side_tables(dcx: &DecodeContext,
ast_doc: rbml::Doc) {
let tbl_doc = ast_doc.get(c::tag_table as uint);
reader::docs(tbl_doc, |tag, entry_doc| {
let id0 = entry_doc.get(c::tag_table_id as uint).as_int();
let id = dcx.tr_id(id0 as ast::NodeId);
let mut entry_dsr = reader::Decoder::new(entry_doc);
let id0: ast::NodeId = Decodable::decode(&mut entry_dsr).unwrap();
let id = dcx.tr_id(id0);

debug!(">> Side table document with tag 0x{:x} \
found for id {} (orig {})",
Expand All @@ -1844,13 +1817,11 @@ fn decode_side_tables(dcx: &DecodeContext,
tag));
}
Some(value) => {
let val_doc = entry_doc.get(c::tag_table_val as uint);
let mut val_dsr = reader::Decoder::new(val_doc);
let val_dsr = &mut val_dsr;
let val_dsr = &mut entry_dsr;

match value {
c::tag_table_def => {
let def = decode_def(dcx, val_doc);
let def = decode_def(dcx, val_dsr);
dcx.tcx.def_map.borrow_mut().insert(id, def::PathResolution {
base_def: def,
// This doesn't matter cross-crate.
Expand Down

0 comments on commit 36a09a1

Please sign in to comment.