Commit 72a6b2e
Use Arc<[PathBuf]> to reduce cloning overhead for shared path lists (#299)
Path lists are frequently cloned when passed to threads during
environment discovery. Since these paths are immutable once created,
`Arc<[PathBuf]>` provides O(1) cloning via atomic increment instead of
O(n) deep copies.
### Changes
- **Convert shared path lists to `Arc<[PathBuf]>`**
- `environment_directories` converted before thread spawns
- `global_env_search_paths` converted in workspace directory loop
- **Update function signatures to accept `&[PathBuf]`**
- `find_python_environments`
- `find_python_environments_in_paths_with_locators`
- **Remove unnecessary clones**
- Pass references where ownership isn't needed
- `find_executables` accepts `AsRef<Path>`, no clone required
### Before/After
```rust
// Before: O(n) clone per workspace folder
for workspace_folder in workspace_directories {
let global_env_search_paths = global_env_search_paths.clone(); // deep copy
// ...
}
// After: O(1) clone per workspace folder
let global_env_search_paths: Arc<[PathBuf]> =
get_search_paths_from_env_variables(environment).into();
for workspace_folder in workspace_directories {
let global_env_search_paths = global_env_search_paths.clone(); // atomic increment
// ...
}
```
<!-- START COPILOT ORIGINAL PROMPT -->
<details>
<summary>Original prompt</summary>
>
> ----
>
> *This section details on the original issue you should resolve*
>
> <issue_title>Performance: Use Arc<[PathBuf]> to reduce cloning
overhead for shared path lists</issue_title>
> <issue_description>## 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`
> ```rust
> // 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`
> ```rust
> // Line 94: Clones entire HashMap to iterate
> let environments = self.environments.lock().unwrap().clone();
> ```
>
> ## Proposed Solution
>
> ### Option 1: Use `Arc<[PathBuf]>`
> ```rust
> // 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:
> ```rust
> 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.</issue_description>
>
> <agent_instructions>Fix the issue. See if the recommendation makes
sense and create a fix.</agent_instructions>
>
> ## Comments on the Issue (you are @copilot in this section)
>
> <comments>
> </comments>
>
</details>
<!-- START COPILOT CODING AGENT SUFFIX -->
- Fixes #287
<!-- START COPILOT CODING AGENT TIPS -->
---
💬 We'd love your input! Share your thoughts on Copilot coding agent in
our [2 minute survey](https://gh.io/copilot-coding-agent-survey).
---------
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: karthiknadig <[email protected]>1 parent 0702485 commit 72a6b2e
1 file changed
+22
-16
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
125 | 125 | | |
126 | 126 | | |
127 | 127 | | |
128 | | - | |
| 128 | + | |
129 | 129 | | |
130 | 130 | | |
131 | 131 | | |
| |||
139 | 139 | | |
140 | 140 | | |
141 | 141 | | |
142 | | - | |
143 | | - | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
144 | 147 | | |
145 | 148 | | |
146 | 149 | | |
147 | 150 | | |
148 | 151 | | |
149 | | - | |
| 152 | + | |
150 | 153 | | |
151 | 154 | | |
152 | 155 | | |
| |||
177 | 180 | | |
178 | 181 | | |
179 | 182 | | |
180 | | - | |
| 183 | + | |
181 | 184 | | |
182 | 185 | | |
183 | 186 | | |
184 | 187 | | |
185 | 188 | | |
186 | 189 | | |
187 | | - | |
| 190 | + | |
188 | 191 | | |
189 | 192 | | |
190 | 193 | | |
| |||
197 | 200 | | |
198 | 201 | | |
199 | 202 | | |
200 | | - | |
| 203 | + | |
| 204 | + | |
201 | 205 | | |
202 | 206 | | |
203 | 207 | | |
| |||
206 | 210 | | |
207 | 211 | | |
208 | 212 | | |
209 | | - | |
210 | | - | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
211 | 216 | | |
212 | 217 | | |
213 | 218 | | |
| |||
236 | 241 | | |
237 | 242 | | |
238 | 243 | | |
239 | | - | |
| 244 | + | |
240 | 245 | | |
241 | 246 | | |
242 | 247 | | |
| |||
277 | 282 | | |
278 | 283 | | |
279 | 284 | | |
280 | | - | |
| 285 | + | |
281 | 286 | | |
282 | 287 | | |
283 | 288 | | |
| |||
309 | 314 | | |
310 | 315 | | |
311 | 316 | | |
312 | | - | |
| 317 | + | |
313 | 318 | | |
314 | 319 | | |
315 | 320 | | |
316 | 321 | | |
317 | 322 | | |
318 | | - | |
| 323 | + | |
319 | 324 | | |
320 | 325 | | |
321 | 326 | | |
| |||
327 | 332 | | |
328 | 333 | | |
329 | 334 | | |
| 335 | + | |
330 | 336 | | |
331 | 337 | | |
332 | | - | |
| 338 | + | |
333 | 339 | | |
334 | 340 | | |
335 | 341 | | |
| |||
341 | 347 | | |
342 | 348 | | |
343 | 349 | | |
344 | | - | |
| 350 | + | |
345 | 351 | | |
346 | 352 | | |
347 | 353 | | |
| |||
356 | 362 | | |
357 | 363 | | |
358 | 364 | | |
359 | | - | |
| 365 | + | |
360 | 366 | | |
361 | 367 | | |
362 | 368 | | |
| |||
0 commit comments