Skip to content

Commit

Permalink
Rollup merge of #37267 - nnethercote:opt-write_metadata, r=eddyb
Browse files Browse the repository at this point in the history
Optimize `write_metadata`.

`write_metadata` currently generates metadata unnecessarily in some
cases, and also compresses it unnecessarily in some cases. This commit
fixes that. It speeds up three of the rustc-benchmarks by 1--4%.

r? @eddyb, who deserves much of the credit because he (a) identified the problem from the profile data I provided in #37086, and (b) explained to me how to fix it. Thank you, @eddyb!
  • Loading branch information
eddyb authored Oct 19, 2016
2 parents 9888313 + 016f69f commit f354d72
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/librustc_trans/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1250,12 +1250,27 @@ fn write_metadata(cx: &SharedCrateContext,
reachable_ids: &NodeSet) -> Vec<u8> {
use flate;

let any_library = cx.sess()
.crate_types
.borrow()
.iter()
.any(|ty| *ty != config::CrateTypeExecutable);
if !any_library {
#[derive(PartialEq, Eq, PartialOrd, Ord)]
enum MetadataKind {
None,
Uncompressed,
Compressed
}

let kind = cx.sess().crate_types.borrow().iter().map(|ty| {
match *ty {
config::CrateTypeExecutable |
config::CrateTypeStaticlib |
config::CrateTypeCdylib => MetadataKind::None,

config::CrateTypeRlib => MetadataKind::Uncompressed,

config::CrateTypeDylib |
config::CrateTypeProcMacro => MetadataKind::Compressed,
}
}).max().unwrap();

if kind == MetadataKind::None {
return Vec::new();
}

Expand All @@ -1265,6 +1280,11 @@ fn write_metadata(cx: &SharedCrateContext,
cx.link_meta(),
reachable_ids,
cx.mir_map());
if kind == MetadataKind::Uncompressed {
return metadata;
}

assert!(kind == MetadataKind::Compressed);
let mut compressed = cstore.metadata_encoding_version().to_vec();
compressed.extend_from_slice(&flate::deflate_bytes(&metadata));

Expand Down

0 comments on commit f354d72

Please sign in to comment.