From 6b5628bef4963e3528ea59a37f3cc1e5ced03345 Mon Sep 17 00:00:00 2001 From: Pieter-Jan Briers Date: Mon, 11 Apr 2022 14:30:27 +0200 Subject: [PATCH] Use Seek.stream_position instead of seek(0). BufReader can cache the result of stream_position() so it's fast, whereas seek(0) discards the read buffer and passes through straight to the OS. This drastically worsens the efficiency of loading performance when using BufReader (or anything else to avoid all these tiny reads going straight to the kernel). --- src/read.rs | 2 +- src/write.rs | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/read.rs b/src/read.rs index c619f24c..0d920032 100644 --- a/src/read.rs +++ b/src/read.rs @@ -639,7 +639,7 @@ pub(crate) fn central_header_to_zip_file( reader: &mut R, archive_offset: u64, ) -> ZipResult { - let central_header_start = reader.seek(io::SeekFrom::Current(0))?; + let central_header_start = reader.stream_position()?; // Parse central header let signature = reader.read_u32::()?; if signature != spec::CENTRAL_DIRECTORY_HEADER_SIGNATURE { diff --git a/src/write.rs b/src/write.rs index 551b4e32..376ea3bb 100644 --- a/src/write.rs +++ b/src/write.rs @@ -348,7 +348,7 @@ impl ZipWriter { { let writer = self.inner.get_plain(); - let header_start = writer.seek(io::SeekFrom::Current(0))?; + let header_start = writer.stream_position()?; let permissions = options.permissions.unwrap_or(0o100644); let mut file = ZipFileData { @@ -375,7 +375,7 @@ impl ZipWriter { }; write_local_file_header(writer, &file)?; - let header_end = writer.seek(io::SeekFrom::Current(0))?; + let header_end = writer.stream_position()?; self.stats.start = header_end; *file.data_start.get_mut() = header_end; @@ -404,7 +404,7 @@ impl ZipWriter { file.crc32 = self.stats.hasher.clone().finalize(); file.uncompressed_size = self.stats.bytes_written; - let file_end = writer.seek(io::SeekFrom::Current(0))?; + let file_end = writer.stream_position()?; file.compressed_size = file_end - self.stats.start; update_local_file_header(writer, file)?; @@ -753,11 +753,11 @@ impl ZipWriter { { let writer = self.inner.get_plain(); - let central_start = writer.seek(io::SeekFrom::Current(0))?; + let central_start = writer.stream_position()?; for file in self.files.iter() { write_central_directory_header(writer, file)?; } - let central_size = writer.seek(io::SeekFrom::Current(0))? - central_start; + let central_size = writer.stream_position()? - central_start; if self.files.len() > spec::ZIP64_ENTRY_THR || central_size.max(central_start) > spec::ZIP64_BYTES_THR