From d771c126376a5c7df15c2dad38869587b1bc0cb2 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Thu, 26 Jan 2023 15:45:23 +0900 Subject: [PATCH] index: make HexPrefix accessor simply return "min" prefix as bytes slice This is low-level function, so I think using &[u8] should be good here. --- lib/src/index.rs | 36 +++++++++++++++++------------------- lib/src/repo.rs | 4 ++-- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/lib/src/index.rs b/lib/src/index.rs index 22e305131..52d639538 100644 --- a/lib/src/index.rs +++ b/lib/src/index.rs @@ -310,6 +310,13 @@ impl HexPrefix { hex_string } + /// Minimum bytes that would match this prefix. (e.g. "abc0" for "abc") + /// + /// Use this to partition a sorted slice, and test `matches(id)` from there. + pub fn min_prefix_bytes(&self) -> &[u8] { + &self.min_prefix_bytes + } + fn split_odd_byte(&self) -> (Option, &[u8]) { if self.has_odd_byte { let (&odd, prefix) = self.min_prefix_bytes.split_last().unwrap(); @@ -319,11 +326,6 @@ impl HexPrefix { } } - pub fn bytes_prefixes(&self) -> (Q, Q) { - let (_, prefix) = self.split_odd_byte(); - (Q::from_bytes(prefix), Q::from_bytes(&self.min_prefix_bytes)) - } - pub fn matches(&self, id: &Q) -> bool { let id_bytes = id.as_bytes(); let (maybe_odd, prefix) = self.split_odd_byte(); @@ -1333,7 +1335,7 @@ impl IndexSegment for ReadonlyIndex { } fn segment_resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution { - let (_, min_bytes_prefix) = prefix.bytes_prefixes(); + let min_bytes_prefix = CommitId::from_bytes(prefix.min_prefix_bytes()); let lookup_pos = self .commit_id_byte_prefix_to_lookup_pos(&min_bytes_prefix) .unwrap_or(self.num_local_commits); @@ -1428,7 +1430,7 @@ impl IndexSegment for MutableIndex { } fn segment_resolve_prefix(&self, prefix: &HexPrefix) -> PrefixResolution { - let (_, min_bytes_prefix) = prefix.bytes_prefixes(); + let min_bytes_prefix = CommitId::from_bytes(prefix.min_prefix_bytes()); let mut matches = self .lookup .range((Bound::Included(&min_bytes_prefix), Bound::Unbounded)) @@ -2598,21 +2600,17 @@ mod tests { #[test] fn test_hex_prefix_prefixes() { - let (prefix, min_prefix) = HexPrefix::new("").unwrap().bytes_prefixes::(); - assert_eq!(prefix, CommitId::from_hex("")); - assert_eq!(min_prefix, CommitId::from_hex("")); + let prefix = HexPrefix::new("").unwrap(); + assert_eq!(prefix.min_prefix_bytes(), b""); - let (prefix, min_prefix) = HexPrefix::new("1").unwrap().bytes_prefixes::(); - assert_eq!(prefix, CommitId::from_hex("")); - assert_eq!(min_prefix, CommitId::from_hex("10")); + let prefix = HexPrefix::new("1").unwrap(); + assert_eq!(prefix.min_prefix_bytes(), b"\x10"); - let (prefix, min_prefix) = HexPrefix::new("12").unwrap().bytes_prefixes::(); - assert_eq!(prefix, CommitId::from_hex("12")); - assert_eq!(min_prefix, CommitId::from_hex("12")); + let prefix = HexPrefix::new("12").unwrap(); + assert_eq!(prefix.min_prefix_bytes(), b"\x12"); - let (prefix, min_prefix) = HexPrefix::new("123").unwrap().bytes_prefixes::(); - assert_eq!(prefix, CommitId::from_hex("12")); - assert_eq!(min_prefix, CommitId::from_hex("1230")); + let prefix = HexPrefix::new("123").unwrap(); + assert_eq!(prefix.min_prefix_bytes(), b"\x12\x30"); } #[test] diff --git a/lib/src/repo.rs b/lib/src/repo.rs index 23e2b2dd4..d7d90b061 100644 --- a/lib/src/repo.rs +++ b/lib/src/repo.rs @@ -1306,8 +1306,8 @@ where &'a self, prefix: &'b HexPrefix, ) -> impl Iterator + 'b { - let (_, min_bytes_prefix) = prefix.bytes_prefixes::(); - let pos = self.0.partition_point(|(k, _)| k < &min_bytes_prefix); + let min_bytes = prefix.min_prefix_bytes(); + let pos = self.0.partition_point(|(k, _)| k.as_bytes() < min_bytes); self.0[pos..] .iter() .take_while(|(k, _)| prefix.matches(k))