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
b044b580
Commit
b044b580
authored
Jan 29, 2026
by
Luke Sandberg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
improve collision tolerant compaction and writing implemenations
parent
16a3edab
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
365 additions
and
21 deletions
+365
-21
collector.rs
turbopack/crates/turbo-persistence/src/collector.rs
+5
-1
collector_entry.rs
turbopack/crates/turbo-persistence/src/collector_entry.rs
+75
-0
lookup_entry.rs
turbopack/crates/turbo-persistence/src/lookup_entry.rs
+60
-18
merge_iter.rs
turbopack/crates/turbo-persistence/src/merge_iter.rs
+6
-1
tests.rs
turbopack/crates/turbo-persistence/src/tests.rs
+219
-1
No files found.
turbopack/crates/turbo-persistence/src/collector.rs
View file @
b044b580
...
@@ -112,8 +112,12 @@ impl<K: StoreKey, const SIZE_SHIFT: usize> Collector<K, SIZE_SHIFT> {
...
@@ -112,8 +112,12 @@ impl<K: StoreKey, const SIZE_SHIFT: usize> Collector<K, SIZE_SHIFT> {
/// Sorts the entries and returns them along with the total key size. This doesn't
/// Sorts the entries and returns them along with the total key size. This doesn't
/// clear the entries.
/// clear the entries.
///
/// Entries are sorted by (key, value) to ensure entries with the same key but different
/// values are grouped together. This is important for collision-tolerant deduplication
/// during compaction.
pub
fn
sorted
(
&
mut
self
)
->
(
&
[
CollectorEntry
<
K
>
],
usize
)
{
pub
fn
sorted
(
&
mut
self
)
->
(
&
[
CollectorEntry
<
K
>
],
usize
)
{
self
.entries
.sort_unstable
_by
(|
a
,
b
|
a
.key
.cmp
(
&
b
.key
)
);
self
.entries
.sort_unstable
(
);
(
&
self
.entries
,
self
.total_key_size
)
(
&
self
.entries
,
self
.total_key_size
)
}
}
...
...
turbopack/crates/turbo-persistence/src/collector_entry.rs
View file @
b044b580
...
@@ -11,6 +11,28 @@ pub struct CollectorEntry<K: StoreKey> {
...
@@ -11,6 +11,28 @@ pub struct CollectorEntry<K: StoreKey> {
pub
value
:
CollectorEntryValue
,
pub
value
:
CollectorEntryValue
,
}
}
impl
<
K
:
StoreKey
>
PartialEq
for
CollectorEntry
<
K
>
{
fn
eq
(
&
self
,
other
:
&
Self
)
->
bool
{
self
.key
==
other
.key
&&
self
.value
==
other
.value
}
}
impl
<
K
:
StoreKey
>
Eq
for
CollectorEntry
<
K
>
{}
impl
<
K
:
StoreKey
>
PartialOrd
for
CollectorEntry
<
K
>
{
fn
partial_cmp
(
&
self
,
other
:
&
Self
)
->
Option
<
Ordering
>
{
Some
(
self
.cmp
(
other
))
}
}
impl
<
K
:
StoreKey
>
Ord
for
CollectorEntry
<
K
>
{
fn
cmp
(
&
self
,
other
:
&
Self
)
->
Ordering
{
self
.key
.cmp
(
&
other
.key
)
.then_with
(||
self
.value
.cmp
(
&
other
.value
))
}
}
/// The size threshold for inline storage in CollectorEntryValue, this is the largest value that can
/// The size threshold for inline storage in CollectorEntryValue, this is the largest value that can
/// be stored inline without inflating the size of the enum
/// be stored inline without inflating the size of the enum
pub
const
TINY_VALUE_THRESHOLD
:
usize
=
22
;
pub
const
TINY_VALUE_THRESHOLD
:
usize
=
22
;
...
@@ -45,6 +67,59 @@ impl CollectorEntryValue {
...
@@ -45,6 +67,59 @@ impl CollectorEntryValue {
CollectorEntryValue
::
Deleted
=>
0
,
CollectorEntryValue
::
Deleted
=>
0
,
}
}
}
}
/// Returns the byte slice for byte-content values, or None for Large/Deleted.
fn
as_bytes
(
&
self
)
->
Option
<&
[
u8
]
>
{
match
self
{
CollectorEntryValue
::
Tiny
{
value
,
len
}
=>
Some
(
&
value
[
..*
len
as
usize
]),
CollectorEntryValue
::
Small
{
value
}
=>
Some
(
value
),
CollectorEntryValue
::
Medium
{
value
}
=>
Some
(
value
),
CollectorEntryValue
::
Large
{
..
}
|
CollectorEntryValue
::
Deleted
=>
None
,
}
}
}
impl
PartialEq
for
CollectorEntryValue
{
fn
eq
(
&
self
,
other
:
&
Self
)
->
bool
{
self
.cmp
(
other
)
==
Ordering
::
Equal
}
}
impl
Eq
for
CollectorEntryValue
{}
impl
PartialOrd
for
CollectorEntryValue
{
fn
partial_cmp
(
&
self
,
other
:
&
Self
)
->
Option
<
Ordering
>
{
Some
(
self
.cmp
(
other
))
}
}
impl
Ord
for
CollectorEntryValue
{
fn
cmp
(
&
self
,
other
:
&
Self
)
->
Ordering
{
// Order: byte-content values < Large < Deleted
// For byte-content values, compare by bytes
match
(
self
.as_bytes
(),
other
.as_bytes
())
{
(
Some
(
a
),
Some
(
b
))
=>
a
.cmp
(
b
),
(
Some
(
_
),
None
)
=>
Ordering
::
Less
,
(
None
,
Some
(
_
))
=>
Ordering
::
Greater
,
(
None
,
None
)
=>
{
// Both are Large or Deleted
match
(
self
,
other
)
{
(
CollectorEntryValue
::
Large
{
blob
:
a
},
CollectorEntryValue
::
Large
{
blob
:
b
},
)
=>
a
.cmp
(
b
),
(
CollectorEntryValue
::
Large
{
..
},
CollectorEntryValue
::
Deleted
)
=>
{
Ordering
::
Less
}
(
CollectorEntryValue
::
Deleted
,
CollectorEntryValue
::
Large
{
..
})
=>
{
Ordering
::
Greater
}
(
CollectorEntryValue
::
Deleted
,
CollectorEntryValue
::
Deleted
)
=>
Ordering
::
Equal
,
_
=>
unreachable!
(
"as_bytes() returned None for non-Large/Deleted variant"
),
}
}
}
}
}
}
pub
struct
EntryKey
<
K
:
StoreKey
>
{
pub
struct
EntryKey
<
K
:
StoreKey
>
{
...
...
turbopack/crates/turbo-persistence/src/lookup_entry.rs
View file @
b044b580
use
std
::
cmp
::
Ordering
;
use
crate
::{
use
crate
::{
ArcSlice
,
ArcSlice
,
constants
::{
MAX_INLINE_VALUE_SIZE
,
MAX_SMALL_VALUE_SIZE
},
constants
::{
MAX_INLINE_VALUE_SIZE
,
MAX_SMALL_VALUE_SIZE
},
...
@@ -45,24 +47,64 @@ impl LazyLookupValue<'_> {
...
@@ -45,24 +47,64 @@ impl LazyLookupValue<'_> {
/// For `Medium` values, compares the compressed block bytes directly
/// For `Medium` values, compares the compressed block bytes directly
/// (assumes deterministic compression).
/// (assumes deterministic compression).
pub
fn
eq_for_dedup
(
&
self
,
other
:
&
Self
)
->
bool
{
pub
fn
eq_for_dedup
(
&
self
,
other
:
&
Self
)
->
bool
{
match
(
self
,
other
)
{
self
.cmp_for_dedup
(
other
)
==
Ordering
::
Equal
(
}
LazyLookupValue
::
Eager
(
LookupValue
::
Deleted
),
LazyLookupValue
::
Eager
(
LookupValue
::
Deleted
),
/// Compares two values for ordering purposes during merge sort.
)
=>
true
,
///
(
/// Order: Deleted < byte-content values (Slice/Medium) < Blob
LazyLookupValue
::
Eager
(
LookupValue
::
Slice
{
value
:
a
}),
/// This ensures entries with the same key are grouped by value for deduplication.
LazyLookupValue
::
Eager
(
LookupValue
::
Slice
{
value
:
b
}),
pub
fn
cmp_for_dedup
(
&
self
,
other
:
&
Self
)
->
Ordering
{
)
=>
a
.as_ref
()
==
b
.as_ref
(),
// Assign a type order: Deleted=0, Slice/Medium=1, Blob=2
(
fn
type_order
(
v
:
&
LazyLookupValue
)
->
u8
{
LazyLookupValue
::
Eager
(
LookupValue
::
Blob
{
sequence_number
:
a
}),
match
v
{
LazyLookupValue
::
Eager
(
LookupValue
::
Blob
{
sequence_number
:
b
}),
LazyLookupValue
::
Eager
(
LookupValue
::
Deleted
)
=>
0
,
)
=>
a
==
b
,
LazyLookupValue
::
Eager
(
LookupValue
::
Slice
{
..
})
(
|
LazyLookupValue
::
Medium
{
..
}
=>
1
,
LazyLookupValue
::
Medium
{
block
:
a
,
..
},
LazyLookupValue
::
Eager
(
LookupValue
::
Blob
{
..
})
=>
2
,
LazyLookupValue
::
Medium
{
block
:
b
,
..
},
}
)
=>
*
a
==
*
b
,
}
_
=>
false
,
// Different types are not equal
match
type_order
(
self
)
.cmp
(
&
type_order
(
other
))
{
Ordering
::
Equal
=>
{
// Same type category, compare within type
match
(
self
,
other
)
{
(
LazyLookupValue
::
Eager
(
LookupValue
::
Deleted
),
LazyLookupValue
::
Eager
(
LookupValue
::
Deleted
),
)
=>
Ordering
::
Equal
,
(
LazyLookupValue
::
Eager
(
LookupValue
::
Slice
{
value
:
a
}),
LazyLookupValue
::
Eager
(
LookupValue
::
Slice
{
value
:
b
}),
)
=>
a
.as_ref
()
.cmp
(
b
.as_ref
()),
(
LazyLookupValue
::
Medium
{
block
:
a
,
..
},
LazyLookupValue
::
Medium
{
block
:
b
,
..
},
)
=>
a
.cmp
(
b
),
(
LazyLookupValue
::
Eager
(
LookupValue
::
Slice
{
value
}),
LazyLookupValue
::
Medium
{
block
,
..
},
)
=>
{
// Cross-type comparison: compare slice bytes with compressed bytes
// This shouldn't happen for the same semantic value, but we need a total
// order
value
.as_ref
()
.cmp
(
*
block
)
}
(
LazyLookupValue
::
Medium
{
block
,
..
},
LazyLookupValue
::
Eager
(
LookupValue
::
Slice
{
value
}),
)
=>
{
// Cross-type comparison
(
*
block
)
.cmp
(
value
.as_ref
())
}
(
LazyLookupValue
::
Eager
(
LookupValue
::
Blob
{
sequence_number
:
a
}),
LazyLookupValue
::
Eager
(
LookupValue
::
Blob
{
sequence_number
:
b
}),
)
=>
a
.cmp
(
b
),
_
=>
unreachable!
(
"type_order comparison should have handled this"
),
}
}
ord
=>
ord
,
}
}
}
}
}
}
...
...
turbopack/crates/turbo-persistence/src/merge_iter.rs
View file @
b044b580
...
@@ -14,7 +14,9 @@ struct ActiveIterator<'l, T: Iterator<Item = Result<LookupEntry<'l>>>> {
...
@@ -14,7 +14,9 @@ struct ActiveIterator<'l, T: Iterator<Item = Result<LookupEntry<'l>>>> {
impl
<
'l
,
T
:
Iterator
<
Item
=
Result
<
LookupEntry
<
'l
>>>>
PartialEq
for
ActiveIterator
<
'l
,
T
>
{
impl
<
'l
,
T
:
Iterator
<
Item
=
Result
<
LookupEntry
<
'l
>>>>
PartialEq
for
ActiveIterator
<
'l
,
T
>
{
fn
eq
(
&
self
,
other
:
&
Self
)
->
bool
{
fn
eq
(
&
self
,
other
:
&
Self
)
->
bool
{
self
.entry.hash
==
other
.entry.hash
&&
*
self
.entry.key
==
*
other
.entry.key
self
.entry.hash
==
other
.entry.hash
&&
*
self
.entry.key
==
*
other
.entry.key
&&
self
.entry.value
.eq_for_dedup
(
&
other
.entry.value
)
}
}
}
}
...
@@ -32,6 +34,9 @@ impl<'l, T: Iterator<Item = Result<LookupEntry<'l>>>> Ord for ActiveIterator<'l,
...
@@ -32,6 +34,9 @@ impl<'l, T: Iterator<Item = Result<LookupEntry<'l>>>> Ord for ActiveIterator<'l,
.hash
.hash
.cmp
(
&
other
.entry.hash
)
.cmp
(
&
other
.entry.hash
)
.then_with
(||
(
*
self
.entry.key
)
.cmp
(
&
other
.entry.key
))
.then_with
(||
(
*
self
.entry.key
)
.cmp
(
&
other
.entry.key
))
// Sort by value to group entries with the same (key, value) together
// for collision-tolerant deduplication
.then_with
(||
self
.entry.value
.cmp_for_dedup
(
&
other
.entry.value
))
.then_with
(||
self
.order
.cmp
(
&
other
.order
))
.then_with
(||
self
.order
.cmp
(
&
other
.order
))
.reverse
()
.reverse
()
}
}
...
...
turbopack/crates/turbo-persistence/src/tests.rs
View file @
b044b580
...
@@ -4,7 +4,8 @@ use anyhow::Result;
...
@@ -4,7 +4,8 @@ use anyhow::Result;
use
rayon
::
iter
::{
IntoParallelIterator
,
ParallelIterator
};
use
rayon
::
iter
::{
IntoParallelIterator
,
ParallelIterator
};
use
crate
::{
use
crate
::{
constants
::{
DbConfig
,
DeduplicationMode
,
FamilyConfig
,
MAX_MEDIUM_VALUE_SIZE
},
DbConfig
,
DeduplicationMode
,
FamilyConfig
,
constants
::
MAX_MEDIUM_VALUE_SIZE
,
db
::{
CompactConfig
,
TurboPersistence
},
db
::{
CompactConfig
,
TurboPersistence
},
parallel_scheduler
::
ParallelScheduler
,
parallel_scheduler
::
ParallelScheduler
,
write_batch
::
WriteBatch
,
write_batch
::
WriteBatch
,
...
@@ -1730,3 +1731,220 @@ fn compaction_by_key_and_value_preserves_all_unique_values() -> Result<()> {
...
@@ -1730,3 +1731,220 @@ fn compaction_by_key_and_value_preserves_all_unique_values() -> Result<()> {
Ok
(())
Ok
(())
}
}
#[test]
fn
compaction_by_key_and_value_interleaved_batches
()
->
Result
<
()
>
{
let
tempdir
=
tempfile
::
tempdir
()
?
;
let
path
=
tempdir
.path
();
// Configure family 0 to use ByKeyAndValue deduplication
let
mut
config
=
DbConfig
::
<
1
>
::
default
();
config
.family_configs
[
0
]
=
FamilyConfig
{
deduplication_mode
:
DeduplicationMode
::
ByKeyAndValue
,
..
Default
::
default
()
};
let
key
=
vec!
[
42u8
];
{
let
db
=
TurboPersistence
::
<
_
,
1
>
::
open_with_config_and_parallel_scheduler
(
path
.to_path_buf
(),
config
,
RayonParallelScheduler
,
)
?
;
// Write interleaved values across multiple batches:
// Batch 1: value=1
// Batch 2: value=2
// Batch 3: value=1 (duplicate)
// Batch 4: value=3
// Batch 5: value=2 (duplicate)
// This tests that the merge sort correctly groups same values together
// even when they come from different SST files in different orders.
for
value
in
[
1u8
,
2
,
1
,
3
,
2
]
{
let
batch
=
db
.write_batch
()
?
;
batch
.put
(
0
,
key
.clone
(),
vec!
[
value
]
.into
())
?
;
db
.commit_write_batch
(
batch
)
?
;
}
// Before compaction: 5 entries exist
let
results
=
db
.get_multiple
(
0
,
&
key
.as_slice
())
?
;
assert_eq!
(
results
.len
(),
5
,
"Should have 5 entries before compaction"
);
// Compact
db
.full_compact
()
?
;
// After compaction: should have exactly 3 unique values (1, 2, 3)
// The duplicates should be merged
let
results
=
db
.get_multiple
(
0
,
&
key
.as_slice
())
?
;
assert_eq!
(
results
.len
(),
3
,
"ByKeyAndValue should deduplicate to 3 unique values after compaction"
);
let
mut
values
:
Vec
<
u8
>
=
results
.iter
()
.map
(|
r
|
r
[
0
])
.collect
();
values
.sort
();
assert_eq!
(
values
,
vec!
[
1
,
2
,
3
],
"Should have values 1, 2, 3"
);
db
.shutdown
()
?
;
}
Ok
(())
}
#[test]
fn
compaction_by_key_and_value_same_batch_different_values
()
->
Result
<
()
>
{
let
tempdir
=
tempfile
::
tempdir
()
?
;
let
path
=
tempdir
.path
();
// Configure family 0 to use ByKeyAndValue deduplication
let
mut
config
=
DbConfig
::
<
1
>
::
default
();
config
.family_configs
[
0
]
=
FamilyConfig
{
deduplication_mode
:
DeduplicationMode
::
ByKeyAndValue
,
..
Default
::
default
()
};
let
key
=
vec!
[
42u8
];
{
let
db
=
TurboPersistence
::
<
_
,
1
>
::
open_with_config_and_parallel_scheduler
(
path
.to_path_buf
(),
config
,
RayonParallelScheduler
,
)
?
;
// Write multiple different values in the SAME batch
// All entries go into a single SST file
let
batch
=
db
.write_batch
()
?
;
batch
.put
(
0
,
key
.clone
(),
vec!
[
3u8
]
.into
())
?
;
batch
.put
(
0
,
key
.clone
(),
vec!
[
1u8
]
.into
())
?
;
batch
.put
(
0
,
key
.clone
(),
vec!
[
2u8
]
.into
())
?
;
batch
.put
(
0
,
key
.clone
(),
vec!
[
1u8
]
.into
())
?
;
// duplicate
batch
.put
(
0
,
key
.clone
(),
vec!
[
3u8
]
.into
())
?
;
// duplicate
db
.commit_write_batch
(
batch
)
?
;
// All 5 entries are stored in the SST file
let
results
=
db
.get_multiple
(
0
,
&
key
.as_slice
())
?
;
assert_eq!
(
results
.len
(),
5
,
"Should have 5 entries in single SST"
);
// Compaction on a single file just moves it (no merge = no deduplication)
db
.full_compact
()
?
;
// Still 5 entries - single-file compaction doesn't deduplicate
let
results
=
db
.get_multiple
(
0
,
&
key
.as_slice
())
?
;
assert_eq!
(
results
.len
(),
5
,
"Single-file compaction preserves all entries"
);
// Now write a second batch with a new value to create a second SST file
let
batch
=
db
.write_batch
()
?
;
batch
.put
(
0
,
key
.clone
(),
vec!
[
4u8
]
.into
())
?
;
db
.commit_write_batch
(
batch
)
?
;
// 6 entries total now
let
results
=
db
.get_multiple
(
0
,
&
key
.as_slice
())
?
;
assert_eq!
(
results
.len
(),
6
,
"Should have 6 entries across 2 SST files"
);
// Second compaction will merge the two files and deduplicate
db
.full_compact
()
?
;
// After multi-file compaction: should have exactly 4 unique values (1, 2, 3, 4)
let
results
=
db
.get_multiple
(
0
,
&
key
.as_slice
())
?
;
assert_eq!
(
results
.len
(),
4
,
"Multi-file compaction should deduplicate to 4 unique values"
);
let
mut
values
:
Vec
<
u8
>
=
results
.iter
()
.map
(|
r
|
r
[
0
])
.collect
();
values
.sort
();
assert_eq!
(
values
,
vec!
[
1
,
2
,
3
,
4
],
"Should have values 1, 2, 3, 4"
);
db
.shutdown
()
?
;
}
Ok
(())
}
#[test]
fn
compaction_by_key_and_value_multiple_compactions
()
->
Result
<
()
>
{
let
tempdir
=
tempfile
::
tempdir
()
?
;
let
path
=
tempdir
.path
();
// Configure family 0 to use ByKeyAndValue deduplication
let
mut
config
=
DbConfig
::
<
1
>
::
default
();
config
.family_configs
[
0
]
=
FamilyConfig
{
deduplication_mode
:
DeduplicationMode
::
ByKeyAndValue
,
..
Default
::
default
()
};
let
key
=
vec!
[
42u8
];
{
let
db
=
TurboPersistence
::
<
_
,
1
>
::
open_with_config_and_parallel_scheduler
(
path
.to_path_buf
(),
config
.clone
(),
RayonParallelScheduler
,
)
?
;
// Write initial values
for
value
in
[
1u8
,
2
,
3
]
{
let
batch
=
db
.write_batch
()
?
;
batch
.put
(
0
,
key
.clone
(),
vec!
[
value
]
.into
())
?
;
db
.commit_write_batch
(
batch
)
?
;
}
db
.full_compact
()
?
;
// After first compaction: 3 unique values
let
results
=
db
.get_multiple
(
0
,
&
key
.as_slice
())
?
;
assert_eq!
(
results
.len
(),
3
);
// Add more values (some duplicates)
for
value
in
[
2u8
,
4
,
1
]
{
let
batch
=
db
.write_batch
()
?
;
batch
.put
(
0
,
key
.clone
(),
vec!
[
value
]
.into
())
?
;
db
.commit_write_batch
(
batch
)
?
;
}
// Before second compaction: 6 entries total (3 old + 3 new)
let
results
=
db
.get_multiple
(
0
,
&
key
.as_slice
())
?
;
assert_eq!
(
results
.len
(),
6
);
// Second compaction
db
.full_compact
()
?
;
// After second compaction: 4 unique values (1, 2, 3, 4)
let
results
=
db
.get_multiple
(
0
,
&
key
.as_slice
())
?
;
assert_eq!
(
results
.len
(),
4
,
"Should have 4 unique values after second compaction"
);
let
mut
values
:
Vec
<
u8
>
=
results
.iter
()
.map
(|
r
|
r
[
0
])
.collect
();
values
.sort
();
assert_eq!
(
values
,
vec!
[
1
,
2
,
3
,
4
],
"Should have values 1, 2, 3, 4"
);
db
.shutdown
()
?
;
}
// Reopen and verify persistence
{
let
db
=
TurboPersistence
::
<
_
,
1
>
::
open_with_config_and_parallel_scheduler
(
path
.to_path_buf
(),
config
,
RayonParallelScheduler
,
)
?
;
let
results
=
db
.get_multiple
(
0
,
&
key
.as_slice
())
?
;
assert_eq!
(
results
.len
(),
4
,
"Should still have 4 values after reopen"
);
db
.shutdown
()
?
;
}
Ok
(())
}
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