Skip to content

Commit

Permalink
Remove use of boxing for read/write
Browse files Browse the repository at this point in the history
  • Loading branch information
bgw committed Sep 4, 2024
1 parent 7533830 commit 12a7521
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions turbopack/crates/turbo-tasks/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,12 +241,12 @@ pub trait TurboTasksBackendApi<B: Backend + 'static>: TurboTasksCallApi + Sync +
/// An untyped object-safe version of [`TurboTasksBackendApiExt::read_task_state`]. Callers
/// should prefer the extension trait's version of this method.
#[allow(clippy::type_complexity)] // Moving this to a typedef would make the docs confusing
fn read_task_state_boxed(&self, func: Box<dyn FnOnce(&B::TaskState) + '_>);
fn read_task_state_dyn(&self, func: &mut dyn FnMut(&B::TaskState));

/// An untyped object-safe version of [`TurboTasksBackendApiExt::write_task_state`]. Callers
/// should prefer the extension trait's version of this method.
#[allow(clippy::type_complexity)]
fn write_task_state_boxed(&self, func: Box<dyn FnOnce(&mut B::TaskState) + '_>);
fn write_task_state_dyn(&self, func: &mut dyn FnMut(&mut B::TaskState));

/// Returns a reference to the backend.
fn backend(&self) -> &B;
Expand All @@ -260,19 +260,21 @@ pub trait TurboTasksBackendApiExt<B: Backend + 'static>: TurboTasksBackendApi<B>
/// This function holds open a non-exclusive read lock that blocks writes, so `func` is expected
/// to execute quickly in order to release the lock.
fn read_task_state<T>(&self, func: impl FnOnce(&B::TaskState) -> T) -> T {
let mut func = Some(func);
let mut out = None;
self.read_task_state_boxed(Box::new(|ts| out = Some(func(ts))));
out.expect("write_task_state_boxed must call `func`")
self.read_task_state_dyn(&mut |ts| out = Some((func.take().unwrap())(ts)));
out.expect("read_task_state_dyn must call `func`")
}

/// Allows modification of the [`Backend::TaskState`].
///
/// This function holds open a write lock, so `func` is expected to execute quickly in order to
/// release the lock.
fn write_task_state<T>(&self, func: impl FnOnce(&mut B::TaskState) -> T) -> T {
let mut func = Some(func);
let mut out = None;
self.write_task_state_boxed(Box::new(|ts| out = Some(func(ts))));
out.expect("write_task_state_boxed must call `func`")
self.write_task_state_dyn(&mut |ts| out = Some((func.take().unwrap())(ts)));
out.expect("write_task_state_dyn must call `func`")
}
}

Expand Down Expand Up @@ -1492,12 +1494,12 @@ impl<B: Backend + 'static> TurboTasksBackendApi<B> for TurboTasks<B> {
unsafe { self.transient_task_id_factory.reuse(id.into()) }
}

fn read_task_state_boxed(&self, func: Box<dyn FnOnce(&B::TaskState) + '_>) {
fn read_task_state_dyn(&self, func: &mut dyn FnMut(&B::TaskState)) {
CURRENT_GLOBAL_TASK_STATE
.with(move |ts| func(ts.read().unwrap().backend_state.downcast_ref().unwrap()))
}

fn write_task_state_boxed(&self, func: Box<dyn FnOnce(&mut B::TaskState) + '_>) {
fn write_task_state_dyn(&self, func: &mut dyn FnMut(&mut B::TaskState)) {
CURRENT_GLOBAL_TASK_STATE
.with(move |ts| func(ts.write().unwrap().backend_state.downcast_mut().unwrap()))
}
Expand Down

0 comments on commit 12a7521

Please sign in to comment.