Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
0ca97b50
Commit
0ca97b50
authored
Jan 23, 2026
by
Luke Sandberg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
used stdlib binary search
parent
d4abd460
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
24 deletions
+28
-24
static_sorted_file.rs
turbopack/crates/turbo-persistence/src/static_sorted_file.rs
+28
-24
No files found.
turbopack/crates/turbo-persistence/src/static_sorted_file.rs
View file @
0ca97b50
...
@@ -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
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment