From 2e33750388f9803a99e042d1ce9ea49a8015449e Mon Sep 17 00:00:00 2001 From: Benjamin Woodruff Date: Wed, 9 Oct 2024 16:30:49 -0700 Subject: [PATCH] chore(turbo-tasks-backend): Clean up internal macros --- .../turbo-tasks-backend/src/backend/mod.rs | 20 ++++- .../backend/operation/aggregation_update.rs | 20 ++++- .../src/backend/storage.rs | 74 ++++++++----------- 3 files changed, 62 insertions(+), 52 deletions(-) diff --git a/turbopack/crates/turbo-tasks-backend/src/backend/mod.rs b/turbopack/crates/turbo-tasks-backend/src/backend/mod.rs index b7f6199ca02994..2d3b594ba1fa36 100644 --- a/turbopack/crates/turbo-tasks-backend/src/backend/mod.rs +++ b/turbopack/crates/turbo-tasks-backend/src/backend/mod.rs @@ -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; diff --git a/turbopack/crates/turbo-tasks-backend/src/backend/operation/aggregation_update.rs b/turbopack/crates/turbo-tasks-backend/src/backend/operation/aggregation_update.rs index a6b2b05664bdbe..5e1a6133de45ed 100644 --- a/turbopack/crates/turbo-tasks-backend/src/backend/operation/aggregation_update.rs +++ b/turbopack/crates/turbo-tasks-backend/src/backend/operation/aggregation_update.rs @@ -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)); } } @@ -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, diff --git a/turbopack/crates/turbo-tasks-backend/src/backend/storage.rs b/turbopack/crates/turbo-tasks-backend/src/backend/storage.rs index bf7aa5360663fe..64025dad055247 100644 --- a/turbopack/crates/turbo-tasks-backend/src/backend/storage.rs +++ b/turbopack/crates/turbo-tasks-backend/src/backend/storage.rs @@ -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 @@ -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 { @@ -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() }; } @@ -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