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.
This commit is contained in:
Yuya Nishihara 2023-01-20 14:13:19 +09:00
parent 2b7021664d
commit 763a8cc0f1

View file

@ -203,7 +203,12 @@ impl CommitLookupEntry<'_> {
} }
fn commit_id(&self) -> CommitId { 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 { fn pos(&self) -> IndexPosition {
@ -1611,7 +1616,7 @@ impl ReadonlyIndex {
return Some(mid); return Some(mid);
} }
let entry = self.lookup_entry(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; low = mid + 1;
} else { } else {
high = mid; high = mid;