repo_path: avoid repeated copying of PathBuf in to_fs_path()

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%).
This commit is contained in:
Martin von Zweigbergk 2023-09-08 18:19:47 -07:00 committed by Martin von Zweigbergk
parent 2e8b824da7
commit 86206e83a4

View file

@ -138,10 +138,10 @@ impl RepoPath {
}
pub fn to_fs_path(&self, base: &Path) -> PathBuf {
let mut result = base.to_owned();
for dir in &self.components {
result = result.join(&dir.value);
}
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);
result.extend(self.components.iter().map(|dir| &dir.value));
result
}