From c134f7587088989bde999cf5d6ae78796b33f342 Mon Sep 17 00:00:00 2001 From: Sebastian Ullrich Date: Sun, 19 Oct 2025 13:18:58 +0200 Subject: [PATCH] perf: try dropping written files async --- Cargo.lock | 26 +++++++++++++++++++ src/elan-dist/Cargo.toml | 1 + src/elan-dist/src/component/package.rs | 36 +++++++++++++++++++++++++- 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 0ed0148..c54e0ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -511,6 +511,7 @@ dependencies = [ "serde_derive", "sha2", "tar", + "threadpool", "time", "toml", "url", @@ -847,6 +848,12 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +[[package]] +name = "hermit-abi" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c" + [[package]] name = "hmac" version = "0.12.1" @@ -1299,6 +1306,16 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" +[[package]] +name = "num_cpus" +version = "1.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91df4bbde75afed763b708b7eee1e8e7651e02d97f6d5dd763e89367e957b23b" +dependencies = [ + "hermit-abi 0.5.2", + "libc", +] + [[package]] name = "object" version = "0.36.5" @@ -1954,6 +1971,15 @@ dependencies = [ "syn", ] +[[package]] +name = "threadpool" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d050e60b33d41c19108b32cea32164033a9013fe3b46cbd4457559bfbf77afaa" +dependencies = [ + "num_cpus", +] + [[package]] name = "time" version = "0.3.41" diff --git a/src/elan-dist/Cargo.toml b/src/elan-dist/Cargo.toml index 7d07413..1d48275 100644 --- a/src/elan-dist/Cargo.toml +++ b/src/elan-dist/Cargo.toml @@ -30,6 +30,7 @@ time = "0.3" serde = "1.0.119" serde_derive = "1.0.119" fslock = "0.2.1" +threadpool = "1.8.1" [target."cfg(windows)".dependencies] winapi = { version = "0.3.9", features = ["handleapi", "sysinfoapi", "tlhelp32", "winnt"] } diff --git a/src/elan-dist/src/component/package.rs b/src/elan-dist/src/component/package.rs index 89ff5ce..6a1aba0 100644 --- a/src/elan-dist/src/component/package.rs +++ b/src/elan-dist/src/component/package.rs @@ -7,10 +7,27 @@ use crate::errors::*; use std::fs::{self, File}; use std::io::{self, Read, Seek}; use std::path::{Path, PathBuf}; +#[cfg(windows)] +use std::sync::OnceLock; use time::OffsetDateTime; use zip::ZipArchive; +// Thread pool for handling expensive file handle drops on Windows +#[cfg(windows)] +static DROP_POOL: OnceLock = OnceLock::new(); + +#[cfg(windows)] +fn get_drop_pool() -> &'static threadpool::ThreadPool { + DROP_POOL.get_or_init(|| { + threadpool::Builder::new() + .thread_name("FileDrop".into()) + .num_threads(8) + .thread_stack_size(1_048_576) + .build() + }) +} + #[derive(Debug)] pub struct TarPackage(); @@ -48,9 +65,26 @@ fn unpack_without_first_dir(archive: &mut tar::Archive, path: &Path) _ => (), }; - entry + let unpacked = entry .unpack(&full_path) .chain_err(|| ErrorKind::ExtractingPackage)?; + + #[cfg(windows)] + { + let pool = get_drop_pool(); + pool.execute(move || { + drop(unpacked); + }); + } + #[cfg(not(windows))] + { + drop(unpacked); + } + } + + #[cfg(windows)] + { + get_drop_pool().join(); } Ok(())