Skip to content

Commit e3a4c9d

Browse files
jdxclaude
andcommitted
perf: remove unnecessary clone in set_subcommand_ancestors
Replace `ancestors.to_vec()` followed by `ancestors.clone().into_iter()` with direct `ancestors.iter().cloned()`. This eliminates two Vec allocations per recursion level (one for the to_vec() and one for each subcommand's clone()). The improvement: - Before: O(n) Vec allocation for to_vec() + O(n) Vec allocation per subcommand - After: O(n) lazy iteration with element-by-element cloning (no intermediate Vec) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 9df7c86 commit e3a4c9d

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

lib/src/spec/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,14 @@ fn extract_usage_from_comments(full: &str) -> String {
309309
}
310310

311311
fn set_subcommand_ancestors(cmd: &mut SpecCommand, ancestors: &[String]) {
312-
let ancestors = ancestors.to_vec();
313312
for subcmd in cmd.subcommands.values_mut() {
314313
subcmd.full_cmd = ancestors
315-
.clone()
316-
.into_iter()
314+
.iter()
315+
.cloned()
317316
.chain(once(subcmd.name.clone()))
318317
.collect();
319-
set_subcommand_ancestors(subcmd, &subcmd.full_cmd.clone());
318+
let child_ancestors = subcmd.full_cmd.clone();
319+
set_subcommand_ancestors(subcmd, &child_ancestors);
320320
}
321321
if cmd.usage.is_empty() {
322322
cmd.usage = cmd.usage();

0 commit comments

Comments
 (0)