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
fc5689a8
Commit
fc5689a8
authored
Mar 01, 2026
by
Tobias Koppers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor thread_local_state to closure-based API to prevent aliased &mut
parent
603377dd
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
28 additions
and
19 deletions
+28
-19
write_batch.rs
turbopack/crates/turbo-persistence/src/write_batch.rs
+28
-19
No files found.
turbopack/crates/turbo-persistence/src/write_batch.rs
View file @
fc5689a8
...
...
@@ -104,17 +104,23 @@ impl<K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize>
}
}
/// Returns the thread local state for the current thread.
#[allow(clippy
::
mut_from_ref)]
fn
thread_local_state
(
&
self
)
->
&
mut
ThreadLocalState
<
K
,
FAMILIES
>
{
/// Runs a closure with exclusive access to the current thread's local state.
///
/// The closure-based API ensures that only one `&mut` reference to the
/// thread-local state exists at a time, preventing accidental aliasing.
fn
with_thread_local_state
<
R
>
(
&
self
,
f
:
impl
FnOnce
(
&
mut
ThreadLocalState
<
K
,
FAMILIES
>
)
->
R
,
)
->
R
{
let
cell
=
self
.thread_locals
.get_or
(||
{
SyncUnsafeCell
::
new
(
ThreadLocalState
{
collectors
:
[
const
{
None
};
FAMILIES
],
new_blob_files
:
Vec
::
new
(),
})
});
// Safety: We know that the cell is only accessed from the current thread.
unsafe
{
&
mut
*
cell
.get
()
}
// Safety: ThreadLocal guarantees this cell belongs to the current thread.
// The closure-based API ensures only one &mut reference exists at a time.
f
(
unsafe
{
&
mut
*
cell
.get
()
})
}
/// Returns the collector for a family for the current thread.
...
...
@@ -204,24 +210,27 @@ impl<K: StoreKey + Send + Sync, S: ParallelScheduler, const FAMILIES: usize>
/// Puts a key-value pair into the write batch.
pub
fn
put
(
&
self
,
family
:
u32
,
key
:
K
,
value
:
ValueBuffer
<
'_
>
)
->
Result
<
()
>
{
let
state
=
self
.thread_local_state
();
let
collector
=
self
.thread_local_collector_mut
(
state
,
family
)
?
;
if
value
.len
()
<=
MAX_MEDIUM_VALUE_SIZE
{
collector
.put
(
key
,
value
);
}
else
{
let
(
blob
,
file
)
=
self
.create_blob
(
&
value
)
?
;
collector
.put_blob
(
key
,
blob
);
state
.new_blob_files
.push
((
blob
,
file
));
}
Ok
(())
self
.with_thread_local_state
(|
state
|
{
let
collector
=
self
.thread_local_collector_mut
(
state
,
family
)
?
;
if
value
.len
()
<=
MAX_MEDIUM_VALUE_SIZE
{
collector
.put
(
key
,
value
);
}
else
{
let
(
blob
,
file
)
=
self
.create_blob
(
&
value
)
?
;
collector
.put_blob
(
key
,
blob
);
// NLL: collector borrow ends here (last use above)
state
.new_blob_files
.push
((
blob
,
file
));
}
Ok
(())
})
}
/// Puts a delete operation into the write batch.
pub
fn
delete
(
&
self
,
family
:
u32
,
key
:
K
)
->
Result
<
()
>
{
let
state
=
self
.thread_local_state
();
let
collector
=
self
.thread_local_collector_mut
(
state
,
family
)
?
;
collector
.delete
(
key
);
Ok
(())
self
.with_thread_local_state
(|
state
|
{
let
collector
=
self
.thread_local_collector_mut
(
state
,
family
)
?
;
collector
.delete
(
key
);
Ok
(())
})
}
/// Flushes a family of the write batch, reducing the amount of buffered memory used.
...
...
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