Skip to content

Commit 594b0a8

Browse files
committed
apply review comments
1 parent 041751d commit 594b0a8

File tree

5 files changed

+71
-60
lines changed

5 files changed

+71
-60
lines changed

turbopack/crates/turbo-tasks-backend/src/backend/mod.rs

+37-20
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use std::{
1414
Arc,
1515
},
1616
thread::available_parallelism,
17-
time::{Duration, Instant},
1817
};
1918

2019
use anyhow::{bail, Result};
@@ -23,6 +22,7 @@ use dashmap::DashMap;
2322
use parking_lot::{Condvar, Mutex};
2423
use rustc_hash::FxHasher;
2524
use smallvec::smallvec;
25+
use tokio::time::{Duration, Instant};
2626
use turbo_tasks::{
2727
backend::{
2828
Backend, BackendJobId, CachedTaskType, CellContent, TaskExecutionSpec, TransientTaskRoot,
@@ -54,6 +54,9 @@ use crate::{
5454
utils::{bi_map::BiMap, chunked_vec::ChunkedVec, ptr_eq_arc::PtrEqArc, sharded::Sharded},
5555
};
5656

57+
const BACKEND_JOB_INITIAL_SNAPSHOT: BackendJobId = unsafe { BackendJobId::new_unchecked(1) };
58+
const BACKEND_JOB_FOLLOW_UP_SNAPSHOT: BackendJobId = unsafe { BackendJobId::new_unchecked(2) };
59+
5760
const SNAPSHOT_REQUESTED_BIT: usize = 1 << (usize::BITS - 1);
5861

5962
struct SnapshotRequest {
@@ -123,7 +126,7 @@ struct TurboTasksBackendInner {
123126
/// Condition Variable that is triggered when a snapshot is completed and
124127
/// operations can continue.
125128
snapshot_completed: Condvar,
126-
/// The timestamp of the last started snapshot.
129+
/// The timestamp of the last started snapshot since [`Self::start_time`].
127130
last_snapshot: AtomicU64,
128131

129132
stopping: AtomicBool,
@@ -291,10 +294,11 @@ impl TurboTasksBackendInner {
291294
child_task: TaskId,
292295
turbo_tasks: &dyn TurboTasksBackendApi<TurboTasksBackend>,
293296
) {
294-
operation::ConnectChildOperation::run(parent_task, child_task, unsafe {
295-
// Safety: Passing `None` is safe.
296-
self.execute_context_with_tx(None, turbo_tasks)
297-
});
297+
operation::ConnectChildOperation::run(
298+
parent_task,
299+
child_task,
300+
self.execute_context(turbo_tasks),
301+
);
298302
}
299303

300304
fn try_read_task_output(
@@ -541,13 +545,11 @@ impl TurboTasksBackendInner {
541545
snapshot_request.snapshot_requested = true;
542546
let active_operations = self
543547
.in_progress_operations
544-
.fetch_or(SNAPSHOT_REQUESTED_BIT, std::sync::atomic::Ordering::Relaxed);
548+
.fetch_or(SNAPSHOT_REQUESTED_BIT, Ordering::Relaxed);
545549
if active_operations != 0 {
546550
self.operations_suspended
547551
.wait_while(&mut snapshot_request, |_| {
548-
self.in_progress_operations
549-
.load(std::sync::atomic::Ordering::Relaxed)
550-
!= SNAPSHOT_REQUESTED_BIT
552+
self.in_progress_operations.load(Ordering::Relaxed) != SNAPSHOT_REQUESTED_BIT
551553
});
552554
}
553555
let suspended_operations = snapshot_request
@@ -562,7 +564,7 @@ impl TurboTasksBackendInner {
562564
let mut snapshot_request = self.snapshot_request.lock();
563565
snapshot_request.snapshot_requested = false;
564566
self.in_progress_operations
565-
.fetch_sub(SNAPSHOT_REQUESTED_BIT, std::sync::atomic::Ordering::Relaxed);
567+
.fetch_sub(SNAPSHOT_REQUESTED_BIT, Ordering::Relaxed);
566568
self.snapshot_completed.notify_all();
567569
let snapshot_time = Instant::now();
568570
drop(snapshot_request);
@@ -622,7 +624,7 @@ impl TurboTasksBackendInner {
622624
}
623625

624626
// Schedule the snapshot job
625-
turbo_tasks.schedule_backend_background_job(BackendJobId::from(1));
627+
turbo_tasks.schedule_backend_background_job(BACKEND_JOB_INITIAL_SNAPSHOT);
626628
}
627629

628630
fn stopping(&self) {
@@ -1157,15 +1159,15 @@ impl TurboTasksBackendInner {
11571159
turbo_tasks: &'a dyn TurboTasksBackendApi<TurboTasksBackend>,
11581160
) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
11591161
Box::pin(async move {
1160-
if *id == 1 || *id == 2 {
1162+
if id == BACKEND_JOB_INITIAL_SNAPSHOT || id == BACKEND_JOB_FOLLOW_UP_SNAPSHOT {
11611163
let last_snapshot = self.last_snapshot.load(Ordering::Relaxed);
11621164
let mut last_snapshot = self.start_time + Duration::from_millis(last_snapshot);
11631165
loop {
11641166
const FIRST_SNAPSHOT_WAIT: Duration = Duration::from_secs(30);
11651167
const SNAPSHOT_INTERVAL: Duration = Duration::from_secs(15);
11661168
const IDLE_TIMEOUT: Duration = Duration::from_secs(1);
11671169

1168-
let time = if *id == 1 {
1170+
let time = if id == BACKEND_JOB_INITIAL_SNAPSHOT {
11691171
FIRST_SNAPSHOT_WAIT
11701172
} else {
11711173
SNAPSHOT_INTERVAL
@@ -1177,7 +1179,11 @@ impl TurboTasksBackendInner {
11771179
if !self.stopping.load(Ordering::Acquire) {
11781180
let mut idle_start_listener = self.idle_start_event.listen();
11791181
let mut idle_end_listener = self.idle_end_event.listen();
1180-
let mut idle_time = until + IDLE_TIMEOUT;
1182+
let mut idle_time = if turbo_tasks.is_idle() {
1183+
Instant::now() + IDLE_TIMEOUT
1184+
} else {
1185+
far_future()
1186+
};
11811187
loop {
11821188
tokio::select! {
11831189
_ = &mut stop_listener => {
@@ -1191,10 +1197,10 @@ impl TurboTasksBackendInner {
11911197
idle_time = until + IDLE_TIMEOUT;
11921198
idle_end_listener = self.idle_end_event.listen()
11931199
},
1194-
_ = tokio::time::sleep_until(until.into()) => {
1200+
_ = tokio::time::sleep_until(until) => {
11951201
break;
11961202
},
1197-
_ = tokio::time::sleep_until(idle_time.into()) => {
1203+
_ = tokio::time::sleep_until(idle_time) => {
11981204
if turbo_tasks.is_idle() {
11991205
break;
12001206
}
@@ -1212,10 +1218,12 @@ impl TurboTasksBackendInner {
12121218
continue;
12131219
}
12141220
let last_snapshot = last_snapshot.duration_since(self.start_time);
1215-
self.last_snapshot
1216-
.store(last_snapshot.as_millis() as u64, Ordering::Relaxed);
1221+
self.last_snapshot.store(
1222+
last_snapshot.as_millis().try_into().unwrap(),
1223+
Ordering::Relaxed,
1224+
);
12171225

1218-
turbo_tasks.schedule_backend_background_job(BackendJobId::from(2));
1226+
turbo_tasks.schedule_backend_background_job(BACKEND_JOB_FOLLOW_UP_SNAPSHOT);
12191227
return;
12201228
}
12211229
}
@@ -1525,3 +1533,12 @@ impl Backend for TurboTasksBackend {
15251533
todo!()
15261534
}
15271535
}
1536+
1537+
// from https://github.com/tokio-rs/tokio/blob/29cd6ec1ec6f90a7ee1ad641c03e0e00badbcb0e/tokio/src/time/instant.rs#L57-L63
1538+
fn far_future() -> Instant {
1539+
// Roughly 30 years from now.
1540+
// API does not provide a way to obtain max `Instant`
1541+
// or convert specific date in the future to instant.
1542+
// 1000 years overflows on macOS, 100 years overflows on FreeBSD.
1543+
Instant::now() + Duration::from_secs(86400 * 365 * 30)
1544+
}

turbopack/crates/turbo-tasks-backend/src/data.rs

+23-33
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@ use turbo_tasks::{
88

99
use crate::backend::{indexed::Indexed, TaskDataCategory};
1010

11+
// this traits are needed for the transient variants of `CachedDataItem`
12+
// transient variants are never cloned or compared
13+
macro_rules! transient_traits {
14+
($name:ident) => {
15+
impl Clone for $name {
16+
fn clone(&self) -> Self {
17+
// this impl is needed for the transient variants of `CachedDataItem`
18+
// transient variants are never cloned
19+
panic!(concat!(stringify!($name), " cannot be cloned"));
20+
}
21+
}
22+
23+
impl PartialEq for $name {
24+
fn eq(&self, _other: &Self) -> bool {
25+
panic!(concat!(stringify!($name), " cannot be compared"));
26+
}
27+
}
28+
};
29+
}
30+
1131
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
1232
pub struct CellRef {
1333
pub task: TaskId,
@@ -53,17 +73,7 @@ impl RootState {
5373
}
5474
}
5575

56-
impl Clone for RootState {
57-
fn clone(&self) -> Self {
58-
panic!("RootState cannot be cloned");
59-
}
60-
}
61-
62-
impl PartialEq for RootState {
63-
fn eq(&self, _other: &Self) -> bool {
64-
panic!("RootState cannot be compared");
65-
}
66-
}
76+
transient_traits!(RootState);
6777

6878
impl Eq for RootState {}
6979

@@ -90,17 +100,7 @@ pub enum InProgressState {
90100
},
91101
}
92102

93-
impl Clone for InProgressState {
94-
fn clone(&self) -> Self {
95-
panic!("InProgressState cannot be cloned");
96-
}
97-
}
98-
99-
impl PartialEq for InProgressState {
100-
fn eq(&self, _other: &Self) -> bool {
101-
panic!("InProgressState cannot be compared");
102-
}
103-
}
103+
transient_traits!(InProgressState);
104104

105105
impl Eq for InProgressState {}
106106

@@ -109,17 +109,7 @@ pub struct InProgressCellState {
109109
pub event: Event,
110110
}
111111

112-
impl Clone for InProgressCellState {
113-
fn clone(&self) -> Self {
114-
panic!("InProgressCell cannot be cloned");
115-
}
116-
}
117-
118-
impl PartialEq for InProgressCellState {
119-
fn eq(&self, _other: &Self) -> bool {
120-
panic!("InProgressCell cannot be compared");
121-
}
122-
}
112+
transient_traits!(InProgressCellState);
123113

124114
impl Eq for InProgressCellState {}
125115

turbopack/crates/turbo-tasks/src/id.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ macro_rules! define_id {
3030
/// # Safety
3131
///
3232
/// The passed `id` must not be zero.
33-
pub unsafe fn new_unchecked(id: $primitive) -> Self {
33+
pub const unsafe fn new_unchecked(id: $primitive) -> Self {
3434
Self { id: unsafe { NonZero::<$primitive>::new_unchecked(id) } }
3535
}
3636
}

turbopack/crates/turbo-tasks/src/key_value_pair.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use std::fmt::Debug;
2+
13
pub trait KeyValuePair {
2-
type Key: PartialEq + Eq + std::hash::Hash;
3-
type Value;
4+
type Key: Debug + Clone + PartialEq + Eq + std::hash::Hash;
5+
type Value: Debug + Clone + Default + PartialEq + Eq;
46
fn key(&self) -> Self::Key;
57
fn value(&self) -> Self::Value;
68
fn from_key_and_value(key: Self::Key, value: Self::Value) -> Self;

turbopack/crates/turbo-tasks/src/scope.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::sync::Arc;
22

33
use crate::{turbo_tasks, turbo_tasks_scope, TurboTasksApi};
44

5+
/// A wrapper around [`rayon::Scope`] that preserves the [`turbo_tasks_scope`].
56
pub struct Scope<'scope, 'a> {
67
scope: &'a rayon::Scope<'scope>,
78
handle: tokio::runtime::Handle,
@@ -10,9 +11,9 @@ pub struct Scope<'scope, 'a> {
1011
}
1112

1213
impl<'scope, 'a> Scope<'scope, 'a> {
13-
pub fn spawn<BODY>(&self, body: BODY)
14+
pub fn spawn<Body>(&self, body: Body)
1415
where
15-
BODY: FnOnce(&Scope<'scope, '_>) + Send + 'scope,
16+
Body: FnOnce(&Scope<'scope, '_>) + Send + 'scope,
1617
{
1718
let span = self.span.clone();
1819
let handle = self.handle.clone();
@@ -32,9 +33,10 @@ impl<'scope, 'a> Scope<'scope, 'a> {
3233
}
3334
}
3435

35-
pub fn scope<'scope, OP, R>(op: OP) -> R
36+
/// A wrapper around [`rayon::in_place_scope`] that preserves the [`turbo_tasks_scope`].
37+
pub fn scope<'scope, Op, R>(op: Op) -> R
3638
where
37-
OP: FnOnce(&Scope<'scope, '_>) -> R,
39+
Op: FnOnce(&Scope<'scope, '_>) -> R,
3840
{
3941
let span = tracing::Span::current();
4042
let handle = tokio::runtime::Handle::current();

0 commit comments

Comments
 (0)