Skip to content

Commit ff5006b

Browse files
bgwsokra
authored andcommitted
chore(turbo-tasks-backend): Clean up internal macros
1 parent f5d6b26 commit ff5006b

File tree

3 files changed

+62
-52
lines changed

3 files changed

+62
-52
lines changed

turbopack/crates/turbo-tasks-backend/src/backend/mod.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -1313,14 +1313,26 @@ impl TurboTasksBackendInner {
13131313
);
13141314
task = ctx.task(task_id, TaskDataCategory::All);
13151315
}
1316-
for collectible in iter_many!(task, AggregatedCollectible { collectible } count if collectible.collectible_type == collectible_type && count > 0 => collectible.cell)
1317-
{
1316+
for collectible in iter_many!(
1317+
task,
1318+
AggregatedCollectible {
1319+
collectible
1320+
} count if collectible.collectible_type == collectible_type && count > 0 => {
1321+
collectible.cell
1322+
}
1323+
) {
13181324
*collectibles
13191325
.entry(RawVc::TaskCell(collectible.task, collectible.cell))
13201326
.or_insert(0) += 1;
13211327
}
1322-
for (collectible, count) in iter_many!(task, Collectible { collectible } count if collectible.collectible_type == collectible_type => (collectible.cell, count))
1323-
{
1328+
for (collectible, count) in iter_many!(
1329+
task,
1330+
Collectible {
1331+
collectible
1332+
} count if collectible.collectible_type == collectible_type => {
1333+
(collectible.cell, count)
1334+
}
1335+
) {
13241336
*collectibles
13251337
.entry(RawVc::TaskCell(collectible.task, collectible.cell))
13261338
.or_insert(0) += count;

turbopack/crates/turbo-tasks-backend/src/backend/operation/aggregation_update.rs

+17-3
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,14 @@ impl AggregatedDataUpdate {
133133
if dirty_container_count > 0 {
134134
dirty = true;
135135
}
136-
for collectible in iter_many!(task, AggregatedCollectible { collectible } count if count > 0 => collectible)
137-
{
136+
for collectible in iter_many!(
137+
task,
138+
AggregatedCollectible {
139+
collectible
140+
} count if count > 0 => {
141+
collectible
142+
}
143+
) {
138144
collectibles_update.push((collectible, 1));
139145
}
140146
}
@@ -249,7 +255,15 @@ impl AggregatedDataUpdate {
249255
);
250256
if added || removed {
251257
let ty = collectible.collectible_type;
252-
let dependent: SmallVec<[TaskId; 4]> = get_many!(task, CollectiblesDependent { collectible_type, task } if *collectible_type == ty => *task);
258+
let dependent: SmallVec<[TaskId; 4]> = get_many!(
259+
task,
260+
CollectiblesDependent {
261+
collectible_type,
262+
task,
263+
} if collectible_type == ty => {
264+
task
265+
}
266+
);
253267
if !dependent.is_empty() {
254268
queue.push(AggregationUpdateJob::Invalidate {
255269
task_ids: dependent,

turbopack/crates/turbo-tasks-backend/src/backend/storage.rs

+29-45
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,9 @@ where
396396

397397
macro_rules! get {
398398
($task:ident, $key:ident $input:tt) => {
399-
if let Some($crate::data::CachedDataItemValue::$key { value }) = $task.get(&$crate::data::CachedDataItemKey::$key $input).as_ref() {
399+
if let Some($crate::data::CachedDataItemValue::$key {
400+
value,
401+
}) = $task.get(&$crate::data::CachedDataItemKey::$key $input).as_ref() {
400402
Some(value)
401403
} else {
402404
None
@@ -409,7 +411,9 @@ macro_rules! get {
409411

410412
macro_rules! get_mut {
411413
($task:ident, $key:ident $input:tt) => {
412-
if let Some($crate::data::CachedDataItemValue::$key { value }) = $task.get_mut(&$crate::data::CachedDataItemKey::$key $input).as_mut() {
414+
if let Some($crate::data::CachedDataItemValue::$key {
415+
value,
416+
}) = $task.get_mut(&$crate::data::CachedDataItemKey::$key $input).as_mut() {
413417
let () = $crate::data::allow_mut_access::$key;
414418
Some(value)
415419
} else {
@@ -421,64 +425,42 @@ macro_rules! get_mut {
421425
};
422426
}
423427

428+
/// Creates an iterator over all [`CachedDataItemKey::$key`][crate::data::CachedDataItemKey]s in
429+
/// `$task` matching the given `$key_pattern`, optional `$value_pattern`, and optional `if $cond`.
430+
///
431+
/// Each element in the iterator is determined by `$iter_item`, which may use fields extracted by
432+
/// `$key_pattern` or `$value_pattern`.
424433
macro_rules! iter_many {
425-
($task:ident, $key:ident $input:tt => $value:expr) => {
426-
$task
427-
.iter($crate::data::indicies::$key)
428-
.filter_map(|(key, _)| match *key {
429-
$crate::data::CachedDataItemKey::$key $input => Some($value),
430-
_ => None,
431-
})
432-
};
433-
($task:ident, $key:ident $input:tt => $value:expr) => {
434-
$task
435-
.iter($crate::data::indicies::$key)
436-
.filter_map(|(key, _)| match key {
437-
$crate::data::CachedDataItemKey::$key $input => Some($value),
438-
_ => None,
439-
})
440-
};
441-
($task:ident, $key:ident $input:tt if $cond:expr => $value:expr) => {
434+
($task:ident, $key:ident $key_pattern:tt $(if $cond:expr)? => $iter_item:expr) => {
442435
$task
443436
.iter($crate::data::indicies::$key)
444437
.filter_map(|(key, _)| match key {
445-
$crate::data::CachedDataItemKey::$key $input if $cond => Some($value),
438+
&$crate::data::CachedDataItemKey::$key $key_pattern $(if $cond)? => Some(
439+
$iter_item
440+
),
446441
_ => None,
447442
})
448443
};
449-
($task:ident, $key:ident $input:tt $value_ident:ident => $value:expr) => {
444+
($task:ident, $key:ident $input:tt $value_pattern:tt $(if $cond:expr)? => $iter_item:expr) => {
450445
$task
451446
.iter($crate::data::indicies::$key)
452447
.filter_map(|(key, value)| match (key, value) {
453-
(&$crate::data::CachedDataItemKey::$key $input, &$crate::data::CachedDataItemValue::$key { value: $value_ident }) => Some($value),
454-
_ => None,
455-
})
456-
};
457-
($task:ident, $key:ident $input:tt $value_ident:ident if $cond:expr => $value:expr) => {
458-
$task
459-
.iter($crate::data::indicies::$key)
460-
.filter_map(|(key, value)| match (key, value) {
461-
(&$crate::data::CachedDataItemKey::$key $input, &$crate::data::CachedDataItemValue::$key { value: $value_ident }) if $cond => Some($value),
448+
(
449+
&$crate::data::CachedDataItemKey::$key $input,
450+
&$crate::data::CachedDataItemValue::$key { value: $value_pattern }
451+
) $(if $cond)? => Some($iter_item),
462452
_ => None,
463453
})
464454
};
465455
}
466456

457+
/// A thin wrapper around [`iter_many`] that calls [`Iterator::collect`].
458+
///
459+
/// Note that the return type of [`Iterator::collect`] may be ambiguous in certain contexts, so
460+
/// using this macro may require explicit type annotations on variables.
467461
macro_rules! get_many {
468-
($task:ident, $key:ident $input:tt => $value:expr) => {
469-
$crate::backend::storage::iter_many!($task, $key $input => $value).collect()
470-
};
471-
($task:ident, $key:ident $input:tt => $value:expr) => {
472-
$crate::backend::storage::iter_many!($task, $key $input => $value).collect()
473-
};
474-
($task:ident, $key:ident $input:tt if $cond:expr => $value:expr) => {
475-
$crate::backend::storage::iter_many!($task, $key $input if $cond => $value).collect()
476-
};
477-
($task:ident, $key:ident $input:tt $value_ident:ident => $value:expr) => {
478-
$crate::backend::storage::iter_many!($task, $key $input $value_ident => $value).collect()
479-
};
480-
($task:ident, $key:ident $input:tt $value_ident:ident if $cond:expr => $value:expr) => {
481-
$crate::backend::storage::iter_many!($task, $key $input $value_ident if $cond => $value).collect()
462+
($($args:tt)*) => {
463+
$crate::backend::storage::iter_many!($($args)*).collect()
482464
};
483465
}
484466

@@ -529,7 +511,9 @@ macro_rules! update_count {
529511

530512
macro_rules! remove {
531513
($task:ident, $key:ident $input:tt) => {
532-
if let Some($crate::data::CachedDataItemValue::$key { value }) = $task.remove(&$crate::data::CachedDataItemKey::$key $input) {
514+
if let Some($crate::data::CachedDataItemValue::$key { value }) = $task.remove(
515+
&$crate::data::CachedDataItemKey::$key $input
516+
) {
533517
Some(value)
534518
} else {
535519
None

0 commit comments

Comments
 (0)