Skip to content

Commit 78e2503

Browse files
committed
librustc: Stop looking in metadata in type contents.
4x improvement in pre-trans compile time for rustc.
1 parent d2f8d4c commit 78e2503

File tree

7 files changed

+61
-9
lines changed

7 files changed

+61
-9
lines changed

src/librustc/lint/builtin.rs

+1
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ impl LintPass for UnusedAttribute {
593593
"static_assert",
594594
"thread_local",
595595
"no_debug",
596+
"unsafe_no_drop_flag",
596597

597598
// used in resolve
598599
"prelude_import",

src/librustc/metadata/common.rs

+2
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,8 @@ pub static tag_reachable_extern_fn_id: uint = 0x91;
214214

215215
pub static tag_items_data_item_stability: uint = 0x92;
216216

217+
pub static tag_items_data_item_repr: uint = 0x93;
218+
217219
#[deriving(Clone, Show)]
218220
pub struct LinkMeta {
219221
pub crate_name: String,

src/librustc/metadata/csearch.rs

+6
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,12 @@ pub fn get_stability(cstore: &cstore::CStore,
350350
decoder::get_stability(&*cdata, def.node)
351351
}
352352

353+
pub fn get_repr_attrs(cstore: &cstore::CStore, def: ast::DefId)
354+
-> Vec<attr::ReprAttr> {
355+
let cdata = cstore.get_crate_data(def.krate);
356+
decoder::get_repr_attrs(&*cdata, def.node)
357+
}
358+
353359
pub fn is_associated_type(cstore: &cstore::CStore, def: ast::DefId) -> bool {
354360
let cdata = cstore.get_crate_data(def.krate);
355361
decoder::is_associated_type(&*cdata, def.node)

src/librustc/metadata/decoder.rs

+11
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,17 @@ pub fn get_stability(cdata: Cmd, id: ast::NodeId) -> Option<attr::Stability> {
384384
})
385385
}
386386

387+
pub fn get_repr_attrs(cdata: Cmd, id: ast::NodeId) -> Vec<attr::ReprAttr> {
388+
let item = lookup_item(id, cdata.data());
389+
match reader::maybe_get_doc(item, tag_items_data_item_repr).map(|doc| {
390+
let mut decoder = reader::Decoder::new(doc);
391+
Decodable::decode(&mut decoder).unwrap()
392+
}) {
393+
Some(attrs) => attrs,
394+
None => Vec::new(),
395+
}
396+
}
397+
387398
pub fn get_impl_trait(cdata: Cmd,
388399
id: ast::NodeId,
389400
tcx: &ty::ctxt) -> Option<Rc<ty::TraitRef>>

src/librustc/metadata/encoder.rs

+16
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ fn encode_enum_variant_info(ecx: &EncodeContext,
332332
encode_parent_item(rbml_w, local_def(id));
333333
encode_visibility(rbml_w, variant.node.vis);
334334
encode_attributes(rbml_w, variant.node.attrs.as_slice());
335+
encode_repr_attrs(rbml_w, ecx, variant.node.attrs.as_slice());
335336

336337
let stab = stability::lookup(ecx.tcx, ast_util::local_def(variant.node.id));
337338
encode_stability(rbml_w, stab);
@@ -948,6 +949,19 @@ fn encode_method_argument_names(rbml_w: &mut Encoder,
948949
rbml_w.end_tag();
949950
}
950951

952+
fn encode_repr_attrs(rbml_w: &mut Encoder,
953+
ecx: &EncodeContext,
954+
attrs: &[Attribute]) {
955+
let mut repr_attrs = Vec::new();
956+
for attr in attrs.iter() {
957+
repr_attrs.extend(attr::find_repr_attrs(ecx.tcx.sess.diagnostic(),
958+
attr).into_iter());
959+
}
960+
rbml_w.start_tag(tag_items_data_item_repr);
961+
repr_attrs.encode(rbml_w);
962+
rbml_w.end_tag();
963+
}
964+
951965
fn encode_inlined_item(ecx: &EncodeContext,
952966
rbml_w: &mut Encoder,
953967
ii: InlinedItemRef) {
@@ -1137,6 +1151,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
11371151
encode_bounds_and_type(rbml_w, ecx, &lookup_item_type(tcx, def_id));
11381152
encode_name(rbml_w, item.ident.name);
11391153
encode_attributes(rbml_w, item.attrs.as_slice());
1154+
encode_repr_attrs(rbml_w, ecx, item.attrs.as_slice());
11401155
for v in (*enum_definition).variants.iter() {
11411156
encode_variant_id(rbml_w, local_def(v.node.id));
11421157
}
@@ -1183,6 +1198,7 @@ fn encode_info_for_item(ecx: &EncodeContext,
11831198
encode_path(rbml_w, path.clone());
11841199
encode_stability(rbml_w, stab);
11851200
encode_visibility(rbml_w, vis);
1201+
encode_repr_attrs(rbml_w, ecx, item.attrs.as_slice());
11861202

11871203
/* Encode def_ids for each field and method
11881204
for methods, write all the stuff get_trait_method

src/librustc/middle/ty.rs

+23-7
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,9 @@ pub struct ctxt<'tcx> {
583583
/// Caches the results of trait selection. This cache is used
584584
/// for things that do not have to do with the parameters in scope.
585585
pub selection_cache: traits::SelectionCache,
586+
587+
/// Caches the representation hints for struct definitions.
588+
pub repr_hint_cache: RefCell<DefIdMap<Rc<Vec<attr::ReprAttr>>>>,
586589
}
587590

588591
pub enum tbox_flag {
@@ -1533,6 +1536,7 @@ pub fn mk_ctxt<'tcx>(s: Session,
15331536
associated_types: RefCell::new(DefIdMap::new()),
15341537
trait_associated_types: RefCell::new(DefIdMap::new()),
15351538
selection_cache: traits::SelectionCache::new(),
1539+
repr_hint_cache: RefCell::new(DefIdMap::new()),
15361540
}
15371541
}
15381542

@@ -4326,7 +4330,7 @@ pub fn ty_dtor(cx: &ctxt, struct_id: DefId) -> DtorKind {
43264330
}
43274331

43284332
pub fn has_dtor(cx: &ctxt, struct_id: DefId) -> bool {
4329-
ty_dtor(cx, struct_id).is_present()
4333+
cx.destructor_for_type.borrow().contains_key(&struct_id)
43304334
}
43314335

43324336
pub fn with_path<T>(cx: &ctxt, id: ast::DefId, f: |ast_map::PathElems| -> T) -> T {
@@ -4513,14 +4517,26 @@ pub fn lookup_simd(tcx: &ctxt, did: DefId) -> bool {
45134517
}
45144518

45154519
/// Obtain the representation annotation for a struct definition.
4516-
pub fn lookup_repr_hints(tcx: &ctxt, did: DefId) -> Vec<attr::ReprAttr> {
4517-
let mut acc = Vec::new();
4520+
pub fn lookup_repr_hints(tcx: &ctxt, did: DefId) -> Rc<Vec<attr::ReprAttr>> {
4521+
match tcx.repr_hint_cache.borrow().find(&did) {
4522+
None => {}
4523+
Some(ref hints) => return (*hints).clone(),
4524+
}
45184525

4519-
ty::each_attr(tcx, did, |meta| {
4520-
acc.extend(attr::find_repr_attrs(tcx.sess.diagnostic(), meta).into_iter());
4521-
true
4522-
});
4526+
let acc = if did.krate == LOCAL_CRATE {
4527+
let mut acc = Vec::new();
4528+
ty::each_attr(tcx, did, |meta| {
4529+
acc.extend(attr::find_repr_attrs(tcx.sess.diagnostic(),
4530+
meta).into_iter());
4531+
true
4532+
});
4533+
acc
4534+
} else {
4535+
csearch::get_repr_attrs(&tcx.sess.cstore, did)
4536+
};
45234537

4538+
let acc = Rc::new(acc);
4539+
tcx.repr_hint_cache.borrow_mut().insert(did, acc.clone());
45244540
acc
45254541
}
45264542

src/libsyntax/attr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ fn int_type_of_word(s: &str) -> Option<IntType> {
508508
}
509509
}
510510

511-
#[deriving(PartialEq, Show)]
511+
#[deriving(PartialEq, Show, Encodable, Decodable)]
512512
pub enum ReprAttr {
513513
ReprAny,
514514
ReprInt(Span, IntType),
@@ -527,7 +527,7 @@ impl ReprAttr {
527527
}
528528
}
529529

530-
#[deriving(Eq, Hash, PartialEq, Show)]
530+
#[deriving(Eq, Hash, PartialEq, Show, Encodable, Decodable)]
531531
pub enum IntType {
532532
SignedInt(ast::IntTy),
533533
UnsignedInt(ast::UintTy)

0 commit comments

Comments
 (0)