Skip to content

Commit 22dbb17

Browse files
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<R>::unpack::{{closure}} 19113 (1.1%, 2.4%) 69 (0.2%, 0.2%) alloc::raw_vec::RawVec<T,A>::grow_amortized 18352 (1.1%, 3.4%) 80 (0.2%, 0.4%) tokio_tar::entry::EntryFields<R>::unpack::{{closure}}::{{closure}} 16871 (1.0%, 4.4%) 613 (1.5%, 1.9%) core::result::Result<T,E>::map_err 14747 (0.8%, 5.2%) 594 (1.4%, 3.3%) <core::result::Result<T,E> as core::ops::try_trait::Try>::branch 12576 (0.7%, 6.0%) 16 (0.0%, 3.3%) <tokio_tar::archive::Entries<R> as futures_core::stream::Stream>::poll_next 12314 (0.7%, 6.7%) 226 (0.5%, 3.8%) <alloc::boxed::Box<T,A> 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<T,A>::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%) <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter 10508 (0.6%, 10.5%) 18 (0.0%, 5.0%) <core::iter::adapters::flatten::FlattenCompat<I,U> 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<T>::with_mut ``` After: ``` Lines Copies Function name ----- ------ ------------- 1496463 37891 (TOTAL) 14958 (1.0%, 1.0%) 54 (0.1%, 0.1%) alloc::raw_vec::RawVec<T,A>::grow_amortized 13997 (0.9%, 1.9%) 564 (1.5%, 1.6%) <core::result::Result<T,E> as core::ops::try_trait::Try>::branch 12776 (0.9%, 2.8%) 463 (1.2%, 2.9%) core::result::Result<T,E>::map_err 12381 (0.8%, 3.6%) 227 (0.6%, 3.5%) <alloc::boxed::Box<T,A> 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%) <alloc::vec::Vec<T> as alloc::vec::spec_from_iter_nested::SpecFromIterNested<T,I>>::from_iter 10508 (0.7%, 6.6%) 18 (0.0%, 4.5%) <core::iter::adapters::flatten::FlattenCompat<I,U> 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<T>::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<T,A>::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<T,S>::new 6438 (0.4%, 12.7%) 269 (0.7%, 9.4%) alloc::boxed::Box<T>::new 6360 (0.4%, 13.1%) 20 (0.1%, 9.4%) <toml_edit::de::value::ValueDeserializer as serde::de::Deserializer>::deserialize_any ```
1 parent 257007c commit 22dbb17

File tree

1 file changed

+37
-31
lines changed

1 file changed

+37
-31
lines changed

crates/uv-extract/src/stream.rs

+37-31
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ pub async fn unzip<R: tokio::io::AsyncRead + Unpin>(
104104
/// Unpack the given tar archive into the destination directory.
105105
///
106106
/// This is equivalent to `archive.unpack_in(dst)`, but it also preserves the executable bit.
107-
async fn untar_in<R: tokio::io::AsyncRead + Unpin, P: AsRef<Path>>(
108-
archive: &mut tokio_tar::Archive<R>,
109-
dst: P,
107+
async fn untar_in<'a>(
108+
mut archive: tokio_tar::Archive<&'a mut (dyn tokio::io::AsyncRead + Unpin)>,
109+
dst: &Path,
110110
) -> std::io::Result<()> {
111111
let mut entries = archive.entries()?;
112112
let mut pinned = Pin::new(&mut entries);
@@ -124,7 +124,7 @@ async fn untar_in<R: tokio::io::AsyncRead + Unpin, P: AsRef<Path>>(
124124
continue;
125125
}
126126

127-
file.unpack_in(dst.as_ref()).await?;
127+
file.unpack_in(dst).await?;
128128

129129
// Preserve the executable bit.
130130
#[cfg(unix)]
@@ -137,7 +137,7 @@ async fn untar_in<R: tokio::io::AsyncRead + Unpin, P: AsRef<Path>>(
137137
let mode = file.header().mode()?;
138138
let has_any_executable_bit = mode & 0o111;
139139
if has_any_executable_bit != 0 {
140-
if let Some(path) = crate::tar::unpacked_at(dst.as_ref(), &file.path()?) {
140+
if let Some(path) = crate::tar::unpacked_at(dst, &file.path()?) {
141141
let permissions = fs_err::tokio::metadata(&path).await?.permissions();
142142
if permissions.mode() & 0o111 != 0o111 {
143143
fs_err::tokio::set_permissions(
@@ -162,13 +162,14 @@ pub async fn untar_gz<R: tokio::io::AsyncRead + Unpin>(
162162
target: impl AsRef<Path>,
163163
) -> Result<(), Error> {
164164
let reader = tokio::io::BufReader::with_capacity(DEFAULT_BUF_SIZE, reader);
165-
let decompressed_bytes = async_compression::tokio::bufread::GzipDecoder::new(reader);
166-
167-
let mut archive = tokio_tar::ArchiveBuilder::new(decompressed_bytes)
168-
.set_preserve_mtime(false)
169-
.build();
170-
untar_in(&mut archive, target.as_ref()).await?;
171-
Ok(())
165+
let mut decompressed_bytes = async_compression::tokio::bufread::GzipDecoder::new(reader);
166+
167+
let archive = tokio_tar::ArchiveBuilder::new(
168+
&mut decompressed_bytes as &mut (dyn tokio::io::AsyncRead + Unpin),
169+
)
170+
.set_preserve_mtime(false)
171+
.build();
172+
Ok(untar_in(archive, target.as_ref()).await?)
172173
}
173174

174175
/// Unzip a `.tar.bz2` archive into the target directory, without requiring `Seek`.
@@ -179,13 +180,14 @@ pub async fn untar_bz2<R: tokio::io::AsyncRead + Unpin>(
179180
target: impl AsRef<Path>,
180181
) -> Result<(), Error> {
181182
let reader = tokio::io::BufReader::with_capacity(DEFAULT_BUF_SIZE, reader);
182-
let decompressed_bytes = async_compression::tokio::bufread::BzDecoder::new(reader);
183-
184-
let mut archive = tokio_tar::ArchiveBuilder::new(decompressed_bytes)
185-
.set_preserve_mtime(false)
186-
.build();
187-
untar_in(&mut archive, target.as_ref()).await?;
188-
Ok(())
183+
let mut decompressed_bytes = async_compression::tokio::bufread::BzDecoder::new(reader);
184+
185+
let archive = tokio_tar::ArchiveBuilder::new(
186+
&mut decompressed_bytes as &mut (dyn tokio::io::AsyncRead + Unpin),
187+
)
188+
.set_preserve_mtime(false)
189+
.build();
190+
Ok(untar_in(archive, target.as_ref()).await?)
189191
}
190192

191193
/// Unzip a `.tar.zst` archive into the target directory, without requiring `Seek`.
@@ -196,12 +198,14 @@ pub async fn untar_zst<R: tokio::io::AsyncRead + Unpin>(
196198
target: impl AsRef<Path>,
197199
) -> Result<(), Error> {
198200
let reader = tokio::io::BufReader::with_capacity(DEFAULT_BUF_SIZE, reader);
199-
let decompressed_bytes = async_compression::tokio::bufread::ZstdDecoder::new(reader);
200-
201-
let mut archive = tokio_tar::ArchiveBuilder::new(decompressed_bytes)
202-
.set_preserve_mtime(false)
203-
.build();
204-
Ok(untar_in(&mut archive, target.as_ref()).await?)
201+
let mut decompressed_bytes = async_compression::tokio::bufread::ZstdDecoder::new(reader);
202+
203+
let archive = tokio_tar::ArchiveBuilder::new(
204+
&mut decompressed_bytes as &mut (dyn tokio::io::AsyncRead + Unpin),
205+
)
206+
.set_preserve_mtime(false)
207+
.build();
208+
Ok(untar_in(archive, target.as_ref()).await?)
205209
}
206210

207211
/// Unzip a `.tar.xz` archive into the target directory, without requiring `Seek`.
@@ -212,12 +216,14 @@ pub async fn untar_xz<R: tokio::io::AsyncRead + Unpin>(
212216
target: impl AsRef<Path>,
213217
) -> Result<(), Error> {
214218
let reader = tokio::io::BufReader::with_capacity(DEFAULT_BUF_SIZE, reader);
215-
let decompressed_bytes = async_compression::tokio::bufread::XzDecoder::new(reader);
216-
217-
let mut archive = tokio_tar::ArchiveBuilder::new(decompressed_bytes)
218-
.set_preserve_mtime(false)
219-
.build();
220-
untar_in(&mut archive, target.as_ref()).await?;
219+
let mut decompressed_bytes = async_compression::tokio::bufread::XzDecoder::new(reader);
220+
221+
let archive = tokio_tar::ArchiveBuilder::new(
222+
&mut decompressed_bytes as &mut (dyn tokio::io::AsyncRead + Unpin),
223+
)
224+
.set_preserve_mtime(false)
225+
.build();
226+
untar_in(archive, target.as_ref()).await?;
221227
Ok(())
222228
}
223229

0 commit comments

Comments
 (0)