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
7425ef06
Commit
7425ef06
authored
Jan 08, 2026
by
Benjamin Woodruff
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Abandoned: Turbopack: Slightly optimize `TurboTasks::pin`
parent
b933d3dc
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
36 additions
and
4 deletions
+36
-4
manager.rs
turbopack/crates/turbo-tasks/src/manager.rs
+36
-4
No files found.
turbopack/crates/turbo-tasks/src/manager.rs
View file @
7425ef06
...
@@ -2,7 +2,7 @@ use std::{
...
@@ -2,7 +2,7 @@ use std::{
fmt
::
Display
,
fmt
::
Display
,
future
::
Future
,
future
::
Future
,
hash
::
BuildHasherDefault
,
hash
::
BuildHasherDefault
,
mem
::
take
,
mem
::
{
ManuallyDrop
,
take
}
,
pin
::
Pin
,
pin
::
Pin
,
sync
::{
sync
::{
Arc
,
Mutex
,
RwLock
,
Weak
,
Arc
,
Mutex
,
RwLock
,
Weak
,
...
@@ -342,8 +342,17 @@ impl Display for ReadTracking {
...
@@ -342,8 +342,17 @@ impl Display for ReadTracking {
}
}
}
}
/// Safety: It is up to the callsite dereferencing the pointer to ensure `Send`+`Sync` for the
/// pointer. Dereferencing a pointer is already unsafe.
struct
SendSyncConstPtr
<
T
>
(
*
const
T
);
unsafe
impl
<
T
:
Send
>
Send
for
SendSyncConstPtr
<
T
>
{}
unsafe
impl
<
T
:
Sync
>
Sync
for
SendSyncConstPtr
<
T
>
{}
pub
struct
TurboTasks
<
B
:
Backend
+
'static
>
{
pub
struct
TurboTasks
<
B
:
Backend
+
'static
>
{
this
:
Weak
<
Self
>
,
this
:
Weak
<
Self
>
,
/// The value that `turbo_tasks.this.as_ptr()` would return. This is used by `TurboTasks::pin`
/// to avoid `Weak`'s danging check inside of the hot codepath.
this_ptr
:
SendSyncConstPtr
<
Self
>
,
backend
:
B
,
backend
:
B
,
task_id_factory
:
IdFactoryWithReuse
<
TaskId
>
,
task_id_factory
:
IdFactoryWithReuse
<
TaskId
>
,
transient_task_id_factory
:
IdFactoryWithReuse
<
TaskId
>
,
transient_task_id_factory
:
IdFactoryWithReuse
<
TaskId
>
,
...
@@ -473,8 +482,10 @@ impl<B: Backend + 'static> TurboTasks<B> {
...
@@ -473,8 +482,10 @@ impl<B: Backend + 'static> TurboTasks<B> {
let
transient_task_id_factory
=
let
transient_task_id_factory
=
IdFactoryWithReuse
::
new
(
TaskId
::
try_from
(
TRANSIENT_TASK_BIT
)
.unwrap
(),
TaskId
::
MAX
);
IdFactoryWithReuse
::
new
(
TaskId
::
try_from
(
TRANSIENT_TASK_BIT
)
.unwrap
(),
TaskId
::
MAX
);
let
execution_id_factory
=
IdFactory
::
new
(
ExecutionId
::
MIN
,
ExecutionId
::
MAX
);
let
execution_id_factory
=
IdFactory
::
new
(
ExecutionId
::
MIN
,
ExecutionId
::
MAX
);
let
this
=
Arc
::
new_cyclic
(|
this
|
Self
{
let
mut
this
=
Arc
::
new
(
Self
{
this
:
this
.clone
(),
// use sentinel values for this/this_ptr
this
:
Weak
::
new
(),
this_ptr
:
SendSyncConstPtr
(
Weak
::
<
Self
>
::
new
()
.as_ptr
()),
backend
,
backend
,
task_id_factory
,
task_id_factory
,
transient_task_id_factory
,
transient_task_id_factory
,
...
@@ -497,12 +508,33 @@ impl<B: Backend + 'static> TurboTasks<B> {
...
@@ -497,12 +508,33 @@ impl<B: Backend + 'static> TurboTasks<B> {
program_start
:
Instant
::
now
(),
program_start
:
Instant
::
now
(),
compilation_events
:
CompilationEventQueue
::
default
(),
compilation_events
:
CompilationEventQueue
::
default
(),
});
});
{
let
this_ptr
=
Arc
::
as_ptr
(
&
this
);
let
this_mut
=
Arc
::
get_mut
(
&
mut
this
)
.unwrap
();
unsafe
{
this_mut
.this
=
Weak
::
clone
(
&
ManuallyDrop
::
new
(
Weak
::
from_raw
(
this_ptr
)))
};
this_mut
.this_ptr
.
0
=
this_ptr
;
}
this
.backend
.startup
(
&*
this
);
this
.backend
.startup
(
&*
this
);
this
this
}
}
pub
fn
pin
(
&
self
)
->
Arc
<
Self
>
{
pub
fn
pin
(
&
self
)
->
Arc
<
Self
>
{
self
.this
.upgrade
()
.unwrap
()
// Use `Arc::from_raw()` + `clone()` because it's slightly cheaper to bump the strong count
// of an `Arc` than a `Weak`. `Weak` must do a compare-and-swap because it cannot safely
// increment the strong reference count if it was previously zero.
//
// We know that the strong reference count cannot be zero within the `body` of `pin()`
// because we know that `&self` still exists.
//
// Caveats:
// - It would not be safe to call `.pin()` within a `Drop` impl of `TurboTasks`.
// - We assume that `Arc::into_inner` or `Arc::try_unwrap` are not called on
// `Arc<TurboTasks>`.
//
// Safety: `this_ptr` is a pointer to `self`. We know it must be valid because `self` is,
// and because we assume that the `Arc` is never unwrapped.
assert
!
(
self
.this
.strong_count
()
>
0
);
unsafe
{
Arc
::
clone
(
&
ManuallyDrop
::
new
(
Arc
::
from_raw
(
self
.this_ptr
.
0
)))
}
}
}
/// Creates a new root task
/// Creates a new root task
...
...
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