From 22dbb1741b9c31df098df969ccef18fbea5494c6 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Sat, 3 Aug 2024 19:29:17 -0400 Subject: [PATCH] Avoid monomorphization of `untar` (#5743) ## Summary This reduces the LLVM lines of the entire project by about 15%. Before: ``` Lines Copies Function name ----- ------ ------------- 1742332 42054 (TOTAL) 22384 (1.3%, 1.3%) 16 (0.0%, 0.0%) tokio_tar::entry::EntryFields::unpack::{{closure}} 19113 (1.1%, 2.4%) 69 (0.2%, 0.2%) alloc::raw_vec::RawVec::grow_amortized 18352 (1.1%, 3.4%) 80 (0.2%, 0.4%) tokio_tar::entry::EntryFields::unpack::{{closure}}::{{closure}} 16871 (1.0%, 4.4%) 613 (1.5%, 1.9%) core::result::Result::map_err 14747 (0.8%, 5.2%) 594 (1.4%, 3.3%) as core::ops::try_trait::Try>::branch 12576 (0.7%, 6.0%) 16 (0.0%, 3.3%) as futures_core::stream::Stream>::poll_next 12314 (0.7%, 6.7%) 226 (0.5%, 3.8%) as core::ops::drop::Drop>::drop 11895 (0.7%, 7.4%) 296 (0.7%, 4.5%) std::panicking::try 11718 (0.7%, 8.0%) 63 (0.1%, 4.7%) alloc::raw_vec::RawVec::try_allocate_in 11152 (0.6%, 8.7%) 16 (0.0%, 4.7%) uv_extract::stream::untar_in::{{closure}} 10977 (0.6%, 9.3%) 1 (0.0%, 4.7%) uv::run::{{closure}}::{{closure}} 10859 (0.6%, 9.9%) 77 (0.2%, 4.9%) as alloc::vec::spec_from_iter_nested::SpecFromIterNested>::from_iter 10508 (0.6%, 10.5%) 18 (0.0%, 5.0%) as core::iter::traits::iterator::Iterator>::size_hint 10260 (0.6%, 11.1%) 138 (0.3%, 5.3%) core::iter::traits::iterator::Iterator::try_fold 10196 (0.6%, 11.7%) 8 (0.0%, 5.3%) uv_extract::stream::unzip::{{closure}} 10178 (0.6%, 12.3%) 7 (0.0%, 5.3%) uv_client::cached_client::CachedClient::get_cacheable::{{closure}}::{{closure}} 9698 (0.6%, 12.8%) 293 (0.7%, 6.0%) tokio::loom::std::unsafe_cell::UnsafeCell::with_mut ``` After: ``` Lines Copies Function name ----- ------ ------------- 1496463 37891 (TOTAL) 14958 (1.0%, 1.0%) 54 (0.1%, 0.1%) alloc::raw_vec::RawVec::grow_amortized 13997 (0.9%, 1.9%) 564 (1.5%, 1.6%) as core::ops::try_trait::Try>::branch 12776 (0.9%, 2.8%) 463 (1.2%, 2.9%) core::result::Result::map_err 12381 (0.8%, 3.6%) 227 (0.6%, 3.5%) as core::ops::drop::Drop>::drop 11895 (0.8%, 4.4%) 296 (0.8%, 4.2%) std::panicking::try 10977 (0.7%, 5.1%) 1 (0.0%, 4.2%) uv::run::{{closure}}::{{closure}} 10859 (0.7%, 5.9%) 77 (0.2%, 4.4%) as alloc::vec::spec_from_iter_nested::SpecFromIterNested>::from_iter 10508 (0.7%, 6.6%) 18 (0.0%, 4.5%) as core::iter::traits::iterator::Iterator>::size_hint 10196 (0.7%, 7.3%) 8 (0.0%, 4.5%) uv_extract::stream::unzip::{{closure}} 10178 (0.7%, 7.9%) 7 (0.0%, 4.5%) uv_client::cached_client::CachedClient::get_cacheable::{{closure}}::{{closure}} 9698 (0.6%, 8.6%) 293 (0.8%, 5.3%) tokio::loom::std::unsafe_cell::UnsafeCell::with_mut 9078 (0.6%, 9.2%) 9 (0.0%, 5.3%) core::slice::sort::partition_in_blocks 8928 (0.6%, 9.8%) 48 (0.1%, 5.4%) alloc::raw_vec::RawVec::try_allocate_in 8288 (0.6%, 10.3%) 296 (0.8%, 6.2%) std::panicking::try::do_catch 8190 (0.5%, 10.9%) 108 (0.3%, 6.5%) core::iter::traits::iterator::Iterator::try_fold 7540 (0.5%, 11.4%) 466 (1.2%, 7.7%) core::ops::function::FnOnce::call_once 6612 (0.4%, 11.8%) 296 (0.8%, 8.5%) std::panicking::try::do_call 6513 (0.4%, 12.3%) 56 (0.1%, 8.7%) tokio::runtime::task::core::Cell::new 6438 (0.4%, 12.7%) 269 (0.7%, 9.4%) alloc::boxed::Box::new 6360 (0.4%, 13.1%) 20 (0.1%, 9.4%) ::deserialize_any ``` --- crates/uv-extract/src/stream.rs | 60 ++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/crates/uv-extract/src/stream.rs b/crates/uv-extract/src/stream.rs index 3e3625c18..ca4eefd35 100644 --- a/crates/uv-extract/src/stream.rs +++ b/crates/uv-extract/src/stream.rs @@ -104,9 +104,9 @@ pub async fn unzip( /// Unpack the given tar archive into the destination directory. /// /// This is equivalent to `archive.unpack_in(dst)`, but it also preserves the executable bit. -async fn untar_in>( - archive: &mut tokio_tar::Archive, - dst: P, +async fn untar_in<'a>( + mut archive: tokio_tar::Archive<&'a mut (dyn tokio::io::AsyncRead + Unpin)>, + dst: &Path, ) -> std::io::Result<()> { let mut entries = archive.entries()?; let mut pinned = Pin::new(&mut entries); @@ -124,7 +124,7 @@ async fn untar_in>( continue; } - file.unpack_in(dst.as_ref()).await?; + file.unpack_in(dst).await?; // Preserve the executable bit. #[cfg(unix)] @@ -137,7 +137,7 @@ async fn untar_in>( let mode = file.header().mode()?; let has_any_executable_bit = mode & 0o111; if has_any_executable_bit != 0 { - if let Some(path) = crate::tar::unpacked_at(dst.as_ref(), &file.path()?) { + if let Some(path) = crate::tar::unpacked_at(dst, &file.path()?) { let permissions = fs_err::tokio::metadata(&path).await?.permissions(); if permissions.mode() & 0o111 != 0o111 { fs_err::tokio::set_permissions( @@ -162,13 +162,14 @@ pub async fn untar_gz( target: impl AsRef, ) -> Result<(), Error> { let reader = tokio::io::BufReader::with_capacity(DEFAULT_BUF_SIZE, reader); - let decompressed_bytes = async_compression::tokio::bufread::GzipDecoder::new(reader); + let mut decompressed_bytes = async_compression::tokio::bufread::GzipDecoder::new(reader); - let mut archive = tokio_tar::ArchiveBuilder::new(decompressed_bytes) - .set_preserve_mtime(false) - .build(); - untar_in(&mut archive, target.as_ref()).await?; - Ok(()) + let archive = tokio_tar::ArchiveBuilder::new( + &mut decompressed_bytes as &mut (dyn tokio::io::AsyncRead + Unpin), + ) + .set_preserve_mtime(false) + .build(); + Ok(untar_in(archive, target.as_ref()).await?) } /// Unzip a `.tar.bz2` archive into the target directory, without requiring `Seek`. @@ -179,13 +180,14 @@ pub async fn untar_bz2( target: impl AsRef, ) -> Result<(), Error> { let reader = tokio::io::BufReader::with_capacity(DEFAULT_BUF_SIZE, reader); - let decompressed_bytes = async_compression::tokio::bufread::BzDecoder::new(reader); + let mut decompressed_bytes = async_compression::tokio::bufread::BzDecoder::new(reader); - let mut archive = tokio_tar::ArchiveBuilder::new(decompressed_bytes) - .set_preserve_mtime(false) - .build(); - untar_in(&mut archive, target.as_ref()).await?; - Ok(()) + let archive = tokio_tar::ArchiveBuilder::new( + &mut decompressed_bytes as &mut (dyn tokio::io::AsyncRead + Unpin), + ) + .set_preserve_mtime(false) + .build(); + Ok(untar_in(archive, target.as_ref()).await?) } /// Unzip a `.tar.zst` archive into the target directory, without requiring `Seek`. @@ -196,12 +198,14 @@ pub async fn untar_zst( target: impl AsRef, ) -> Result<(), Error> { let reader = tokio::io::BufReader::with_capacity(DEFAULT_BUF_SIZE, reader); - let decompressed_bytes = async_compression::tokio::bufread::ZstdDecoder::new(reader); + let mut decompressed_bytes = async_compression::tokio::bufread::ZstdDecoder::new(reader); - let mut archive = tokio_tar::ArchiveBuilder::new(decompressed_bytes) - .set_preserve_mtime(false) - .build(); - Ok(untar_in(&mut archive, target.as_ref()).await?) + let archive = tokio_tar::ArchiveBuilder::new( + &mut decompressed_bytes as &mut (dyn tokio::io::AsyncRead + Unpin), + ) + .set_preserve_mtime(false) + .build(); + Ok(untar_in(archive, target.as_ref()).await?) } /// Unzip a `.tar.xz` archive into the target directory, without requiring `Seek`. @@ -212,12 +216,14 @@ pub async fn untar_xz( target: impl AsRef, ) -> Result<(), Error> { let reader = tokio::io::BufReader::with_capacity(DEFAULT_BUF_SIZE, reader); - let decompressed_bytes = async_compression::tokio::bufread::XzDecoder::new(reader); + let mut decompressed_bytes = async_compression::tokio::bufread::XzDecoder::new(reader); - let mut archive = tokio_tar::ArchiveBuilder::new(decompressed_bytes) - .set_preserve_mtime(false) - .build(); - untar_in(&mut archive, target.as_ref()).await?; + let archive = tokio_tar::ArchiveBuilder::new( + &mut decompressed_bytes as &mut (dyn tokio::io::AsyncRead + Unpin), + ) + .set_preserve_mtime(false) + .build(); + untar_in(archive, target.as_ref()).await?; Ok(()) }