Commit 0ca97b50 authored by Luke Sandberg's avatar Luke Sandberg

used stdlib binary search

parent d4abd460
...@@ -172,8 +172,7 @@ impl StaticSortedFile { ...@@ -172,8 +172,7 @@ impl StaticSortedFile {
let rotated_key = rotate_key(key); let rotated_key = rotate_key(key);
// Binary search on key table (only 4 bytes per comparison) // Binary search on key table (only 4 bytes per comparison)
if let Some(index) = if let Some(index) = self.binary_search_key_table(rotated_key, key_table_start, entry_count)
self.binary_search_key_table(rotated_key, key_table_start, entry_count)?
{ {
// Found - read value from value table // Found - read value from value table
let value_offset = value_table_start + index * VALUE_SIZE; let value_offset = value_table_start + index * VALUE_SIZE;
...@@ -185,34 +184,40 @@ impl StaticSortedFile { ...@@ -185,34 +184,40 @@ impl StaticSortedFile {
Ok(None) Ok(None)
} }
/// Binary search on a key table (u32 keys, 4 bytes each). /// Binary search on a key table (u32 keys stored as big-endian, 4 bytes each).
/// Returns the index if found, None otherwise. /// Returns the index if found, None otherwise.
///
/// Uses the standard library's optimized binary search which is well-tuned
/// for cache efficiency. Keys are compared as big-endian bytes directly
/// (byte-wise comparison of BE bytes is equivalent to numeric comparison).
fn binary_search_key_table( fn binary_search_key_table(
&self, &self,
rotated_key: u32, rotated_key: u32,
key_table_start: usize, key_table_start: usize,
entry_count: usize, entry_count: usize,
) -> Result<Option<usize>> { ) -> Option<usize> {
let mut l = 0; if entry_count == 0 {
let mut r = entry_count; return None;
while l < r { }
let m = (l + r) / 2;
let key_offset = key_table_start + m * 4;
let mid_key = (&self.mmap[key_offset..key_offset + 4]).read_u32::<BE>()?;
match rotated_key.cmp(&mid_key) { let key_table_end = key_table_start + entry_count * 4;
Ordering::Less => { let key_bytes = &self.mmap[key_table_start..key_table_end];
r = m;
} // SAFETY: &[u8] can always be reinterpreted as &[[u8; 4]] when the length
Ordering::Equal => { // is a multiple of 4, since [u8; 4] has alignment 1. We've ensured the
return Ok(Some(m)); // slice length is exactly entry_count * 4.
} let key_table: &[[u8; 4]] =
Ordering::Greater => { unsafe { std::slice::from_raw_parts(key_bytes.as_ptr().cast(), entry_count) };
l = m + 1;
} // Convert search key to big-endian bytes for comparison.
} // Comparing [u8; 4] lexicographically is equivalent to comparing
// the u32 values when both are in big-endian format.
let search_key = rotated_key.to_be_bytes();
match key_table.binary_search(&search_key) {
Ok(index) => Some(index),
Err(_) => None,
} }
Ok(None)
} }
/// Looks up a u32 key in a direct-variable SST file. /// Looks up a u32 key in a direct-variable SST file.
...@@ -259,8 +264,7 @@ impl StaticSortedFile { ...@@ -259,8 +264,7 @@ impl StaticSortedFile {
let rotated_key = rotate_key(key); let rotated_key = rotate_key(key);
// Binary search on key table // Binary search on key table
if let Some(index) = if let Some(index) = self.binary_search_key_table(rotated_key, key_table_start, entry_count)
self.binary_search_key_table(rotated_key, key_table_start, entry_count)?
{ {
// Found - read value location (8 bytes) // Found - read value location (8 bytes)
let loc_offset = value_loc_start + index * 8; let loc_offset = value_loc_start + index * 8;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment