From 95a0cceb9706eade9ff87b00b6f5fd5a9563571f Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Wed, 13 Dec 2023 19:20:44 +0900 Subject: [PATCH] index: use loaded readonly data without splitting into vecs Since lookup data isn't typically small, .split_off() can take a few milliseconds to memcpy(). --- lib/src/default_index/readonly.rs | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/src/default_index/readonly.rs b/lib/src/default_index/readonly.rs index 1c01fbfb3..10b822627 100644 --- a/lib/src/default_index/readonly.rs +++ b/lib/src/default_index/readonly.rs @@ -132,9 +132,7 @@ pub(super) struct ReadonlyIndexSegment { commit_lookup_entry_size: usize, // Number of commits not counting the parent file num_local_commits: u32, - graph: Vec, - lookup: Vec, - overflow_parent: Vec, + data: Vec, } impl Debug for ReadonlyIndexSegment { @@ -189,9 +187,6 @@ impl ReadonlyIndexSegment { if data.len() != expected_size { return Err(IndexLoadError::IndexCorrupt(name)); } - let overflow_parent = data.split_off(graph_size + lookup_size); - let lookup = data.split_off(graph_size); - let graph = data; Ok(Arc::new(ReadonlyIndexSegment { parent_file: maybe_parent_file, num_parent_commits, @@ -201,9 +196,7 @@ impl ReadonlyIndexSegment { commit_graph_entry_size, commit_lookup_entry_size, num_local_commits: num_commits, - graph, - lookup, - overflow_parent, + data, })) } @@ -224,26 +217,31 @@ impl ReadonlyIndexSegment { } fn graph_entry(&self, local_pos: u32) -> CommitGraphEntry { + assert!(local_pos < self.num_local_commits); let offset = (local_pos as usize) * self.commit_graph_entry_size; CommitGraphEntry { - data: &self.graph[offset..][..self.commit_graph_entry_size], + data: &self.data[offset..][..self.commit_graph_entry_size], commit_id_length: self.commit_id_length, change_id_length: self.change_id_length, } } fn lookup_entry(&self, lookup_pos: u32) -> CommitLookupEntry { - let offset = (lookup_pos as usize) * self.commit_lookup_entry_size; + assert!(lookup_pos < self.num_local_commits); + let offset = (lookup_pos as usize) * self.commit_lookup_entry_size + + (self.num_local_commits as usize) * self.commit_graph_entry_size; CommitLookupEntry { - data: &self.lookup[offset..][..self.commit_lookup_entry_size], + data: &self.data[offset..][..self.commit_lookup_entry_size], commit_id_length: self.commit_id_length, } } fn overflow_parent(&self, overflow_pos: u32) -> IndexPosition { - let offset = (overflow_pos as usize) * 4; + let offset = (overflow_pos as usize) * 4 + + (self.num_local_commits as usize) * self.commit_graph_entry_size + + (self.num_local_commits as usize) * self.commit_lookup_entry_size; IndexPosition( - (&self.overflow_parent[offset..][..4]) + (&self.data[offset..][..4]) .read_u32::() .unwrap(), )