Unverified Commit 11eaeb23 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

[Turbopack] add count method to storage (#74830)

add count method to storage

avoid tracking children count

<!-- Thanks for opening a PR! Your contribution is much appreciated.
To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below.
Choose the right checklist for the change(s) that you're making:

## For Contributors

### Improving Documentation

- Run `pnpm prettier-fix` to fix formatting issues before opening the PR.
- Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide

### Adding or Updating Examples

- The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md
- Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md

### Fixing a bug

- Related issues linked using `fixes #number`
- Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md

### Adding a feature

- Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas)
- Related issues/discussions are linked using `fixes #number`
- e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs)
- Documentation added
- Telemetry added. In case of a feature if it's used or not.
- Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md


## For Maintainers

- Minimal description (aim for explaining to someone not on the team to understand the PR)
- When linking to a Slack thread, you might want to share details of the conclusion
- Link both the Linear (Fixes NEXT-xxx) and the GitHub issues
- Add review comments if necessary to explain to the reviewer the logic behind a change

### What?

### Why?

### How?

Closes NEXT-
Fixes #

-->
parent 0d1dd9ca
...@@ -13,7 +13,7 @@ use crate::{ ...@@ -13,7 +13,7 @@ use crate::{
invalidate::{make_task_dirty, TaskDirtyCause}, invalidate::{make_task_dirty, TaskDirtyCause},
AggregatedDataUpdate, ExecuteContext, Operation, TaskGuard, AggregatedDataUpdate, ExecuteContext, Operation, TaskGuard,
}, },
storage::{update_count, update_ucount_and_get}, storage::update_count,
TaskDataCategory, TaskDataCategory,
}, },
data::{CachedDataItemKey, CellRef, CollectibleRef, CollectiblesRef}, data::{CachedDataItemKey, CellRef, CollectibleRef, CollectiblesRef},
...@@ -82,8 +82,6 @@ impl Operation for CleanupOldEdgesOperation { ...@@ -82,8 +82,6 @@ impl Operation for CleanupOldEdgesOperation {
for &child_id in children.iter() { for &child_id in children.iter() {
task.remove(&CachedDataItemKey::Child { task: child_id }); 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)) { if is_aggregating_node(get_aggregation_number(&task)) {
queue.push(AggregationUpdateJob::InnerOfUpperLostFollowers { queue.push(AggregationUpdateJob::InnerOfUpperLostFollowers {
upper_id: task_id, upper_id: task_id,
......
...@@ -12,7 +12,7 @@ use crate::{ ...@@ -12,7 +12,7 @@ use crate::{
}, },
is_root_node, ExecuteContext, Operation, TaskGuard, is_root_node, ExecuteContext, Operation, TaskGuard,
}, },
storage::{get, update_ucount_and_get}, storage::{count, get},
TaskDataCategory, TaskDataCategory,
}, },
data::{CachedDataItem, CachedDataItemKey}, data::{CachedDataItem, CachedDataItemKey},
...@@ -60,8 +60,8 @@ impl ConnectChildOperation { ...@@ -60,8 +60,8 @@ impl ConnectChildOperation {
}) { }) {
let mut queue = AggregationUpdateQueue::new(); let mut queue = AggregationUpdateQueue::new();
// Update the children count // Get the children count
let children_count = update_ucount_and_get!(parent_task, ChildrenCount, 1); let children_count = count!(parent_task, Child);
// Compute future parent aggregation number based on the number of children // Compute future parent aggregation number based on the number of children
let current_parent_aggregation = get!(parent_task, AggregationNumber) let current_parent_aggregation = get!(parent_task, AggregationNumber)
......
...@@ -371,6 +371,7 @@ pub trait TaskGuard: Debug { ...@@ -371,6 +371,7 @@ pub trait TaskGuard: Debug {
fn get(&self, key: &CachedDataItemKey) -> Option<CachedDataItemValueRef<'_>>; fn get(&self, key: &CachedDataItemKey) -> Option<CachedDataItemValueRef<'_>>;
fn get_mut(&mut self, key: &CachedDataItemKey) -> Option<CachedDataItemValueRefMut<'_>>; fn get_mut(&mut self, key: &CachedDataItemKey) -> Option<CachedDataItemValueRefMut<'_>>;
fn has_key(&self, key: &CachedDataItemKey) -> bool; fn has_key(&self, key: &CachedDataItemKey) -> bool;
fn count(&self, ty: CachedDataItemType) -> usize;
fn iter( fn iter(
&self, &self,
ty: CachedDataItemType, ty: CachedDataItemType,
...@@ -600,6 +601,11 @@ impl<B: BackingStorage> TaskGuard for TaskGuardImpl<'_, B> { ...@@ -600,6 +601,11 @@ impl<B: BackingStorage> TaskGuard for TaskGuardImpl<'_, B> {
self.task.has_key(key) self.task.has_key(key)
} }
fn count(&self, ty: CachedDataItemType) -> usize {
self.check_access(ty.category());
self.task.count(ty)
}
fn iter( fn iter(
&self, &self,
ty: CachedDataItemType, ty: CachedDataItemType,
......
...@@ -199,6 +199,10 @@ impl InnerStorage { ...@@ -199,6 +199,10 @@ impl InnerStorage {
.unwrap_or_default() .unwrap_or_default()
} }
pub fn count(&self, ty: CachedDataItemType) -> usize {
self.get_map(ty).map(|m| m.len()).unwrap_or_default()
}
pub fn iter( pub fn iter(
&self, &self,
ty: CachedDataItemType, ty: CachedDataItemType,
...@@ -321,6 +325,12 @@ impl DerefMut for StorageWriteGuard<'_> { ...@@ -321,6 +325,12 @@ impl DerefMut for StorageWriteGuard<'_> {
} }
} }
macro_rules! count {
($task:ident, $key:ident) => {{
$task.count($crate::data::CachedDataItemType::$key)
}};
}
macro_rules! get { macro_rules! get {
($task:ident, $key:ident $input:tt) => {{ ($task:ident, $key:ident $input:tt) => {{
#[allow(unused_imports)] #[allow(unused_imports)]
...@@ -516,6 +526,7 @@ macro_rules! remove { ...@@ -516,6 +526,7 @@ macro_rules! remove {
}; };
} }
pub(crate) use count;
pub(crate) use get; pub(crate) use get;
pub(crate) use get_many; pub(crate) use get_many;
pub(crate) use get_mut; pub(crate) use get_mut;
......
...@@ -577,6 +577,12 @@ impl CachedDataItemKey { ...@@ -577,6 +577,12 @@ impl CachedDataItemKey {
matches!(self, CachedDataItemKey::CellData { .. }) matches!(self, CachedDataItemKey::CellData { .. })
} }
pub fn category(&self) -> TaskDataCategory {
self.ty().category()
}
}
impl CachedDataItemType {
pub fn category(&self) -> TaskDataCategory { pub fn category(&self) -> TaskDataCategory {
match self { match self {
Self::Collectible { .. } Self::Collectible { .. }
......
...@@ -162,6 +162,11 @@ pub fn derive_key_value_pair(input: TokenStream) -> TokenStream { ...@@ -162,6 +162,11 @@ pub fn derive_key_value_pair(input: TokenStream) -> TokenStream {
storage.is_none() storage.is_none()
} }
}, },
len: quote! {
#storage_name::#variant_name { storage } => {
if storage.is_some() { 1 } else { 0 }
}
},
iter: quote! { iter: quote! {
#storage_name::#variant_name { storage } => { #storage_name::#variant_name { storage } => {
#iter_name::#variant_name(storage.iter()) #iter_name::#variant_name(storage.iter())
...@@ -234,6 +239,11 @@ pub fn derive_key_value_pair(input: TokenStream) -> TokenStream { ...@@ -234,6 +239,11 @@ pub fn derive_key_value_pair(input: TokenStream) -> TokenStream {
storage.is_empty() storage.is_empty()
} }
}, },
len: quote! {
#storage_name::#variant_name { storage } => {
storage.len()
}
},
iter: quote! { iter: quote! {
#storage_name::#variant_name { storage } => { #storage_name::#variant_name { storage } => {
#iter_name::#variant_name(storage.iter()) #iter_name::#variant_name(storage.iter())
...@@ -307,6 +317,11 @@ pub fn derive_key_value_pair(input: TokenStream) -> TokenStream { ...@@ -307,6 +317,11 @@ pub fn derive_key_value_pair(input: TokenStream) -> TokenStream {
storage.is_empty() storage.is_empty()
} }
}, },
len: quote! {
#storage_name::#variant_name { storage } => {
storage.len()
}
},
iter: quote! { iter: quote! {
#storage_name::#variant_name { storage } => { #storage_name::#variant_name { storage } => {
#iter_name::#variant_name(storage.iter()) #iter_name::#variant_name(storage.iter())
...@@ -344,6 +359,7 @@ pub fn derive_key_value_pair(input: TokenStream) -> TokenStream { ...@@ -344,6 +359,7 @@ pub fn derive_key_value_pair(input: TokenStream) -> TokenStream {
.iter() .iter()
.map(|decl| &decl.is_empty) .map(|decl| &decl.is_empty)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let storage_len = storage.iter().map(|decl| &decl.len).collect::<Vec<_>>();
let storage_iter = storage.iter().map(|decl| &decl.iter).collect::<Vec<_>>(); let storage_iter = storage.iter().map(|decl| &decl.iter).collect::<Vec<_>>();
let storage_iterator = storage let storage_iterator = storage
.iter() .iter()
...@@ -590,6 +606,14 @@ pub fn derive_key_value_pair(input: TokenStream) -> TokenStream { ...@@ -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 { pub fn iter(&self) -> #iter_name {
match self { match self {
#( #(
...@@ -764,6 +788,7 @@ struct StorageDecl { ...@@ -764,6 +788,7 @@ struct StorageDecl {
get_mut: proc_macro2::TokenStream, get_mut: proc_macro2::TokenStream,
shrink_to_fit: proc_macro2::TokenStream, shrink_to_fit: proc_macro2::TokenStream,
is_empty: proc_macro2::TokenStream, is_empty: proc_macro2::TokenStream,
len: proc_macro2::TokenStream,
iter: proc_macro2::TokenStream, iter: proc_macro2::TokenStream,
iterator: proc_macro2::TokenStream, iterator: proc_macro2::TokenStream,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment