From 763a8cc0f1a3ee1304a78e4d864db5b636bf9898 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Fri, 20 Jan 2023 14:13:19 +0900 Subject: [PATCH] index: remove allocation from bisection loop of commit_id prefix search This wouldn't actually matter since the depth of binary search is O(log(N)), but I feel it's better to avoid allocation in comparison loop. --- lib/src/index.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/src/index.rs b/lib/src/index.rs index d59b58f7f..ce4119228 100644 --- a/lib/src/index.rs +++ b/lib/src/index.rs @@ -203,7 +203,12 @@ impl CommitLookupEntry<'_> { } fn commit_id(&self) -> CommitId { - CommitId::from_bytes(&self.data[0..self.hash_length]) + CommitId::from_bytes(self.commit_id_bytes()) + } + + // might be better to add borrowed version of CommitId + fn commit_id_bytes(&self) -> &[u8] { + &self.data[0..self.hash_length] } fn pos(&self) -> IndexPosition { @@ -1611,7 +1616,7 @@ impl ReadonlyIndex { return Some(mid); } let entry = self.lookup_entry(mid); - if entry.commit_id().as_bytes() < prefix.as_bytes() { + if entry.commit_id_bytes() < prefix.as_bytes() { low = mid + 1; } else { high = mid;