Skip to content

Commit 0c1dcb0

Browse files
committed
Pre-allocate vec for rlib metadata reading
Reduces the time spent during the copy from ~9% to ~1% for helloworld cc #878
1 parent fe0e2ae commit 0c1dcb0

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

src/metadata.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::convert::TryFrom;
12
use std::fs::File;
23
use std::path::Path;
34

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

0 commit comments

Comments
 (0)