Remove some filesystem calls from the installer (#834)

Noticed these when working on something unrelated. Generally:

- Prefer `entry.file_type()` over `entry.path().is_file()` or similar,
as the former is almost always free on Unix.
- Call `entry.path()` once, since it allocates internally (returns a
`PathBuf`).
This commit is contained in:
Charlie Marsh 2024-01-08 12:59:01 -05:00 committed by GitHub
parent cd6265a66d
commit c06bf658bb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 31 deletions

View file

@ -136,8 +136,9 @@ fn find_dist_info(path: impl AsRef<Path>) -> Result<String, Error> {
// Iterate over `path` to find the `.dist-info` directory. It should be at the top-level.
let Some(dist_info) = fs::read_dir(path.as_ref())?.find_map(|entry| {
let entry = entry.ok()?;
let path = entry.path();
if path.is_dir() {
let file_type = entry.file_type().ok()?;
if file_type.is_dir() {
let path = entry.path();
if path.extension().map_or(false, |ext| ext == "dist-info") {
Some(path)
} else {
@ -317,7 +318,9 @@ fn copy_wheel_files(
// Walk over the directory.
for entry in walkdir::WalkDir::new(&wheel) {
let entry = entry?;
let relative = entry.path().strip_prefix(&wheel).unwrap();
let path = entry.path();
let relative = path.strip_prefix(&wheel).unwrap();
let out_path = site_packages.as_ref().join(relative);
if entry.file_type().is_dir() {
@ -326,7 +329,7 @@ fn copy_wheel_files(
}
// Copy the file.
fs::copy(entry.path(), &out_path)?;
fs::copy(path, &out_path)?;
#[cfg(unix)]
{
@ -370,7 +373,9 @@ fn hardlink_wheel_files(
// Walk over the directory.
for entry in walkdir::WalkDir::new(&wheel) {
let entry = entry?;
let relative = entry.path().strip_prefix(&wheel).unwrap();
let path = entry.path();
let relative = path.strip_prefix(&wheel).unwrap();
let out_path = site_packages.as_ref().join(relative);
if entry.file_type().is_dir() {
@ -379,8 +384,8 @@ fn hardlink_wheel_files(
}
// The `RECORD` file is modified during installation, so we copy it instead of hard-linking.
if entry.path().ends_with("RECORD") {
fs::copy(entry.path(), &out_path)?;
if path.ends_with("RECORD") {
fs::copy(path, &out_path)?;
count += 1;
continue;
}
@ -390,33 +395,33 @@ fn hardlink_wheel_files(
Attempt::Initial => {
// Once https://github.com/rust-lang/rust/issues/86442 is stable, use that.
attempt = Attempt::Subsequent;
if let Err(err) = fs::hard_link(entry.path(), &out_path) {
if let Err(err) = fs::hard_link(path, &out_path) {
// If the file already exists, remove it and try again.
if err.kind() == std::io::ErrorKind::AlreadyExists {
fs::remove_file(&out_path)?;
if fs::hard_link(entry.path(), &out_path).is_err() {
fs::copy(entry.path(), &out_path)?;
if fs::hard_link(path, &out_path).is_err() {
fs::copy(path, &out_path)?;
attempt = Attempt::UseCopyFallback;
}
} else {
fs::copy(entry.path(), &out_path)?;
fs::copy(path, &out_path)?;
attempt = Attempt::UseCopyFallback;
}
}
}
Attempt::Subsequent => {
if let Err(err) = fs::hard_link(entry.path(), &out_path) {
if let Err(err) = fs::hard_link(path, &out_path) {
// If the file already exists, remove it and try again.
if err.kind() == std::io::ErrorKind::AlreadyExists {
fs::remove_file(&out_path)?;
fs::hard_link(entry.path(), &out_path)?;
fs::hard_link(path, &out_path)?;
} else {
return Err(err.into());
}
}
}
Attempt::UseCopyFallback => {
fs::copy(entry.path(), &out_path)?;
fs::copy(path, &out_path)?;
}
}