From fb06e8964902e04ab646c7e2fef724a5a0356e16 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Mon, 18 Dec 2023 17:31:06 +0900 Subject: [PATCH] index: use u32::from_le_bytes() to reinterpret bytes as integer It's less abstract than going through io::Read, so is probably easier for compiler to optimize out. I also feel it's a bit more readable. --- lib/src/default_index/readonly.rs | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/lib/src/default_index/readonly.rs b/lib/src/default_index/readonly.rs index 5bd98960b..eafb4070a 100644 --- a/lib/src/default_index/readonly.rs +++ b/lib/src/default_index/readonly.rs @@ -87,19 +87,19 @@ impl CommitGraphEntry<'_> { } fn generation_number(&self) -> u32 { - (&self.data[4..]).read_u32::().unwrap() + u32::from_le_bytes(self.data[4..8].try_into().unwrap()) } fn num_parents(&self) -> u32 { - (&self.data[8..]).read_u32::().unwrap() + u32::from_le_bytes(self.data[8..12].try_into().unwrap()) } fn parent1_pos(&self) -> IndexPosition { - IndexPosition((&self.data[12..]).read_u32::().unwrap()) + IndexPosition(u32::from_le_bytes(self.data[12..16].try_into().unwrap())) } fn parent2_overflow_pos(&self) -> u32 { - (&self.data[16..]).read_u32::().unwrap() + u32::from_le_bytes(self.data[16..20].try_into().unwrap()) } // TODO: Consider storing the change ids in a separate table. That table could @@ -137,11 +137,8 @@ impl CommitLookupEntry<'_> { } fn pos(&self) -> IndexPosition { - IndexPosition( - (&self.data[self.commit_id_length..][..4]) - .read_u32::() - .unwrap(), - ) + let pos = u32::from_le_bytes(self.data[self.commit_id_length..][..4].try_into().unwrap()); + IndexPosition(pos) } } @@ -328,11 +325,8 @@ impl ReadonlyIndexSegment { 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.data[offset..][..4]) - .read_u32::() - .unwrap(), - ) + let pos = u32::from_le_bytes(self.data[offset..][..4].try_into().unwrap()); + IndexPosition(pos) } fn commit_id_byte_prefix_to_lookup_pos(&self, prefix: &CommitId) -> Option {