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
337a6078
Commit
337a6078
authored
Mar 05, 2026
by
Benjamin Woodruff
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
more lifetime cleanup, return guard object instead of using with_* method
parent
00555abd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
51 deletions
+45
-51
mod.rs
...opack/crates/turbo-tasks-backend/src/database/lmdb/mod.rs
+23
-33
read_tx_cache.rs
...es/turbo-tasks-backend/src/database/lmdb/read_tx_cache.rs
+22
-18
No files found.
turbopack/crates/turbo-tasks-backend/src/database/lmdb/mod.rs
View file @
337a6078
...
...
@@ -2,8 +2,7 @@ use std::{fs::create_dir_all, marker::PhantomData, path::Path, thread::available
use
anyhow
::{
Context
,
Result
};
use
lmdb
::{
Database
,
DatabaseFlags
,
Environment
,
EnvironmentFlags
,
RoTransaction
,
RwTransaction
,
Transaction
,
WriteFlags
,
Database
,
DatabaseFlags
,
Environment
,
EnvironmentFlags
,
RwTransaction
,
Transaction
,
WriteFlags
,
};
use
crate
::
database
::{
...
...
@@ -92,11 +91,8 @@ impl LmbdKeyValueDatabase {
}
}
fn
with_read_tx
<
'db
,
R
>
(
&
'db
self
,
f
:
impl
FnOnce
(
&
'db
RoTransaction
<
'db
>
,
read_tx_cache
::
ReadTxGuard
<
'db
>
)
->
Result
<
R
>
,
)
->
Result
<
R
>
{
self
.read_tx_cache
.with_read_tx
(
&
self
.env
,
f
)
fn
get_read_tx
(
&
self
)
->
read_tx_cache
::
ReadTxGuard
<
'_
>
{
self
.read_tx_cache
.get_read_tx
(
&
self
.env
)
}
fn
get_from_tx
<
'tx
>
(
...
...
@@ -115,21 +111,16 @@ impl LmbdKeyValueDatabase {
impl
KeyValueDatabase
for
LmbdKeyValueDatabase
{
type
ValueBuffer
<
'l
>
=
LmdbValueBuffer
<
'l
>
;
fn
get
<
'l
>
(
&
'l
self
,
key_space
:
super
::
key_value_database
::
KeySpace
,
key
:
&
[
u8
],
)
->
Result
<
Option
<
Self
::
ValueBuffer
<
'l
>>>
{
self
.with_read_tx
(|
tx
,
guard
|
{
let
Some
(
value
)
=
Self
::
get_from_tx
(
tx
,
self
.db
(
key_space
),
key
)
?
else
{
return
Ok
(
None
);
};
Ok
(
Some
(
LmdbValueBuffer
::
from_raw_parts
(
value
.as_ptr
(),
value
.len
(),
guard
,
)))
})
fn
get
<
'l
>
(
&
'l
self
,
key_space
:
KeySpace
,
key
:
&
[
u8
])
->
Result
<
Option
<
Self
::
ValueBuffer
<
'l
>>>
{
let
tx
=
self
.get_read_tx
();
let
Some
(
value
)
=
Self
::
get_from_tx
(
&**
tx
,
self
.db
(
key_space
),
key
)
?
else
{
return
Ok
(
None
);
};
Ok
(
Some
(
LmdbValueBuffer
::
from_raw_parts
(
value
.as_ptr
(),
value
.len
(),
tx
,
)))
}
fn
batch_get
<
'l
>
(
...
...
@@ -137,17 +128,16 @@ impl KeyValueDatabase for LmbdKeyValueDatabase {
key_space
:
KeySpace
,
keys
:
&
[
&
[
u8
]],
)
->
Result
<
Vec
<
Option
<
Self
::
ValueBuffer
<
'l
>>>>
{
self
.with_read_tx
(|
tx
,
guard
|
{
let
db
=
self
.db
(
key_space
);
keys
.iter
()
.map
(|
key
|
{
let
value
=
Self
::
get_from_tx
(
tx
,
db
,
key
)
?
;
Ok
(
value
.map
(|
value
|
{
LmdbValueBuffer
::
from_raw_parts
(
value
.as_ptr
(),
value
.len
(),
guard
.clone
())
}))
})
.collect
()
})
let
tx
=
self
.get_read_tx
();
let
db
=
self
.db
(
key_space
);
keys
.iter
()
.map
(|
key
|
{
let
value
=
Self
::
get_from_tx
(
&**
tx
,
db
,
key
)
?
;
Ok
(
value
.map
(|
value
|
{
LmdbValueBuffer
::
from_raw_parts
(
value
.as_ptr
(),
value
.len
(),
tx
.clone
())
}))
})
.collect
()
}
type
SerialWriteBatch
<
'l
>
...
...
turbopack/crates/turbo-tasks-backend/src/database/lmdb/read_tx_cache.rs
View file @
337a6078
use
std
::{
m
arker
::
PhantomData
,
m
em
::
transmute
,
ops
::
Deref
,
sync
::
Arc
};
use
std
::{
mem
::
transmute
,
ops
::
Deref
,
sync
::
Arc
};
use
arc_swap
::
ArcSwap
;
use
lmdb
::{
Environment
,
RoTransaction
};
use
thread_local
::
ThreadLocal
;
type
ReadTransactionsCache
=
ThreadLocal
<
SendRoTransaction
>
;
type
ReadTransactionsCache
=
ThreadLocal
<
SendRoTransaction
<
'static
>
>
;
struct
SendRoTransaction
(
RoTransaction
<
'static
>
);
pub
struct
SendRoTransaction
<
'tx
>
(
RoTransaction
<
'tx
>
);
impl
Deref
for
SendRoTransaction
{
type
Target
=
RoTransaction
<
'
static
>
;
impl
<
'tx
>
Deref
for
SendRoTransaction
<
'tx
>
{
type
Target
=
RoTransaction
<
'
tx
>
;
fn
deref
(
&
self
)
->
&
Self
::
Target
{
&
self
.
0
...
...
@@ -23,14 +23,22 @@ impl Deref for SendRoTransaction {
// We still do not use a transaction concurrently from multiple threads. This `Send` wrapper only
// allows moving ownership between threads when `ThreadLocal` internals move stored values at drop
// time.
unsafe
impl
Send
for
SendRoTransaction
{}
unsafe
impl
<
'tx
>
Send
for
SendRoTransaction
<
'tx
>
{}
#[derive(Clone)]
pub
struct
ReadTxGuard
<
'db
>
{
tx
:
&
'db
SendRoTransaction
<
'db
>
,
// Keep this generation alive so the borrowed read transaction (and value pointers from it)
// remain valid until all value buffers/guards from that generation are dropped.
_thread_locals
:
Arc
<
ReadTransactionsCache
>
,
_marker
:
PhantomData
<&
'db
Environment
>
,
}
impl
<
'db
>
Deref
for
ReadTxGuard
<
'db
>
{
type
Target
=
SendRoTransaction
<
'db
>
;
fn
deref
(
&
self
)
->
&
Self
::
Target
{
self
.tx
}
}
pub
struct
ReadTxCache
{
...
...
@@ -46,11 +54,7 @@ impl ReadTxCache {
}
}
pub
fn
with_read_tx
<
'db
,
R
>
(
&
'db
self
,
env
:
&
'db
Environment
,
f
:
impl
FnOnce
(
&
'db
RoTransaction
<
'db
>
,
ReadTxGuard
<
'db
>
)
->
R
,
)
->
R
{
pub
fn
get_read_tx
<
'db
>
(
&
'db
self
,
env
:
&
'db
Environment
)
->
ReadTxGuard
<
'db
>
{
let
thread_locals
=
self
.read_transactions_cache
.load
()
.clone
();
let
guard_thread_locals
=
thread_locals
.clone
();
let
tx_static
=
thread_locals
.get_or
(||
{
...
...
@@ -63,14 +67,14 @@ impl ReadTxCache {
// Safety: `tx_static` always originates from `env` and `ReadTxCache` is dropped before
// the environment in the parent database type. We narrow the externally-visible lifetime
// to `'db` so callers cannot observe or depend on `'static`.
let
tx
:
&
'db
RoTransaction
<
'db
>
=
unsafe
{
transmute
::
<&
RoTransaction
<
'static
>
,
&
'db
RoTransaction
<
'db
>>
(
tx_static
)
};
let
tx
:
&
'db
SendRoTransaction
<
'db
>
=
unsafe
{
transmute
::
<&
SendRoTransaction
<
'static
>
,
&
'db
SendRoTransaction
<
'db
>>
(
tx_static
)
};
let
guard
=
ReadTxGuard
{
ReadTxGuard
{
tx
,
_thread_locals
:
guard_thread_locals
,
_marker
:
PhantomData
,
};
f
(
tx
,
guard
)
}
}
pub
fn
swap_generation
(
&
self
)
{
...
...
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