From 2d42a4bc8ffe4b0e5d23195169bd53071e1d2cdf Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Wed, 21 Aug 2024 14:22:33 +0200 Subject: [PATCH] Use ZIP file size metadata to allocate string --- crates/ruff_db/src/vendored.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/ruff_db/src/vendored.rs b/crates/ruff_db/src/vendored.rs index 5cd462d55a8730..300409ebe326b8 100644 --- a/crates/ruff_db/src/vendored.rs +++ b/crates/ruff_db/src/vendored.rs @@ -97,7 +97,12 @@ impl VendoredFileSystem { fn read_to_string(fs: &VendoredFileSystem, path: &VendoredPath) -> Result { let mut archive = fs.lock_archive(); let mut zip_file = archive.lookup_path(&NormalizedVendoredPath::from(path))?; - let mut buffer = String::new(); + + let mut buffer = String::with_capacity( + usize::try_from(zip_file.size()) + .unwrap_or(usize::MAX) + .min(10_000_000), + ); zip_file.read_to_string(&mut buffer)?; Ok(buffer) }