Skip to content

Commit

Permalink
Pre-allocate vec for rlib metadata reading
Browse files Browse the repository at this point in the history
Reduces the time spent during the copy from ~9% to ~1% for helloworld

cc #878
  • Loading branch information
bjorn3 committed Mar 14, 2020
1 parent fe0e2ae commit 0c1dcb0
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::convert::TryFrom;
use std::fs::File;
use std::path::Path;

Expand All @@ -24,7 +25,10 @@ impl MetadataLoader for CraneliftMetadataLoader {
while let Some(entry_result) = archive.next_entry() {
let mut entry = entry_result.map_err(|e| format!("{:?}", e))?;
if entry.header().identifier() == METADATA_FILENAME.as_bytes() {
let mut buf = Vec::new();
let mut buf = Vec::with_capacity(
usize::try_from(entry.header().size())
.expect("Rlib metadata file too big to load into memory.")
);
::std::io::copy(&mut entry, &mut buf).map_err(|e| format!("{:?}", e))?;
let buf: OwningRef<Vec<u8>, [u8]> = OwningRef::new(buf).into();
return Ok(rustc_erase_owner!(buf.map_owner_box()));
Expand Down

0 comments on commit 0c1dcb0

Please sign in to comment.