Unverified Commit 4e03bf93 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

turbo-persistence: use partition_point for batch lookup bounds (#100821)

Follow-up to #100620, addressing [this review
comment](https://github.com/vercel/next.js/pull/100620#discussion_r4399047345).

### What?

`MetaFile::batch_lookup_entry` now finds the cells inside an SST file's
hash range with `partition_point` instead of `binary_search_by`.

### Why?

The old code used `binary_search_by` with a comparator that never
returns `Equal` (`.then(Ordering::Greater)` / `.then(Ordering::Less)`)
and then took the insertion point with `.err().unwrap()`.
`partition_point` states the bounds directly and is easier to read.

### How?

- Start: `cells.partition_point(|&(hash, _)| hash < range.min_hash)`,
the first cell with `hash >= min_hash`.
- End: `cells.partition_point(|&(hash, _)| hash <= range.max_hash)`, the
first cell with `hash > max_hash`.

Both return the same indices as before for every input, including
duplicate hashes, empty slices, and bounds at `0` or `u64::MAX`. The now
unused `Ordering` import is removed. There is no behavior change.

<!-- NEXT_JS_LLM -->


<!-- fleet 2e9e7c99-0cc2-4e7b-9e62-627c0627638b -->
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent ced39776
use std::{ use std::{
cmp::Ordering,
fmt::Display, fmt::Display,
mem::take, mem::take,
ops::Deref, ops::Deref,
...@@ -640,10 +639,7 @@ impl MetaFile { ...@@ -640,10 +639,7 @@ impl MetaFile {
); );
#[allow(unused_mut, reason = "It's used when stats are enabled")] #[allow(unused_mut, reason = "It's used when stats are enabled")]
let mut lookup_result = MetaBatchLookupResult::default(); let mut lookup_result = MetaBatchLookupResult::default();
let start_index = cells let start_index = cells.partition_point(|&(hash, _)| hash < range.min_hash);
.binary_search_by(|(hash, _)| hash.cmp(&range.min_hash).then(Ordering::Greater))
.err()
.unwrap();
if start_index >= cells.len() { if start_index >= cells.len() {
#[cfg(feature = "stats")] #[cfg(feature = "stats")]
{ {
...@@ -651,10 +647,7 @@ impl MetaFile { ...@@ -651,10 +647,7 @@ impl MetaFile {
} }
return Ok(lookup_result); return Ok(lookup_result);
} }
let end_index = cells let end_index = cells.partition_point(|&(hash, _)| hash <= range.max_hash);
.binary_search_by(|(hash, _)| hash.cmp(&range.max_hash).then(Ordering::Less))
.err()
.unwrap();
if start_index >= end_index { if start_index >= end_index {
#[cfg(feature = "stats")] #[cfg(feature = "stats")]
{ {
......
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