From 6f1d3a4ec02bb4784c1bd6eddc05a18993240a85 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Fri, 8 Sep 2023 18:19:47 -0700 Subject: [PATCH] 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%). --- lib/src/repo_path.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/src/repo_path.rs b/lib/src/repo_path.rs index 48033a8283..c9fd4f619a 100644 --- a/lib/src/repo_path.rs +++ b/lib/src/repo_path.rs @@ -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 }