Skip to content

Commit

Permalink
repo_path: avoid repeated copying of PathBuf in to_fs_path()
Browse files Browse the repository at this point in the history
I've noticed `WorkspaceCommandHelper::format_file_path()` appear in
profiles a few times. A big part of that is spent in
`RepoPath::to_fs_path()`. I think I had been thinking that
`PathBuf::join()` takes `self` by value and mutates it, but it turns
out it creates a new instance. So our `result = result.join(...)` in a
loop was copying the `PathBuf` over and over. This fixes that and also
reserves the expected size. That speeds up `jj files
--ignore-working-copy -r v6.0` in the Linux repo from 546.0 s to 509.3
s (6.7%).
  • Loading branch information
martinvonz committed Sep 9, 2023
1 parent 70f6e0a commit 6f1d3a4
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/src/repo_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,11 @@ impl RepoPath {
}

pub fn to_fs_path(&self, base: &Path) -> PathBuf {
let mut result = base.to_owned();
let repo_path_len: usize = self.components.iter().map(|x| x.as_str().len() + 1).sum();
let mut result = PathBuf::with_capacity(base.as_os_str().len() + repo_path_len);
result.push(base);
for dir in &self.components {
result = result.join(&dir.value);
result.push(&dir.value);
}
result
}
Expand Down

0 comments on commit 6f1d3a4

Please sign in to comment.