From 11eaeb23ed3fc2cc2434ba938ed6659705bea195 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Tue, 14 Jan 2025 13:48:58 +0100 Subject: [PATCH] [Turbopack] add count method to storage (#74830) add count method to storage avoid tracking children count --- .../backend/operation/cleanup_old_edges.rs | 4 +-- .../src/backend/operation/connect_child.rs | 6 ++--- .../src/backend/operation/mod.rs | 6 +++++ .../src/backend/storage.rs | 11 ++++++++ .../crates/turbo-tasks-backend/src/data.rs | 6 +++++ .../src/derive/key_value_pair_macro.rs | 25 +++++++++++++++++++ 6 files changed, 52 insertions(+), 6 deletions(-) diff --git a/turbopack/crates/turbo-tasks-backend/src/backend/operation/cleanup_old_edges.rs b/turbopack/crates/turbo-tasks-backend/src/backend/operation/cleanup_old_edges.rs index 9dcdaa499c9ad..05add4cd36918 100644 --- a/turbopack/crates/turbo-tasks-backend/src/backend/operation/cleanup_old_edges.rs +++ b/turbopack/crates/turbo-tasks-backend/src/backend/operation/cleanup_old_edges.rs @@ -13,7 +13,7 @@ use crate::{ invalidate::{make_task_dirty, TaskDirtyCause}, AggregatedDataUpdate, ExecuteContext, Operation, TaskGuard, }, - storage::{update_count, update_ucount_and_get}, + storage::update_count, TaskDataCategory, }, data::{CachedDataItemKey, CellRef, CollectibleRef, CollectiblesRef}, @@ -82,8 +82,6 @@ impl Operation for CleanupOldEdgesOperation { for &child_id in children.iter() { task.remove(&CachedDataItemKey::Child { task: child_id }); } - let remove_children_count = u32::try_from(children.len()).unwrap(); - update_ucount_and_get!(task, ChildrenCount, -remove_children_count); if is_aggregating_node(get_aggregation_number(&task)) { queue.push(AggregationUpdateJob::InnerOfUpperLostFollowers { upper_id: task_id, diff --git a/turbopack/crates/turbo-tasks-backend/src/backend/operation/connect_child.rs b/turbopack/crates/turbo-tasks-backend/src/backend/operation/connect_child.rs index e6befd263a406..6180b70a3b8fa 100644 --- a/turbopack/crates/turbo-tasks-backend/src/backend/operation/connect_child.rs +++ b/turbopack/crates/turbo-tasks-backend/src/backend/operation/connect_child.rs @@ -12,7 +12,7 @@ use crate::{ }, is_root_node, ExecuteContext, Operation, TaskGuard, }, - storage::{get, update_ucount_and_get}, + storage::{count, get}, TaskDataCategory, }, data::{CachedDataItem, CachedDataItemKey}, @@ -60,8 +60,8 @@ impl ConnectChildOperation { }) { let mut queue = AggregationUpdateQueue::new(); - // Update the children count - let children_count = update_ucount_and_get!(parent_task, ChildrenCount, 1); + // Get the children count + let children_count = count!(parent_task, Child); // Compute future parent aggregation number based on the number of children let current_parent_aggregation = get!(parent_task, AggregationNumber) diff --git a/turbopack/crates/turbo-tasks-backend/src/backend/operation/mod.rs b/turbopack/crates/turbo-tasks-backend/src/backend/operation/mod.rs index 99d84b563ff1d..90da3175c7197 100644 --- a/turbopack/crates/turbo-tasks-backend/src/backend/operation/mod.rs +++ b/turbopack/crates/turbo-tasks-backend/src/backend/operation/mod.rs @@ -371,6 +371,7 @@ pub trait TaskGuard: Debug { fn get(&self, key: &CachedDataItemKey) -> Option>; fn get_mut(&mut self, key: &CachedDataItemKey) -> Option>; fn has_key(&self, key: &CachedDataItemKey) -> bool; + fn count(&self, ty: CachedDataItemType) -> usize; fn iter( &self, ty: CachedDataItemType, @@ -600,6 +601,11 @@ impl TaskGuard for TaskGuardImpl<'_, B> { self.task.has_key(key) } + fn count(&self, ty: CachedDataItemType) -> usize { + self.check_access(ty.category()); + self.task.count(ty) + } + fn iter( &self, ty: CachedDataItemType, diff --git a/turbopack/crates/turbo-tasks-backend/src/backend/storage.rs b/turbopack/crates/turbo-tasks-backend/src/backend/storage.rs index 328cca2d53c2b..c9715413e8fed 100644 --- a/turbopack/crates/turbo-tasks-backend/src/backend/storage.rs +++ b/turbopack/crates/turbo-tasks-backend/src/backend/storage.rs @@ -199,6 +199,10 @@ impl InnerStorage { .unwrap_or_default() } + pub fn count(&self, ty: CachedDataItemType) -> usize { + self.get_map(ty).map(|m| m.len()).unwrap_or_default() + } + pub fn iter( &self, ty: CachedDataItemType, @@ -321,6 +325,12 @@ impl DerefMut for StorageWriteGuard<'_> { } } +macro_rules! count { + ($task:ident, $key:ident) => {{ + $task.count($crate::data::CachedDataItemType::$key) + }}; +} + macro_rules! get { ($task:ident, $key:ident $input:tt) => {{ #[allow(unused_imports)] @@ -516,6 +526,7 @@ macro_rules! remove { }; } +pub(crate) use count; pub(crate) use get; pub(crate) use get_many; pub(crate) use get_mut; diff --git a/turbopack/crates/turbo-tasks-backend/src/data.rs b/turbopack/crates/turbo-tasks-backend/src/data.rs index 7cecd0caca081..5ed4732cc8d91 100644 --- a/turbopack/crates/turbo-tasks-backend/src/data.rs +++ b/turbopack/crates/turbo-tasks-backend/src/data.rs @@ -577,6 +577,12 @@ impl CachedDataItemKey { matches!(self, CachedDataItemKey::CellData { .. }) } + pub fn category(&self) -> TaskDataCategory { + self.ty().category() + } +} + +impl CachedDataItemType { pub fn category(&self) -> TaskDataCategory { match self { Self::Collectible { .. } diff --git a/turbopack/crates/turbo-tasks-macros/src/derive/key_value_pair_macro.rs b/turbopack/crates/turbo-tasks-macros/src/derive/key_value_pair_macro.rs index 05692a23053f7..1d8203dfa977a 100644 --- a/turbopack/crates/turbo-tasks-macros/src/derive/key_value_pair_macro.rs +++ b/turbopack/crates/turbo-tasks-macros/src/derive/key_value_pair_macro.rs @@ -162,6 +162,11 @@ pub fn derive_key_value_pair(input: TokenStream) -> TokenStream { storage.is_none() } }, + len: quote! { + #storage_name::#variant_name { storage } => { + if storage.is_some() { 1 } else { 0 } + } + }, iter: quote! { #storage_name::#variant_name { storage } => { #iter_name::#variant_name(storage.iter()) @@ -234,6 +239,11 @@ pub fn derive_key_value_pair(input: TokenStream) -> TokenStream { storage.is_empty() } }, + len: quote! { + #storage_name::#variant_name { storage } => { + storage.len() + } + }, iter: quote! { #storage_name::#variant_name { storage } => { #iter_name::#variant_name(storage.iter()) @@ -307,6 +317,11 @@ pub fn derive_key_value_pair(input: TokenStream) -> TokenStream { storage.is_empty() } }, + len: quote! { + #storage_name::#variant_name { storage } => { + storage.len() + } + }, iter: quote! { #storage_name::#variant_name { storage } => { #iter_name::#variant_name(storage.iter()) @@ -344,6 +359,7 @@ pub fn derive_key_value_pair(input: TokenStream) -> TokenStream { .iter() .map(|decl| &decl.is_empty) .collect::>(); + let storage_len = storage.iter().map(|decl| &decl.len).collect::>(); let storage_iter = storage.iter().map(|decl| &decl.iter).collect::>(); let storage_iterator = storage .iter() @@ -590,6 +606,14 @@ pub fn derive_key_value_pair(input: TokenStream) -> TokenStream { } } + pub fn len(&self) -> usize { + match self { + #( + #storage_len + )* + } + } + pub fn iter(&self) -> #iter_name { match self { #( @@ -764,6 +788,7 @@ struct StorageDecl { get_mut: proc_macro2::TokenStream, shrink_to_fit: proc_macro2::TokenStream, is_empty: proc_macro2::TokenStream, + len: proc_macro2::TokenStream, iter: proc_macro2::TokenStream, iterator: proc_macro2::TokenStream,