Merge pull request #303 from PJB3005/22-04-11-list-capacity
Use some ::with_capacity when reading zip file.
This commit is contained in:
commit
cf9e347031
3 changed files with 47 additions and 2 deletions
|
@ -42,3 +42,7 @@ default = ["aes-crypto", "bzip2", "deflate", "time", "zstd"]
|
||||||
[[bench]]
|
[[bench]]
|
||||||
name = "read_entry"
|
name = "read_entry"
|
||||||
harness = false
|
harness = false
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
name = "read_metadata"
|
||||||
|
harness = false
|
||||||
|
|
41
benches/read_metadata.rs
Normal file
41
benches/read_metadata.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
use bencher::{benchmark_group, benchmark_main};
|
||||||
|
|
||||||
|
use std::io::{Cursor, Write};
|
||||||
|
|
||||||
|
use bencher::Bencher;
|
||||||
|
use zip::{ZipArchive, ZipWriter};
|
||||||
|
|
||||||
|
const FILE_COUNT: usize = 15_000;
|
||||||
|
const FILE_SIZE: usize = 1024;
|
||||||
|
|
||||||
|
fn generate_random_archive(count_files: usize, file_size: usize) -> Vec<u8> {
|
||||||
|
let data = Vec::new();
|
||||||
|
let mut writer = ZipWriter::new(Cursor::new(data));
|
||||||
|
let options =
|
||||||
|
zip::write::FileOptions::default().compression_method(zip::CompressionMethod::Stored);
|
||||||
|
|
||||||
|
let bytes = vec![0u8; file_size];
|
||||||
|
|
||||||
|
for i in 0..count_files {
|
||||||
|
let name = format!(
|
||||||
|
"file_deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef_{}.dat",
|
||||||
|
i
|
||||||
|
);
|
||||||
|
writer.start_file(name, options).unwrap();
|
||||||
|
writer.write_all(&bytes).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.finish().unwrap().into_inner()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read_metadata(bench: &mut Bencher) {
|
||||||
|
let bytes = generate_random_archive(FILE_COUNT, FILE_SIZE);
|
||||||
|
|
||||||
|
bench.iter(|| {
|
||||||
|
let archive = ZipArchive::new(Cursor::new(bytes.as_slice())).unwrap();
|
||||||
|
archive.len()
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
benchmark_group!(benches, read_metadata);
|
||||||
|
benchmark_main!(benches);
|
|
@ -408,8 +408,8 @@ impl<R: Read + io::Seek> ZipArchive<R> {
|
||||||
let (archive_offset, directory_start, number_of_files) =
|
let (archive_offset, directory_start, number_of_files) =
|
||||||
Self::get_directory_counts(&mut reader, &footer, cde_start_pos)?;
|
Self::get_directory_counts(&mut reader, &footer, cde_start_pos)?;
|
||||||
|
|
||||||
let mut files = Vec::new();
|
let mut files = Vec::with_capacity(number_of_files);
|
||||||
let mut names_map = HashMap::new();
|
let mut names_map = HashMap::with_capacity(number_of_files);
|
||||||
|
|
||||||
if reader.seek(io::SeekFrom::Start(directory_start)).is_err() {
|
if reader.seek(io::SeekFrom::Start(directory_start)).is_err() {
|
||||||
return Err(ZipError::InvalidArchive(
|
return Err(ZipError::InvalidArchive(
|
||||||
|
|
Loading…
Add table
Reference in a new issue