Skip to content

Commit

Permalink
Avoid monomorphization of untar (#5743)
Browse files Browse the repository at this point in the history
## 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
```
  • Loading branch information
charliermarsh authored Aug 3, 2024
1 parent 257007c commit 22dbb17
Showing 1 changed file with 37 additions and 31 deletions.
68 changes: 37 additions & 31 deletions crates/uv-extract/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ pub async fn unzip<R: tokio::io::AsyncRead + Unpin>(
/// 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<R: tokio::io::AsyncRead + Unpin, P: AsRef<Path>>(
archive: &mut tokio_tar::Archive<R>,
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);
Expand All @@ -124,7 +124,7 @@ async fn untar_in<R: tokio::io::AsyncRead + Unpin, P: AsRef<Path>>(
continue;
}

file.unpack_in(dst.as_ref()).await?;
file.unpack_in(dst).await?;

// Preserve the executable bit.
#[cfg(unix)]
Expand All @@ -137,7 +137,7 @@ async fn untar_in<R: tokio::io::AsyncRead + Unpin, P: AsRef<Path>>(
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(
Expand All @@ -162,13 +162,14 @@ pub async fn untar_gz<R: tokio::io::AsyncRead + Unpin>(
target: impl AsRef<Path>,
) -> 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 archive = tokio_tar::ArchiveBuilder::new(decompressed_bytes)
.set_preserve_mtime(false)
.build();
untar_in(&mut archive, target.as_ref()).await?;
Ok(())
let mut decompressed_bytes = async_compression::tokio::bufread::GzipDecoder::new(reader);

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`.
Expand All @@ -179,13 +180,14 @@ pub async fn untar_bz2<R: tokio::io::AsyncRead + Unpin>(
target: impl AsRef<Path>,
) -> 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 archive = tokio_tar::ArchiveBuilder::new(decompressed_bytes)
.set_preserve_mtime(false)
.build();
untar_in(&mut archive, target.as_ref()).await?;
Ok(())
let mut decompressed_bytes = async_compression::tokio::bufread::BzDecoder::new(reader);

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`.
Expand All @@ -196,12 +198,14 @@ pub async fn untar_zst<R: tokio::io::AsyncRead + Unpin>(
target: impl AsRef<Path>,
) -> 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 archive = tokio_tar::ArchiveBuilder::new(decompressed_bytes)
.set_preserve_mtime(false)
.build();
Ok(untar_in(&mut archive, target.as_ref()).await?)
let mut decompressed_bytes = async_compression::tokio::bufread::ZstdDecoder::new(reader);

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`.
Expand All @@ -212,12 +216,14 @@ pub async fn untar_xz<R: tokio::io::AsyncRead + Unpin>(
target: impl AsRef<Path>,
) -> 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 archive = tokio_tar::ArchiveBuilder::new(decompressed_bytes)
.set_preserve_mtime(false)
.build();
untar_in(&mut archive, target.as_ref()).await?;
let mut decompressed_bytes = async_compression::tokio::bufread::XzDecoder::new(reader);

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(())
}

Expand Down

0 comments on commit 22dbb17

Please sign in to comment.