From 879f585b21e870363f50a161ecfe20a42bce85c9 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 20 Jan 2023 16:42:04 +0900 Subject: [PATCH] repo: leverage stored index to calculate shortest prefix in commit id space MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With my "jj" work repo, this saves ~4ms to show the log with default revset. Command: JJ_CONFIG=/dev/null hyperfine --warmup 3 --runs 100 \ "jj log -T 'commit_id.short_prefix_and_brackets() \ change_id.short_prefix_and_brackets()' \ --no-commit-working-copy" Baseline (a7541e1ba4): Time (mean ± σ): 54.1 ms ± 16.4 ms [User: 46.4 ms, System: 7.8 ms] Range (min … max): 36.5 ms … 78.1 ms 100 runs This commit: Time (mean ± σ): 49.5 ms ± 16.4 ms [User: 42.4 ms, System: 7.2 ms] Range (min … max): 31.4 ms … 70.9 ms 100 runs --- lib/src/repo.rs | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/lib/src/repo.rs b/lib/src/repo.rs index b63e6db7c..f15c27b32 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -102,7 +102,6 @@ pub struct ReadonlyRepo { index_store: Arc, index: OnceCell>, // TODO: This should eventually become part of the index and not be stored fully in memory. - commit_id_index: OnceCell, change_id_index: OnceCell, view: View, } @@ -193,7 +192,6 @@ impl ReadonlyRepo { settings: repo_settings, index_store, index: OnceCell::new(), - commit_id_index: OnceCell::new(), change_id_index: OnceCell::new(), view, })) @@ -245,17 +243,6 @@ impl ReadonlyRepo { }) } - fn commit_id_index(&self) -> &IdIndex { - self.commit_id_index.get_or_init(|| { - // We need to account for rewritten commits as well - let mut id_index = IdIndex::new(); - for entry in self.as_repo_ref().index().iter() { - id_index.insert(entry.commit_id().as_bytes(), ()); - } - id_index - }) - } - fn change_id_index(&self) -> &IdIndex { self.change_id_index.get_or_init(|| { let all_visible_revisions = crate::revset::RevsetExpression::all() @@ -278,8 +265,8 @@ impl ReadonlyRepo { // The root change/commit ids share the same prefix, and they are found in both // indices with different lengths. So we have to feed bytes of valid lengths. cmp::max( - self.commit_id_index() - .shortest_unique_prefix_len(root_commit_id.as_bytes()), + self.index() + .shortest_unique_commit_id_prefix_len(root_commit_id), self.change_id_index() .shortest_unique_prefix_len(root_change_id.as_bytes()), ) @@ -289,8 +276,8 @@ impl ReadonlyRepo { // `max(len, anything_else)`, so a max of such lengths will disambiguate in all // indices. cmp::max( - self.commit_id_index() - .shortest_unique_prefix_len(target_id_bytes), + self.index() + .shortest_unique_commit_id_prefix_len(&CommitId::from_bytes(target_id_bytes)), self.change_id_index() .shortest_unique_prefix_len(target_id_bytes), ) @@ -551,7 +538,6 @@ impl RepoLoader { settings: self.repo_settings.clone(), index_store: self.index_store.clone(), index: OnceCell::with_value(index), - commit_id_index: OnceCell::new(), change_id_index: OnceCell::new(), view, }; @@ -583,7 +569,6 @@ impl RepoLoader { settings: self.repo_settings.clone(), index_store: self.index_store.clone(), index: OnceCell::new(), - commit_id_index: OnceCell::new(), change_id_index: OnceCell::new(), view, };