Skip to content

Commit

Permalink
Add u64 ExecutionId type (vercel/turborepo#8771)
Browse files Browse the repository at this point in the history
### Description

The plan for task-local Vcs is to extend the `RawVc` type into:

```
enum RawVc {
    TaskOutput(TaskId),
    TaskCell(TaskId, CellId),
    LocalCell(ExecutionId, LocalCellId),  // new variant!
}
```

Where `ExecutionId` is a globally unique value representing the current task execution. That will be used with runtime safety checks to ensure that a given `LocalCellId` (which is an index into a task-local arena/vec) is valid upon read.

https://www.notion.so/vercel/Resolved-Vcs-Vc-Lifetimes-Local-Vcs-and-Vc-Refcounts-49d666d3f9594017b5b312b87ddc5bff?pvs=4

This PR extends the `define_id` macro to support arbitrary integer types, and uses that to define `ExecutionId`.

### Testing Instructions

```
cargo nextest r -p turbo-tasks -p turbo-tasks-memory
```
  • Loading branch information
bgw committed Jul 17, 2024
1 parent 584e211 commit feac4db
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
2 changes: 1 addition & 1 deletion crates/turbo-tasks/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use auto_hash_map::AutoMap;
use serde::{Deserialize, Serialize};
use tracing::Span;

pub use crate::id::BackendJobId;
pub use crate::id::{BackendJobId, ExecutionId};
use crate::{
event::EventListener, manager::TurboTasksBackendApi, raw_vc::CellId, registry,
ConcreteTaskInput, FunctionId, RawVc, ReadRef, SharedReference, TaskId, TaskIdProvider,
Expand Down
42 changes: 20 additions & 22 deletions crates/turbo-tasks/src/id.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
fmt::{Debug, Display},
mem::transmute_copy,
num::{NonZeroU32, NonZeroU64, TryFromIntError},
num::{NonZero, NonZeroU64, TryFromIntError},
ops::Deref,
};

Expand All @@ -10,10 +10,10 @@ use serde::{de::Visitor, Deserialize, Serialize};
use crate::registry;

macro_rules! define_id {
(internal $name:ident $(,$derive:ty)*) => {
#[derive(Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord $(,$derive)*)]
($name:ident : $primitive:ty $(,derive($($derive:ty),*))?) => {
#[derive(Hash, Clone, Copy, PartialEq, Eq, PartialOrd, Ord $($(,$derive)*)? )]
pub struct $name {
id: NonZeroU32,
id: NonZero<$primitive>,
}

impl $name {
Expand All @@ -22,8 +22,8 @@ macro_rules! define_id {
/// # Safety
///
/// The passed `id` must not be zero.
pub unsafe fn new_unchecked(id: u32) -> Self {
Self { id: unsafe { NonZeroU32::new_unchecked(id) } }
pub unsafe fn new_unchecked(id: $primitive) -> Self {
Self { id: unsafe { NonZero::<$primitive>::new_unchecked(id) } }
}
}

Expand All @@ -34,7 +34,7 @@ macro_rules! define_id {
}

impl Deref for $name {
type Target = u32;
type Target = $primitive;

fn deref(&self) -> &Self::Target {
unsafe { transmute_copy(&&self.id) }
Expand All @@ -44,9 +44,12 @@ macro_rules! define_id {
/// Converts a numeric identifier to the wrapper type.
///
/// Panics if the given id value is zero.
impl From<u32> for $name {
fn from(id: u32) -> Self {
Self { id: NonZeroU32::new(id).expect("Ids can only be created from non zero values") }
impl From<$primitive> for $name {
fn from(id: $primitive) -> Self {
Self {
id: NonZero::<$primitive>::new(id)
.expect("Ids can only be created from non zero values")
}
}
}

Expand All @@ -55,23 +58,18 @@ macro_rules! define_id {
type Error = TryFromIntError;

fn try_from(id: NonZeroU64) -> Result<Self, Self::Error> {
Ok(Self { id: NonZeroU32::try_from(id)? })
Ok(Self { id: NonZero::try_from(id)? })
}
}
};
($name:ident) => {
define_id!(internal $name);
};
($name:ident, derive($($derive:ty),*)) => {
define_id!(internal $name $(,$derive)*);
};
}

define_id!(TaskId);
define_id!(FunctionId);
define_id!(ValueTypeId);
define_id!(TraitTypeId);
define_id!(BackendJobId);
define_id!(TaskId: u32);
define_id!(FunctionId: u32);
define_id!(ValueTypeId: u32);
define_id!(TraitTypeId: u32);
define_id!(BackendJobId: u32);
define_id!(ExecutionId: u64, derive(Debug));

impl Debug for TaskId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Expand Down

0 comments on commit feac4db

Please sign in to comment.