Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shrink metadata size #28521

Merged
merged 11 commits into from
Oct 3, 2015
75 changes: 45 additions & 30 deletions src/librbml/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -467,37 +467,44 @@ pub mod reader {
f(&d.data[d.start..d.end])
}


pub fn doc_as_u8(d: Doc) -> u8 {
assert_eq!(d.end, d.start + 1);
d.data[d.start]
}

pub fn doc_as_u16(d: Doc) -> u16 {
assert_eq!(d.end, d.start + 2);
let mut b = [0; 2];
bytes::copy_memory(&d.data[d.start..d.end], &mut b);
unsafe { (*(b.as_ptr() as *const u16)).to_be() }
}

pub fn doc_as_u32(d: Doc) -> u32 {
assert_eq!(d.end, d.start + 4);
let mut b = [0; 4];
bytes::copy_memory(&d.data[d.start..d.end], &mut b);
unsafe { (*(b.as_ptr() as *const u32)).to_be() }
}

pub fn doc_as_u64(d: Doc) -> u64 {
assert_eq!(d.end, d.start + 8);
let mut b = [0; 8];
bytes::copy_memory(&d.data[d.start..d.end], &mut b);
unsafe { (*(b.as_ptr() as *const u64)).to_be() }
if d.end >= 8 {
// For performance, we read 8 big-endian bytes,
// and mask off the junk if there is any. This
// obviously won't work on the first 8 bytes
// of a file - we will fall of the start
// of the page and segfault.

let mut b = [0; 8];
bytes::copy_memory(&d.data[d.end-8..d.end], &mut b);
let data = unsafe { (*(b.as_ptr() as *const u64)).to_be() };
let len = d.end - d.start;
if len < 8 {
data & ((1<<(len*8))-1)
} else {
data
}
} else {
let mut result = 0;
for b in &d.data[d.start..d.end] {
result = (result<<8) + (*b as u64);
}
result
}
}

pub fn doc_as_i8(d: Doc) -> i8 { doc_as_u8(d) as i8 }
pub fn doc_as_i16(d: Doc) -> i16 { doc_as_u16(d) as i16 }
pub fn doc_as_i32(d: Doc) -> i32 { doc_as_u32(d) as i32 }
pub fn doc_as_i64(d: Doc) -> i64 { doc_as_u64(d) as i64 }
#[inline] pub fn doc_as_u16(d: Doc) -> u16 { doc_as_u64(d) as u16 }
#[inline] pub fn doc_as_u32(d: Doc) -> u32 { doc_as_u64(d) as u32 }

#[inline] pub fn doc_as_i8(d: Doc) -> i8 { doc_as_u8(d) as i8 }
#[inline] pub fn doc_as_i16(d: Doc) -> i16 { doc_as_u16(d) as i16 }
#[inline] pub fn doc_as_i32(d: Doc) -> i32 { doc_as_u32(d) as i32 }
#[inline] pub fn doc_as_i64(d: Doc) -> i64 { doc_as_u64(d) as i64 }

pub struct Decoder<'a> {
parent: Doc<'a>,
Expand Down Expand Up @@ -907,7 +914,7 @@ pub mod writer {
}
}

fn write_vuint<W: Write>(w: &mut W, n: usize) -> EncodeResult {
pub fn write_vuint<W: Write>(w: &mut W, n: usize) -> EncodeResult {
if n < 0x7f { return write_sized_vuint(w, n, 1); }
if n < 0x4000 { return write_sized_vuint(w, n, 2); }
if n < 0x200000 { return write_sized_vuint(w, n, 3); }
Expand Down Expand Up @@ -996,35 +1003,43 @@ pub mod writer {

pub fn wr_tagged_u64(&mut self, tag_id: usize, v: u64) -> EncodeResult {
let bytes: [u8; 8] = unsafe { mem::transmute(v.to_be()) };
self.wr_tagged_bytes(tag_id, &bytes)
// tagged integers are emitted in big-endian, with no
// leading zeros.
let leading_zero_bytes = v.leading_zeros()/8;
self.wr_tagged_bytes(tag_id, &bytes[leading_zero_bytes as usize..])
}

pub fn wr_tagged_u32(&mut self, tag_id: usize, v: u32) -> EncodeResult{
let bytes: [u8; 4] = unsafe { mem::transmute(v.to_be()) };
self.wr_tagged_bytes(tag_id, &bytes)
#[inline]
pub fn wr_tagged_u32(&mut self, tag_id: usize, v: u32) -> EncodeResult {
self.wr_tagged_u64(tag_id, v as u64)
}

#[inline]
pub fn wr_tagged_u16(&mut self, tag_id: usize, v: u16) -> EncodeResult {
let bytes: [u8; 2] = unsafe { mem::transmute(v.to_be()) };
self.wr_tagged_bytes(tag_id, &bytes)
self.wr_tagged_u64(tag_id, v as u64)
}

#[inline]
pub fn wr_tagged_u8(&mut self, tag_id: usize, v: u8) -> EncodeResult {
self.wr_tagged_bytes(tag_id, &[v])
}

#[inline]
pub fn wr_tagged_i64(&mut self, tag_id: usize, v: i64) -> EncodeResult {
self.wr_tagged_u64(tag_id, v as u64)
}

#[inline]
pub fn wr_tagged_i32(&mut self, tag_id: usize, v: i32) -> EncodeResult {
self.wr_tagged_u32(tag_id, v as u32)
}

#[inline]
pub fn wr_tagged_i16(&mut self, tag_id: usize, v: i16) -> EncodeResult {
self.wr_tagged_u16(tag_id, v as u16)
}

#[inline]
pub fn wr_tagged_i8(&mut self, tag_id: usize, v: i8) -> EncodeResult {
self.wr_tagged_bytes(tag_id, &[v as u8])
}
Expand Down
61 changes: 20 additions & 41 deletions src/librustc/metadata/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ pub const tag_items_data_parent_item: usize = 0x28;
pub const tag_items_data_item_is_tuple_struct_ctor: usize = 0x29;

pub const tag_items_closure_kind: usize = 0x2a;

pub const tag_items_closure_ty: usize = 0x2b;
pub const tag_def_key: usize = 0x2c;

pub const tag_index: usize = 0x2c;

pub const tag_def_key: usize = 0x2d;
// GAP 0x2d 0x2e

// GAP 0x2e
pub const tag_index: usize = 0x110; // top-level only
pub const tag_xref_index: usize = 0x111; // top-level only
pub const tag_xref_data: usize = 0x112; // top-level only

pub const tag_meta_item_name_value: usize = 0x2f;

Expand Down Expand Up @@ -80,8 +80,6 @@ pub const tag_crate_dep_crate_name: usize = 0x36;
pub const tag_crate_dep_hash: usize = 0x37;
pub const tag_crate_dep_explicitly_linked: usize = 0x38; // top-level only

pub const tag_mod_impl: usize = 0x39;

pub const tag_item_trait_item: usize = 0x3a;

pub const tag_item_trait_ref: usize = 0x3b;
Expand All @@ -95,7 +93,6 @@ pub const tag_path_len: usize = 0x3e;
pub const tag_path_elem_mod: usize = 0x3f;
pub const tag_path_elem_name: usize = 0x40;
pub const tag_item_field: usize = 0x41;
pub const tag_item_field_origin: usize = 0x42;

pub const tag_item_variances: usize = 0x43;
/*
Expand Down Expand Up @@ -125,39 +122,27 @@ enum_from_u32! {

tag_tree = 0x51,

tag_id_range = 0x52,

// GAP 0x52
tag_table = 0x53,
// GAP 0x54, 0x55
tag_table_def = 0x56,
tag_table_node_type = 0x57,
tag_table_item_subst = 0x58,
tag_table_freevars = 0x59,
tag_table_tcache = 0x5a,
tag_table_param_defs = 0x5b,
tag_table_mutbl = 0x5c,
tag_table_last_use = 0x5d,
tag_table_spill = 0x5e,
// GAP 0x5a, 0x5b, 0x5c, 0x5d, 0x5e
tag_table_method_map = 0x5f,
tag_table_vtable_map = 0x60,
// GAP 0x60
tag_table_adjustments = 0x61,
tag_table_moves_map = 0x62,
tag_table_capture_map = 0x63,
// GAP 0x64, 0x65
// GAP 0x62, 0x63, 0x64, 0x65
tag_table_upvar_capture_map = 0x66,
tag_table_capture_modes = 0x67,
// GAP 0x68
// GAP 0x67, 0x68
tag_table_const_qualif = 0x69,
tag_table_cast_kinds = 0x6a,
}
}

pub const tag_item_trait_item_sort: usize = 0x70;

pub const tag_item_trait_parent_sort: usize = 0x71;

pub const tag_item_impl_type_basename: usize = 0x72;

pub const tag_crate_triple: usize = 0x105; // top-level only

pub const tag_dylib_dependency_formats: usize = 0x106; // top-level only
Expand All @@ -177,23 +162,17 @@ pub const tag_lang_items_missing: usize = 0x76;

pub const tag_item_unnamed_field: usize = 0x77;
pub const tag_items_data_item_visibility: usize = 0x78;

pub const tag_item_method_tps: usize = 0x79;
pub const tag_item_method_fty: usize = 0x7a;

pub const tag_items_data_item_inherent_impl: usize = 0x79;
// GAP 0x7a
pub const tag_mod_child: usize = 0x7b;
pub const tag_misc_info: usize = 0x108; // top-level only
pub const tag_misc_info_crate_items: usize = 0x7c;

// GAP 0x7d
pub const tag_item_impl_vtables: usize = 0x7e;

pub const tag_impls: usize = 0x109; // top-level only
pub const tag_impls_impl: usize = 0x7f;
pub const tag_impls_impl_trait_def_id: usize = 0x8d;
pub const tag_impls_trait: usize = 0x7d;
pub const tag_impls_trait_impl: usize = 0x7e;

pub const tag_items_data_item_inherent_impl: usize = 0x80;
pub const tag_items_data_item_extension_impl: usize = 0x81;
// GAP 0x7f, 0x80, 0x81

pub const tag_native_libraries: usize = 0x10a; // top-level only
pub const tag_native_libraries_lib: usize = 0x82;
Expand All @@ -220,10 +199,10 @@ pub struct LinkMeta {

pub const tag_struct_fields: usize = 0x10d; // top-level only
pub const tag_struct_field: usize = 0x8a;
// GAP 0x8b

pub const tag_items_data_item_struct_ctor: usize = 0x8b;
pub const tag_attribute_is_sugared_doc: usize = 0x8c;

// GAP 0x8d
pub const tag_items_data_region: usize = 0x8e;

pub const tag_region_param_def: usize = 0x8f;
Expand All @@ -237,9 +216,9 @@ pub const tag_type_param_def: usize = 0x94;
pub const tag_item_generics: usize = 0x95;
pub const tag_method_ty_generics: usize = 0x96;

pub const tag_predicate: usize = 0x97;
pub const tag_predicate_space: usize = 0x98;
pub const tag_predicate_data: usize = 0x99;
pub const tag_type_predicate: usize = 0x97;
pub const tag_self_predicate: usize = 0x98;
pub const tag_fn_predicate: usize = 0x99;

pub const tag_unsafety: usize = 0x9a;

Expand Down
1 change: 1 addition & 0 deletions src/librustc/metadata/creader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ impl<'a> CrateReader<'a> {
local_path: RefCell::new(SmallVector::zero()),
local_def_path: RefCell::new(vec![]),
index: decoder::load_index(metadata.as_slice()),
xref_index: decoder::load_xrefs(metadata.as_slice()),
data: metadata,
cnum_map: RefCell::new(cnum_map),
cnum: cnum,
Expand Down
6 changes: 0 additions & 6 deletions src/librustc/metadata/csearch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,6 @@ pub fn get_associated_consts<'tcx>(tcx: &ty::ctxt<'tcx>, def: DefId)
decoder::get_associated_consts(cstore.intr.clone(), &*cdata, def.index, tcx)
}

pub fn get_type_name_if_impl(cstore: &cstore::CStore, def: DefId)
-> Option<ast::Name> {
let cdata = cstore.get_crate_data(def.krate);
decoder::get_type_name_if_impl(&*cdata, def.index)
}

pub fn get_methods_if_impl(cstore: &cstore::CStore,
def: DefId)
-> Option<Vec<MethodInfo> > {
Expand Down
2 changes: 2 additions & 0 deletions src/librustc/metadata/cstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ pub struct crate_metadata {
pub codemap_import_info: RefCell<Vec<ImportedFileMap>>,
pub span: codemap::Span,
pub staged_api: bool,

pub index: index::Index,
pub xref_index: index::DenseIndex,

/// Flag if this crate is required by an rlib version of this crate, or in
/// other words whether it was explicitly linked to. An example of a crate
Expand Down
Loading