Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(turbo-tasks-backend): Clean up internal macros #71061

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions turbopack/crates/turbo-tasks-backend/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1313,14 +1313,26 @@ impl TurboTasksBackendInner {
);
task = ctx.task(task_id, TaskDataCategory::All);
}
for collectible in iter_many!(task, AggregatedCollectible { collectible } count if collectible.collectible_type == collectible_type && count > 0 => collectible.cell)
{
for collectible in iter_many!(
task,
AggregatedCollectible {
collectible
} count if collectible.collectible_type == collectible_type && count > 0 => {
collectible.cell
}
) {
*collectibles
.entry(RawVc::TaskCell(collectible.task, collectible.cell))
.or_insert(0) += 1;
}
for (collectible, count) in iter_many!(task, Collectible { collectible } count if collectible.collectible_type == collectible_type => (collectible.cell, count))
{
for (collectible, count) in iter_many!(
task,
Collectible {
collectible
} count if collectible.collectible_type == collectible_type => {
(collectible.cell, count)
}
) {
*collectibles
.entry(RawVc::TaskCell(collectible.task, collectible.cell))
.or_insert(0) += count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,14 @@ impl AggregatedDataUpdate {
if dirty_container_count > 0 {
dirty = true;
}
for collectible in iter_many!(task, AggregatedCollectible { collectible } count if count > 0 => collectible)
{
for collectible in iter_many!(
task,
AggregatedCollectible {
collectible
} count if count > 0 => {
collectible
}
) {
collectibles_update.push((collectible, 1));
}
}
Expand Down Expand Up @@ -249,7 +255,15 @@ impl AggregatedDataUpdate {
);
if added || removed {
let ty = collectible.collectible_type;
let dependent: SmallVec<[TaskId; 4]> = get_many!(task, CollectiblesDependent { collectible_type, task } if *collectible_type == ty => *task);
let dependent: SmallVec<[TaskId; 4]> = get_many!(
task,
CollectiblesDependent {
collectible_type,
task,
} if collectible_type == ty => {
task
}
);
if !dependent.is_empty() {
queue.push(AggregationUpdateJob::Invalidate {
task_ids: dependent,
Expand Down
74 changes: 29 additions & 45 deletions turbopack/crates/turbo-tasks-backend/src/backend/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,9 @@ where

macro_rules! get {
($task:ident, $key:ident $input:tt) => {
if let Some($crate::data::CachedDataItemValue::$key { value }) = $task.get(&$crate::data::CachedDataItemKey::$key $input).as_ref() {
if let Some($crate::data::CachedDataItemValue::$key {
value,
}) = $task.get(&$crate::data::CachedDataItemKey::$key $input).as_ref() {
Some(value)
} else {
None
Expand All @@ -409,7 +411,9 @@ macro_rules! get {

macro_rules! get_mut {
($task:ident, $key:ident $input:tt) => {
if let Some($crate::data::CachedDataItemValue::$key { value }) = $task.get_mut(&$crate::data::CachedDataItemKey::$key $input).as_mut() {
if let Some($crate::data::CachedDataItemValue::$key {
value,
}) = $task.get_mut(&$crate::data::CachedDataItemKey::$key $input).as_mut() {
let () = $crate::data::allow_mut_access::$key;
Some(value)
} else {
Expand All @@ -421,64 +425,42 @@ macro_rules! get_mut {
};
}

/// Creates an iterator over all [`CachedDataItemKey::$key`][crate::data::CachedDataItemKey]s in
/// `$task` matching the given `$key_pattern`, optional `$value_pattern`, and optional `if $cond`.
///
/// Each element in the iterator is determined by `$iter_item`, which may use fields extracted by
/// `$key_pattern` or `$value_pattern`.
macro_rules! iter_many {
($task:ident, $key:ident $input:tt => $value:expr) => {
$task
.iter($crate::data::indicies::$key)
.filter_map(|(key, _)| match *key {
$crate::data::CachedDataItemKey::$key $input => Some($value),
_ => None,
})
};
($task:ident, $key:ident $input:tt => $value:expr) => {
$task
.iter($crate::data::indicies::$key)
.filter_map(|(key, _)| match key {
$crate::data::CachedDataItemKey::$key $input => Some($value),
_ => None,
})
};
($task:ident, $key:ident $input:tt if $cond:expr => $value:expr) => {
($task:ident, $key:ident $key_pattern:tt $(if $cond:expr)? => $iter_item:expr) => {
$task
.iter($crate::data::indicies::$key)
.filter_map(|(key, _)| match key {
$crate::data::CachedDataItemKey::$key $input if $cond => Some($value),
&$crate::data::CachedDataItemKey::$key $key_pattern $(if $cond)? => Some(
$iter_item
),
_ => None,
})
};
($task:ident, $key:ident $input:tt $value_ident:ident => $value:expr) => {
($task:ident, $key:ident $input:tt $value_pattern:tt $(if $cond:expr)? => $iter_item:expr) => {
$task
.iter($crate::data::indicies::$key)
.filter_map(|(key, value)| match (key, value) {
(&$crate::data::CachedDataItemKey::$key $input, &$crate::data::CachedDataItemValue::$key { value: $value_ident }) => Some($value),
_ => None,
})
};
($task:ident, $key:ident $input:tt $value_ident:ident if $cond:expr => $value:expr) => {
$task
.iter($crate::data::indicies::$key)
.filter_map(|(key, value)| match (key, value) {
(&$crate::data::CachedDataItemKey::$key $input, &$crate::data::CachedDataItemValue::$key { value: $value_ident }) if $cond => Some($value),
(
&$crate::data::CachedDataItemKey::$key $input,
&$crate::data::CachedDataItemValue::$key { value: $value_pattern }
) $(if $cond)? => Some($iter_item),
_ => None,
})
};
}

/// A thin wrapper around [`iter_many`] that calls [`Iterator::collect`].
///
/// Note that the return type of [`Iterator::collect`] may be ambiguous in certain contexts, so
/// using this macro may require explicit type annotations on variables.
macro_rules! get_many {
($task:ident, $key:ident $input:tt => $value:expr) => {
$crate::backend::storage::iter_many!($task, $key $input => $value).collect()
};
($task:ident, $key:ident $input:tt => $value:expr) => {
$crate::backend::storage::iter_many!($task, $key $input => $value).collect()
};
($task:ident, $key:ident $input:tt if $cond:expr => $value:expr) => {
$crate::backend::storage::iter_many!($task, $key $input if $cond => $value).collect()
};
($task:ident, $key:ident $input:tt $value_ident:ident => $value:expr) => {
$crate::backend::storage::iter_many!($task, $key $input $value_ident => $value).collect()
};
($task:ident, $key:ident $input:tt $value_ident:ident if $cond:expr => $value:expr) => {
$crate::backend::storage::iter_many!($task, $key $input $value_ident if $cond => $value).collect()
($($args:tt)*) => {
$crate::backend::storage::iter_many!($($args)*).collect()
};
}

Expand Down Expand Up @@ -529,7 +511,9 @@ macro_rules! update_count {

macro_rules! remove {
($task:ident, $key:ident $input:tt) => {
if let Some($crate::data::CachedDataItemValue::$key { value }) = $task.remove(&$crate::data::CachedDataItemKey::$key $input) {
if let Some($crate::data::CachedDataItemValue::$key { value }) = $task.remove(
&$crate::data::CachedDataItemKey::$key $input
) {
Some(value)
} else {
None
Expand Down
Loading