Avoid unzipping local wheels when fresh (#1076)

Since the archive is a single file in this case, we can rely on the
modification timestamp to check for freshness.
This commit is contained in:
Charlie Marsh 2024-01-24 10:01:16 -05:00 committed by GitHub
parent 411613a24e
commit afb571643f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 45 additions and 6 deletions

View file

@ -17,6 +17,7 @@ use platform_tags::Tags;
use puffin_cache::{Cache, CacheBucket, WheelCache};
use puffin_client::{CacheControl, CachedClientError, RegistryClient};
use puffin_extract::unzip_no_seek;
use puffin_fs::metadata_if_exists;
use puffin_git::GitSource;
use puffin_traits::{BuildContext, NoBinary};
use pypi_types::Metadata21;
@ -131,7 +132,22 @@ impl<'a, Context: BuildContext + Send + Sync> DistributionDatabase<'a, Context>
wheel.filename.stem(),
);
// TODO(charlie): There's no need to unzip if the wheel is unchanged.
// If the file is already unzipped, and the unzipped directory is fresh,
// return it.
if let (Some(cache_metadata), Some(path_metadata)) = (
metadata_if_exists(cache_entry.path())?,
metadata_if_exists(path)?,
) {
if cache_metadata.modified()? > path_metadata.modified()? {
return Ok(LocalWheel::Unzipped(UnzippedWheel {
dist: dist.clone(),
target: cache_entry.into_path_buf(),
filename: wheel.filename.clone(),
}));
}
}
// Otherwise, unzip the file.
return Ok(LocalWheel::Disk(DiskWheel {
dist: dist.clone(),
path: path.clone(),
@ -256,7 +272,21 @@ impl<'a, Context: BuildContext + Send + Sync> DistributionDatabase<'a, Context>
wheel.filename.stem(),
);
// TODO(charlie): There's no need to unzip if the wheel is unchanged.
// If the file is already unzipped, and the unzipped directory is fresh,
// return it.
if let (Some(cache_metadata), Some(path_metadata)) = (
metadata_if_exists(cache_entry.path())?,
metadata_if_exists(&wheel.path)?,
) {
if cache_metadata.modified()? > path_metadata.modified()? {
return Ok(LocalWheel::Unzipped(UnzippedWheel {
dist: dist.clone(),
target: cache_entry.into_path_buf(),
filename: wheel.filename.clone(),
}));
}
}
Ok(LocalWheel::Disk(DiskWheel {
dist: dist.clone(),
path: wheel.path.clone(),

View file

@ -68,10 +68,8 @@ pub fn write_atomic_sync(path: impl AsRef<Path>, data: impl AsRef<[u8]>) -> std:
pub fn force_remove_all(path: impl AsRef<Path>) -> Result<bool, std::io::Error> {
let path = path.as_ref();
let metadata = match fs::metadata(path) {
Ok(metadata) => metadata,
Err(err) if err.kind() == std::io::ErrorKind::NotFound => return Ok(false),
Err(err) => return Err(err),
let Some(metadata) = metadata_if_exists(path)? else {
return Ok(false);
};
if metadata.is_dir() {
@ -189,3 +187,14 @@ impl Drop for LockedFile {
}
}
}
/// Given a path, return its metadata if the file exists, or `None` if it does not.
///
/// If the file exists but cannot be read, returns an error.
pub fn metadata_if_exists(path: impl AsRef<Path>) -> std::io::Result<Option<std::fs::Metadata>> {
match fs::metadata(path) {
Ok(metadata) => Ok(Some(metadata)),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(None),
Err(err) => Err(err),
}
}