Skip to content

Performance: Use Arc<[PathBuf]> to reduce cloning overhead for shared path lists #287

@karthiknadig

Description

@karthiknadig

Summary

The codebase frequently clones Vec<PathBuf> when passing path lists to threads. Since these paths are typically immutable once created, wrapping them in Arc<[PathBuf]> would eliminate deep cloning overhead.

Affected Locations

crates/pet/src/find.rs

// Line 128: Clones entire vector for each iteration
global_env_search_paths.clone(),

// Line 142: Clone before thread scope
let environment_directories_search = environment_directories.clone();

// Lines 212-213: Clones inside thread spawn
let global_env_search_paths = global_env_search_paths.clone();
let environment_directories = environment_directories.clone();

crates/pet-conda/src/lib.rs

// Line 94: Clones entire HashMap to iterate
let environments = self.environments.lock().unwrap().clone();

Proposed Solution

Option 1: Use Arc<[PathBuf]>

// Instead of:
let global_env_search_paths: Vec<PathBuf> = get_search_paths_from_env_variables(environment);

// Use:
let global_env_search_paths: Arc<[PathBuf]> = 
    get_search_paths_from_env_variables(environment).into();

// Then cloning is cheap (just atomic increment):
let paths = global_env_search_paths.clone(); // O(1) instead of O(n)

Option 2: Pass references where possible

For thread::scope contexts, references can often be passed directly:

thread::scope(|s| {
    let paths = &global_env_search_paths; // borrow instead of clone
    s.spawn(move || {
        // use paths
    });
});

Impact

  • Reduces memory allocations during environment discovery
  • Particularly impactful when there are many workspace directories or PATH entries
  • Arc<[PathBuf]> has same memory layout as Vec<PathBuf> but with shared ownership

Priority

Medium - Reduces allocations but requires API changes to helper functions.

Metadata

Metadata

Labels

debtCode quality issues

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions