Skip to content

Commit

Permalink
Hide some turbo_tasks internals (vercel/turborepo#5584)
Browse files Browse the repository at this point in the history
### Description

This hides _some_ turbo_tasks internals from its public API.

It's currently not possible to completely hide them (hence
`Vc::into_raw`/unsafe `Vc::<AutoSet<RawVc>>::from_task_id`) because
turbo-tasks-memory still depends on some internal types, but this is a
step in the right direction.
  • Loading branch information
alexkirsz authored Aug 7, 2023
1 parent 493da58 commit e438d27
Show file tree
Hide file tree
Showing 19 changed files with 109 additions and 106 deletions.
4 changes: 2 additions & 2 deletions crates/node-file-trace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ async fn run<B: Backend + 'static, F: Future<Output = ()>>(
resolve_options,
);

let source = TransientValue::new(output.node);
let source = TransientValue::new(Vc::into_raw(output));
let issues = output
.peek_issues_with_path()
.await?
Expand All @@ -518,7 +518,7 @@ async fn run<B: Backend + 'static, F: Future<Output = ()>>(
sender.send(output_iter.collect::<Vec<String>>()).await?;
drop(sender);
}
Ok(unit().node)
Ok(unit())
})
});
finish(tt, task).await?;
Expand Down
2 changes: 1 addition & 1 deletion crates/turbo-tasks-fs/examples/hash_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async fn main() -> Result<()> {
let input = fs.root().join("demo".to_string());
let dir_hash = hash_directory(input);
print_hash(dir_hash).await?;
Ok(unit().node)
Ok(unit())
})
});
tt.wait_task_completion(task, true).await.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/turbo-tasks-fs/examples/hash_glob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn main() -> Result<()> {
let glob_result = input.read_glob(glob, true);
let dir_hash = hash_glob_result(glob_result);
print_hash(dir_hash).await?;
Ok(unit().node)
Ok(unit())
})
});
tt.wait_task_completion(task, true).await.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/turbo-tasks-memory/benches/scope_stress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn scope_stress(c: &mut Criterion) {
async move {
let task = tt.spawn_once_task(async move {
rectangle(a, b).strongly_consistent().await?;
Ok(unit().node)
Ok(unit())
});
tt.wait_task_completion(task, false).await
}
Expand Down
2 changes: 1 addition & 1 deletion crates/turbo-tasks-memory/benches/stress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub fn fibonacci(c: &mut Criterion) {
// size >= 1 => + fib(0) = 1
// size >= 2 => + fib(1) = 2
(0..size).map(|i| fib(i, i)).try_join().await?;
Ok(unit().node)
Ok(unit())
});
tt.wait_task_completion(task, false).await.unwrap();
tt
Expand Down
6 changes: 4 additions & 2 deletions crates/turbo-tasks-memory/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2269,7 +2269,7 @@ impl Task {
read_task_id,
&*turbo_tasks,
);
RawVc::TaskOutput(task).into_read::<AutoSet<RawVc>>()
unsafe { <Vc<AutoSet<RawVc>>>::from_task_id(task) }
})
})
})
Expand All @@ -2280,7 +2280,9 @@ impl Task {
current.add(*v);
}
}
Ok(Vc::<AutoSet<_>>::cell(current.iter().copied().collect()).node)
Ok(Vc::into_raw(Vc::<AutoSet<RawVc>>::cell(
current.iter().copied().collect(),
)))
}

pub(crate) fn read_task_collectibles(
Expand Down
1 change: 1 addition & 0 deletions crates/turbo-tasks/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ mod once_map;
pub mod persisted_graph;
pub mod primitives;
mod raw_vc;
mod raw_vc_set;
mod read_ref;
pub mod registry;
pub mod small_duration;
Expand Down
45 changes: 24 additions & 21 deletions crates/turbo-tasks/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,16 +339,18 @@ impl<B: Backend + 'static> TurboTasks<B> {
}

/// Creates a new root task
pub fn spawn_root_task(
&self,
functor: impl Fn() -> Pin<Box<dyn Future<Output = Result<RawVc>> + Send>>
+ Sync
+ Send
+ 'static,
) -> TaskId {
let id = self
.backend
.create_transient_task(TransientTaskType::Root(Box::new(functor)), self);
pub fn spawn_root_task<T, F, Fut>(&self, functor: F) -> TaskId
where
F: Fn() -> Fut + Send + Sync + Clone + 'static,
Fut: Future<Output = Result<Vc<T>>> + Send,
{
let id = self.backend.create_transient_task(
TransientTaskType::Root(Box::new(move || {
let functor = functor.clone();
Box::pin(async move { Ok(functor().await?.node) })
})),
self,
);
self.schedule(id);
id
}
Expand All @@ -357,13 +359,14 @@ impl<B: Backend + 'static> TurboTasks<B> {
/// Creates a new root task, that is only executed once.
/// Dependencies will not invalidate the task.
#[track_caller]
pub fn spawn_once_task(
&self,
future: impl Future<Output = Result<RawVc>> + Send + 'static,
) -> TaskId {
let id = self
.backend
.create_transient_task(TransientTaskType::Once(Box::pin(future)), self);
pub fn spawn_once_task<T, Fut>(&self, future: Fut) -> TaskId
where
Fut: Future<Output = Result<Vc<T>>> + Send + 'static,
{
let id = self.backend.create_transient_task(
TransientTaskType::Once(Box::pin(async move { Ok(future.await?.node) })),
self,
);
self.schedule(id);
id
}
Expand All @@ -377,7 +380,7 @@ impl<B: Backend + 'static> TurboTasks<B> {
let result = future.await?;
tx.send(result)
.map_err(|_| anyhow!("unable to send result"))?;
Ok(Completion::new().node)
Ok(Completion::new())
});
// INVALIDATION: A Once task will never invalidate, therefore we don't need to
// track a dependency
Expand Down Expand Up @@ -837,7 +840,7 @@ impl<B: Backend + 'static> TurboTasksCallApi for TurboTasks<B> {
) -> TaskId {
self.spawn_once_task(async move {
future.await?;
Ok(Completion::new().node)
Ok(Completion::new())
})
}

Expand All @@ -853,7 +856,7 @@ impl<B: Backend + 'static> TurboTasksCallApi for TurboTasks<B> {
}
self.spawn_once_task(async move {
future.await?;
Ok(Completion::new().node)
Ok(Completion::new())
})
}

Expand All @@ -867,7 +870,7 @@ impl<B: Backend + 'static> TurboTasksCallApi for TurboTasks<B> {
this.finish_primary_job();
future.await?;
this.begin_primary_job();
Ok(Completion::new().node)
Ok(Completion::new())
})
}
}
Expand Down
4 changes: 1 addition & 3 deletions crates/turbo-tasks/src/primitives.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::{future::IntoFuture, ops::Deref};

use anyhow::Result;
use auto_hash_map::AutoSet;
use futures::TryFutureExt;
// This specific macro identifier is detected by turbo-tasks-build.
use turbo_tasks_macros::primitive as __turbo_tasks_internal_primitive;

use crate::{
RawVc, TryJoinIterExt, Vc, {self as turbo_tasks},
TryJoinIterExt, Vc, {self as turbo_tasks},
};

__turbo_tasks_internal_primitive!(());
Expand Down Expand Up @@ -72,7 +71,6 @@ __turbo_tasks_internal_primitive!(i64);
__turbo_tasks_internal_primitive!(i128);
__turbo_tasks_internal_primitive!(usize);
__turbo_tasks_internal_primitive!(isize);
__turbo_tasks_internal_primitive!(AutoSet<RawVc>);
__turbo_tasks_internal_primitive!(serde_json::Value);
__turbo_tasks_internal_primitive!(Vec<u8>);

Expand Down
63 changes: 13 additions & 50 deletions crates/turbo-tasks/src/raw_vc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,39 +63,31 @@ pub enum RawVc {
}

impl RawVc {
pub fn into_read<T: VcValueType>(self) -> ReadRawVcFuture<T, VcValueTypeCast<T>> {
pub(crate) fn into_read<T: VcValueType>(self) -> ReadRawVcFuture<T, VcValueTypeCast<T>> {
// returns a custom future to have something concrete and sized
// this avoids boxing in IntoFuture
ReadRawVcFuture::new(self)
}

pub fn into_strongly_consistent_read<T: VcValueType>(
pub(crate) fn into_strongly_consistent_read<T: VcValueType>(
self,
) -> ReadRawVcFuture<T, VcValueTypeCast<T>> {
// returns a custom future to have something concrete and sized
// this avoids boxing in IntoFuture
ReadRawVcFuture::new_strongly_consistent(self)
}

pub fn into_trait_read<T: VcValueTrait + ?Sized>(
pub(crate) fn into_trait_read<T: VcValueTrait + ?Sized>(
self,
) -> ReadRawVcFuture<T, VcValueTraitCast<T>> {
// returns a custom future to have something concrete and sized
// this avoids boxing in IntoFuture
ReadRawVcFuture::new(self)
}

pub fn into_strongly_consistent_trait_read<T: VcValueTrait + Sized>(
self,
) -> ReadRawVcFuture<T, VcValueTraitCast<T>> {
// returns a custom future to have something concrete and sized
// this avoids boxing in IntoFuture
ReadRawVcFuture::new_strongly_consistent(self)
}

/// INVALIDATION: Be careful with this, it will not track dependencies, so
/// using it could break cache invalidation.
pub fn into_read_untracked_with_turbo_tasks<T: Any + VcValueType>(
pub(crate) fn into_read_untracked_with_turbo_tasks<T: Any + VcValueType>(
self,
turbo_tasks: &dyn TurboTasksApi,
) -> ReadRawVcFuture<T, VcValueTypeCast<T>> {
Expand All @@ -104,21 +96,13 @@ impl RawVc {

/// INVALIDATION: Be careful with this, it will not track dependencies, so
/// using it could break cache invalidation.
pub fn into_trait_read_untracked<T: VcValueTrait + ?Sized>(
pub(crate) fn into_trait_read_untracked<T: VcValueTrait + ?Sized>(
self,
) -> ReadRawVcFuture<T, VcValueTraitCast<T>> {
ReadRawVcFuture::new_untracked(self)
}

/// INVALIDATION: Be careful with this, it will not track dependencies, so
/// using it could break cache invalidation.
pub fn into_strongly_consistent_read_untracked<T: Any + VcValueType>(
self,
) -> ReadRawVcFuture<T, VcValueTypeCast<T>> {
ReadRawVcFuture::new_strongly_consistent_untracked(self)
}

pub async fn resolve_trait(
pub(crate) async fn resolve_trait(
self,
trait_type: TraitTypeId,
) -> Result<Option<RawVc>, ResolveTypeError> {
Expand Down Expand Up @@ -154,7 +138,7 @@ impl RawVc {
}
}

pub async fn resolve_value(
pub(crate) async fn resolve_value(
self,
value_type: ValueTypeId,
) -> Result<Option<RawVc>, ResolveTypeError> {
Expand Down Expand Up @@ -191,7 +175,7 @@ impl RawVc {
}

/// See [`crate::Vc::resolve`].
pub async fn resolve(self) -> Result<RawVc> {
pub(crate) async fn resolve(self) -> Result<RawVc> {
let tt = turbo_tasks();
let mut current = self;
let mut notified = false;
Expand All @@ -208,8 +192,9 @@ impl RawVc {
}
}
}

/// See [`crate::Vc::resolve_strongly_consistent`].
pub async fn resolve_strongly_consistent(self) -> Result<RawVc> {
pub(crate) async fn resolve_strongly_consistent(self) -> Result<RawVc> {
let tt = turbo_tasks();
let mut current = self;
let mut notified = false;
Expand All @@ -227,18 +212,11 @@ impl RawVc {
}
}

pub fn connect(&self) {
pub(crate) fn connect(&self) {
let tt = turbo_tasks();
tt.connect_task(self.get_task_id());
}

pub fn is_resolved(&self) -> bool {
match self {
RawVc::TaskOutput(_) => false,
RawVc::TaskCell(_, _) => true,
}
}

pub fn get_task_id(&self) -> TaskId {
match self {
RawVc::TaskOutput(t) | RawVc::TaskCell(t, _) => *t,
Expand All @@ -250,8 +228,7 @@ impl CollectiblesSource for RawVc {
fn peek_collectibles<T: VcValueTrait>(self) -> CollectiblesFuture<T> {
let tt = turbo_tasks();
tt.notify_scheduled_tasks();
let set: Vc<AutoSet<RawVc>> =
tt.read_task_collectibles(self.get_task_id(), T::get_trait_type_id());
let set = tt.read_task_collectibles(self.get_task_id(), T::get_trait_type_id());
CollectiblesFuture {
turbo_tasks: tt,
inner: set.into_future(),
Expand All @@ -263,8 +240,7 @@ impl CollectiblesSource for RawVc {
fn take_collectibles<T: VcValueTrait>(self) -> CollectiblesFuture<T> {
let tt = turbo_tasks();
tt.notify_scheduled_tasks();
let set: Vc<AutoSet<RawVc>> =
tt.read_task_collectibles(self.get_task_id(), T::get_trait_type_id());
let set = tt.read_task_collectibles(self.get_task_id(), T::get_trait_type_id());
CollectiblesFuture {
turbo_tasks: tt,
inner: set.into_future(),
Expand Down Expand Up @@ -349,19 +325,6 @@ impl<T: ?Sized, Cast: VcCast> ReadRawVcFuture<T, Cast> {
_cast: PhantomData,
}
}

fn new_strongly_consistent_untracked(vc: RawVc) -> Self {
let tt = turbo_tasks();
ReadRawVcFuture {
turbo_tasks: tt,
strongly_consistent: true,
current: vc,
untracked: true,
listener: None,
phantom_data: PhantomData,
_cast: PhantomData,
}
}
}

impl<T: ?Sized, Cast: VcCast> Future for ReadRawVcFuture<T, Cast> {
Expand Down
24 changes: 24 additions & 0 deletions crates/turbo-tasks/src/raw_vc_set.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use std::marker::PhantomData;

use auto_hash_map::AutoSet;
// This specific macro identifier is detected by turbo-tasks-build.
use turbo_tasks_macros::primitive as __turbo_tasks_internal_primitive;

use crate as turbo_tasks;
use crate::{RawVc, TaskId, Vc};

__turbo_tasks_internal_primitive!(AutoSet<RawVc>);

impl Vc<AutoSet<RawVc>> {
/// Casts a `TaskId` to a `Vc<AutoSet<RawVc>>`.
///
/// # Safety
///
/// The `TaskId` must be point to a valid `AutoSet<RawVc>`.
pub unsafe fn from_task_id(task_id: TaskId) -> Self {
Vc {
node: RawVc::TaskOutput(task_id),
_t: PhantomData,
}
}
}
Loading

0 comments on commit e438d27

Please sign in to comment.