Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(cli): Optimize setting up node_modules on macOS #23980

Merged
merged 9 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ base64.workspace = true
bincode = "=1.3.3"
bytes.workspace = true
cache_control.workspace = true
cfg-if = "1.0.0"
chrono = { workspace = true, features = ["now"] }
clap = { version = "=4.4.17", features = ["env", "string"] }
clap_complete = "=4.4.7"
Expand Down
18 changes: 4 additions & 14 deletions cli/npm/managed/resolvers/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::cache::CACHE_PERM;
use crate::npm::cache_dir::mixed_case_package_name_decode;
use crate::util::fs::atomic_write_file;
use crate::util::fs::canonicalize_path_maybe_not_exists_with_fs;
use crate::util::fs::clone_dir_recursive;
use crate::util::fs::symlink_dir;
use crate::util::fs::LaxSingleProcessFsFlag;
use crate::util::progress_bar::ProgressBar;
Expand Down Expand Up @@ -44,8 +45,6 @@ use serde::Deserialize;
use serde::Serialize;

use crate::npm::cache_dir::mixed_case_package_name_encode;
use crate::util::fs::copy_dir_recursive;
use crate::util::fs::hard_link_dir_recursive;

use super::super::super::common::types_package_name;
use super::super::cache::NpmCache;
Expand Down Expand Up @@ -331,16 +330,9 @@ async fn sync_resolution_with_fs(
let sub_node_modules = folder_path.join("node_modules");
let package_path =
join_package_name(&sub_node_modules, &package.id.nv.name);
fs::create_dir_all(&package_path)
.with_context(|| format!("Creating '{}'", folder_path.display()))?;
let cache_folder =
cache.package_folder_for_name_and_version(&package.id.nv);
if hard_link_dir_recursive(&cache_folder, &package_path).is_err() {
// Fallback to copying the directory.
//
// Also handles EXDEV when when trying to hard link across volumes.
copy_dir_recursive(&cache_folder, &package_path)?;
}
clone_dir_recursive(&cache_folder, &package_path)?;
// write out a file that indicates this folder has been initialized
fs::write(initialized_file, "")?;

Expand Down Expand Up @@ -373,9 +365,7 @@ async fn sync_resolution_with_fs(
let sub_node_modules = destination_path.join("node_modules");
let package_path =
join_package_name(&sub_node_modules, &package.id.nv.name);
fs::create_dir_all(&package_path).with_context(|| {
format!("Creating '{}'", destination_path.display())
})?;

let source_path = join_package_name(
&deno_local_registry_dir
.join(get_package_folder_id_folder_name(
Expand All @@ -384,7 +374,7 @@ async fn sync_resolution_with_fs(
.join("node_modules"),
&package.id.nv.name,
);
hard_link_dir_recursive(&source_path, &package_path)?;
clone_dir_recursive(&source_path, &package_path)?;
// write out a file that indicates this folder has been initialized
fs::write(initialized_file, "")?;
}
Expand Down
62 changes: 62 additions & 0 deletions cli/util/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,68 @@ pub async fn remove_dir_all_if_exists(path: &Path) -> std::io::Result<()> {
}
}

mod clone_dir_imp {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a similar optimization for cp and copyFile APIs. It uses rayon for recursive clonefile and has some other optimizations for small files and fallbacks:

fn cp(from: &Path, to: &Path) -> FsResult<()> {

Maybe could be reused?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we should use rayon in non-js sync specific scenarios. I feel like that might make stuff slower by using another thread pool.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gave it a shot anyway, no performance difference but I found it caused a failure when the destination is a directory that already exists - didn't dig into it but seems like something wasn't getting copied over properly (the panic is from setting up the bin entries – the origin file we're trying to symlink doesn't exist)

thread 'main' panicked at cli/npm/managed/resolvers/local/bin_entries.rs:89:48:
called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }

use deno_core::error::AnyError;

use super::copy_dir_recursive;
use std::path::Path;

cfg_if::cfg_if! {
nathanwhit marked this conversation as resolved.
Show resolved Hide resolved
if #[cfg(target_vendor = "apple")] {
use std::os::unix::ffi::OsStrExt;
fn clonefile(from: &Path, to: &Path) -> std::io::Result<()> {
let from = std::ffi::CString::new(from.as_os_str().as_bytes())?;
let to = std::ffi::CString::new(to.as_os_str().as_bytes())?;
// SAFETY: `from` and `to` are valid C strings.
let ret = unsafe { libc::clonefile(from.as_ptr(), to.as_ptr(), 0) };
if ret != 0 {
return Err(std::io::Error::last_os_error());
}
Ok(())
}

pub(super) fn clone_dir_recursive(
from: &Path,
to: &Path,
) -> Result<(), AnyError> {
if let Some(parent) = to.parent() {
std::fs::create_dir_all(parent)?;
}
// Try to clone the whole directory
if let Err(err) = clonefile(from, to) {
if err.kind() != std::io::ErrorKind::AlreadyExists {
log::warn!("Failed to clone dir {:?} to {:?} via clonefile: {}", from, to, err);
}
// clonefile won't overwrite existing files, so if the dir exists
// we need to handle it recursively.
copy_dir_recursive(from, to)?;
}

Ok(())
}
} else {
use super::hard_link_dir_recursive;
pub(super) fn clone_dir_recursive(from: &Path, to: &Path) -> Result<(), AnyError> {
if let Err(e) = hard_link_dir_recursive(from, to) {
log::debug!("Failed to hard link dir {:?} to {:?}: {}", from, to, e);
copy_dir_recursive(from, to)?;
}

Ok(())
}
}
}
}

/// Clones a directory to another directory. The exact method
/// is not guaranteed - it may be a hardlink, copy, or other platform-specific
/// operation.
///
/// Note: Does not handle symlinks.
pub fn clone_dir_recursive(from: &Path, to: &Path) -> Result<(), AnyError> {
clone_dir_imp::clone_dir_recursive(from, to)
}

/// Copies a directory to another directory.
///
/// Note: Does not handle symlinks.
Expand Down