Unverified Commit b7da0c54 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: add parallel execution helpers (#82666)

<!-- 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 76ebbb97
...@@ -410,64 +410,11 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorageSealed ...@@ -410,64 +410,11 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorageSealed
)?; )?;
} }
WriteBatch::Serial(batch) => { WriteBatch::Serial(batch) => {
let mut task_items_result = Ok(Vec::new());
turbo_tasks::scope(|s| {
s.spawn(|_| {
task_items_result =
process_task_data(snapshots, None::<&T::ConcurrentWriteBatch<'_>>);
});
let mut next_task_id =
get_next_free_task_id::<
T::SerialWriteBatch<'_>,
T::ConcurrentWriteBatch<'_>,
>(&mut WriteBatchRef::serial(batch))?;
{
let _span = tracing::trace_span!(
"update task cache",
items = task_cache_updates.iter().map(|m| m.len()).sum::<usize>()
)
.entered();
let mut task_type_bytes = Vec::new();
for (task_type, task_id) in task_cache_updates.into_iter().flatten() {
let task_id = *task_id;
serialize_task_type(&task_type, &mut task_type_bytes, task_id)?;
batch
.put(
KeySpace::ForwardTaskCache,
WriteBuffer::Borrowed(&task_type_bytes),
WriteBuffer::Borrowed(&task_id.to_le_bytes()),
)
.with_context(|| {
anyhow!("Unable to write task cache {task_type:?} => {task_id}")
})?;
batch
.put(
KeySpace::ReverseTaskCache,
WriteBuffer::Borrowed(IntKey::new(task_id).as_ref()),
WriteBuffer::Borrowed(&task_type_bytes),
)
.with_context(|| {
anyhow!("Unable to write task cache {task_id} => {task_type:?}")
})?;
next_task_id = next_task_id.max(task_id + 1);
}
}
save_infra::<T::SerialWriteBatch<'_>, T::ConcurrentWriteBatch<'_>>(
&mut WriteBatchRef::serial(batch),
next_task_id,
session_id,
operations,
)?;
anyhow::Ok(())
})?;
{ {
let _span = tracing::trace_span!("update tasks").entered(); let _span = tracing::trace_span!("update tasks").entered();
for (task_id, meta, data) in task_items_result?.into_iter().flatten() { let task_items =
process_task_data(snapshots, None::<&T::ConcurrentWriteBatch<'_>>)?;
for (task_id, meta, data) in task_items.into_iter().flatten() {
let key = IntKey::new(*task_id); let key = IntKey::new(*task_id);
let key = key.as_ref(); let key = key.as_ref();
if let Some(meta) = meta { if let Some(meta) = meta {
...@@ -485,7 +432,54 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorageSealed ...@@ -485,7 +432,54 @@ impl<T: KeyValueDatabase + Send + Sync + 'static> BackingStorageSealed
})?; })?;
} }
} }
batch.flush(KeySpace::TaskMeta)?;
batch.flush(KeySpace::TaskData)?;
}
let mut next_task_id = get_next_free_task_id::<
T::SerialWriteBatch<'_>,
T::ConcurrentWriteBatch<'_>,
>(&mut WriteBatchRef::serial(batch))?;
{
let _span = tracing::trace_span!(
"update task cache",
items = task_cache_updates.iter().map(|m| m.len()).sum::<usize>()
)
.entered();
let mut task_type_bytes = Vec::new();
for (task_type, task_id) in task_cache_updates.into_iter().flatten() {
let task_id = *task_id;
serialize_task_type(&task_type, &mut task_type_bytes, task_id)?;
batch
.put(
KeySpace::ForwardTaskCache,
WriteBuffer::Borrowed(&task_type_bytes),
WriteBuffer::Borrowed(&task_id.to_le_bytes()),
)
.with_context(|| {
anyhow!("Unable to write task cache {task_type:?} => {task_id}")
})?;
batch
.put(
KeySpace::ReverseTaskCache,
WriteBuffer::Borrowed(IntKey::new(task_id).as_ref()),
WriteBuffer::Borrowed(&task_type_bytes),
)
.with_context(|| {
anyhow!("Unable to write task cache {task_id} => {task_type:?}")
})?;
next_task_id = next_task_id.max(task_id + 1);
}
} }
save_infra::<T::SerialWriteBatch<'_>, T::ConcurrentWriteBatch<'_>>(
&mut WriteBatchRef::serial(batch),
next_task_id,
session_id,
operations,
)?;
} }
} }
......
...@@ -37,6 +37,8 @@ ...@@ -37,6 +37,8 @@
#![feature(never_type)] #![feature(never_type)]
#![feature(downcast_unchecked)] #![feature(downcast_unchecked)]
#![feature(ptr_metadata)] #![feature(ptr_metadata)]
#![feature(sync_unsafe_cell)]
#![feature(vec_into_raw_parts)]
pub mod backend; pub mod backend;
mod capture_future; mod capture_future;
...@@ -64,13 +66,14 @@ mod no_move_vec; ...@@ -64,13 +66,14 @@ mod no_move_vec;
mod once_map; mod once_map;
mod output; mod output;
pub mod panic_hooks; pub mod panic_hooks;
pub mod parallel;
pub mod persisted_graph; pub mod persisted_graph;
pub mod primitives; pub mod primitives;
mod raw_vc; mod raw_vc;
mod read_options; mod read_options;
mod read_ref; mod read_ref;
pub mod registry; pub mod registry;
mod scope; pub mod scope;
mod serialization_invalidation; mod serialization_invalidation;
pub mod small_duration; pub mod small_duration;
mod spawn; mod spawn;
...@@ -115,7 +118,6 @@ pub use raw_vc::{CellId, RawVc, ReadRawVcFuture, ResolveTypeError}; ...@@ -115,7 +118,6 @@ pub use raw_vc::{CellId, RawVc, ReadRawVcFuture, ResolveTypeError};
pub use read_options::ReadCellOptions; pub use read_options::ReadCellOptions;
pub use read_ref::ReadRef; pub use read_ref::ReadRef;
use rustc_hash::FxHasher; use rustc_hash::FxHasher;
pub use scope::scope;
pub use serialization_invalidation::SerializationInvalidator; pub use serialization_invalidation::SerializationInvalidator;
pub use shrink_to_fit::ShrinkToFit; pub use shrink_to_fit::ShrinkToFit;
pub use spawn::{JoinHandle, block_for_future, spawn, spawn_blocking, spawn_thread}; pub use spawn::{JoinHandle, block_for_future, spawn, spawn_blocking, spawn_thread};
......
...@@ -1678,6 +1678,10 @@ pub fn turbo_tasks() -> Arc<dyn TurboTasksApi> { ...@@ -1678,6 +1678,10 @@ pub fn turbo_tasks() -> Arc<dyn TurboTasksApi> {
TURBO_TASKS.with(|arc| arc.clone()) TURBO_TASKS.with(|arc| arc.clone())
} }
pub fn try_turbo_tasks() -> Option<Arc<dyn TurboTasksApi>> {
TURBO_TASKS.try_with(|arc| arc.clone()).ok()
}
pub fn with_turbo_tasks<T>(func: impl FnOnce(&Arc<dyn TurboTasksApi>) -> T) -> T { pub fn with_turbo_tasks<T>(func: impl FnOnce(&Arc<dyn TurboTasksApi>) -> T) -> T {
TURBO_TASKS.with(|arc| func(arc)) TURBO_TASKS.with(|arc| func(arc))
} }
......
//! Parallel for each and map using tokio tasks.
//!
//! This avoid the problem of sleeping threads with mimalloc when using rayon in combination with
//! tokio. It also avoid having multiple thread pools.
//! see also https://pwy.io/posts/mimalloc-cigarette/
use std::{sync::LazyLock, thread::available_parallelism};
use crate::{scope::scope_and_block, util::into_chunks};
/// Calculates a good chunk size for parallel processing based on the number of available threads.
/// This is used to ensure that the workload is evenly distributed across the threads.
fn good_chunk_size(len: usize) -> usize {
static GOOD_CHUNK_COUNT: LazyLock<usize> =
LazyLock::new(|| available_parallelism().map_or(16, |c| c.get() * 4));
let min_chunk_count = *GOOD_CHUNK_COUNT;
len.div_ceil(min_chunk_count)
}
pub fn for_each<'l, T, F>(items: &'l [T], f: F)
where
T: Sync,
F: Fn(&'l T) + Send + Sync,
{
let len = items.len();
if len <= 1 {
for item in items {
f(item);
}
return;
}
let chunk_size = good_chunk_size(len);
let f = &f;
let _results = scope_and_block(len.div_ceil(chunk_size), |scope| {
for chunk in items.chunks(chunk_size) {
scope.spawn(async move {
for item in chunk {
f(item);
}
})
}
});
}
pub fn for_each_owned<T>(items: Vec<T>, f: impl Fn(T) + Send + Sync)
where
T: Send + Sync,
{
let len = items.len();
if len <= 1 {
for item in items {
f(item);
}
return;
}
let chunk_size = good_chunk_size(len);
let f = &f;
let _results = scope_and_block(len.div_ceil(chunk_size), |scope| {
for chunk in into_chunks(items, chunk_size) {
scope.spawn(async move {
// SAFETY: Even when f() panics we drop all items in the chunk.
for item in chunk {
f(item);
}
})
}
});
}
pub fn try_for_each<'l, T, E>(
items: &'l [T],
f: impl (Fn(&'l T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Sync,
E: Send + 'static,
{
let len = items.len();
if len <= 1 {
for item in items {
f(item)?;
}
return Ok(());
}
let chunk_size = good_chunk_size(len);
let f = &f;
scope_and_block(len.div_ceil(chunk_size), |scope| {
for chunk in items.chunks(chunk_size) {
scope.spawn(async move {
for item in chunk {
f(item)?;
}
Ok(())
})
}
})
.collect::<Result<(), E>>()
}
pub fn try_for_each_mut<'l, T, E>(
items: &'l mut [T],
f: impl (Fn(&'l mut T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Send + Sync,
E: Send + 'static,
{
let len = items.len();
if len <= 1 {
for item in items {
f(item)?;
}
return Ok(());
}
let chunk_size = good_chunk_size(len);
let f = &f;
scope_and_block(len.div_ceil(chunk_size), |scope| {
for chunk in items.chunks_mut(chunk_size) {
scope.spawn(async move {
for item in chunk {
f(item)?;
}
Ok(())
})
}
})
.collect::<Result<(), E>>()
}
pub fn try_for_each_owned<T, E>(
items: Vec<T>,
f: impl (Fn(T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Send + Sync,
E: Send + 'static,
{
let len = items.len();
if len <= 1 {
for item in items {
f(item)?;
}
return Ok(());
}
let chunk_size = good_chunk_size(len);
let f = &f;
scope_and_block(len.div_ceil(chunk_size), |scope| {
for chunk in into_chunks(items, chunk_size) {
scope.spawn(async move {
for item in chunk {
f(item)?;
}
Ok(())
})
}
})
.collect::<Result<(), E>>()
}
pub fn map_collect<'l, Item, PerItemResult, Result>(
items: &'l [Item],
f: impl Fn(&'l Item) -> PerItemResult + Send + Sync,
) -> Result
where
Item: Sync,
PerItemResult: Send + Sync + 'l,
Result: FromIterator<PerItemResult>,
{
let len = items.len();
if len == 0 {
return Result::from_iter(std::iter::empty()); // No items to process, return empty
// collection
}
let chunk_size = good_chunk_size(len);
let f = &f;
scope_and_block(len.div_ceil(chunk_size), |scope| {
for chunk in items.chunks(chunk_size) {
scope.spawn(async move { chunk.iter().map(f).collect::<Vec<_>>() })
}
})
.flatten()
.collect()
}
pub fn map_collect_owned<'l, Item, PerItemResult, Result>(
items: Vec<Item>,
f: impl Fn(Item) -> PerItemResult + Send + Sync,
) -> Result
where
Item: Send + Sync,
PerItemResult: Send + Sync + 'l,
Result: FromIterator<PerItemResult>,
{
let len = items.len();
if len == 0 {
return Result::from_iter(std::iter::empty()); // No items to process, return empty
// collection;
}
let chunk_size = good_chunk_size(len);
let f = &f;
scope_and_block(len.div_ceil(chunk_size), |scope| {
for chunk in into_chunks(items, chunk_size) {
scope.spawn(async move { chunk.map(f).collect::<Vec<_>>() })
}
})
.flatten()
.collect()
}
#[cfg(test)]
mod tests {
use std::{
panic::{AssertUnwindSafe, catch_unwind},
sync::atomic::{AtomicI32, Ordering},
};
use super::*;
#[tokio::test(flavor = "multi_thread")]
async fn test_parallel_for_each() {
let input = vec![1, 2, 3, 4, 5];
let sum = AtomicI32::new(0);
for_each(&input, |&x| {
sum.fetch_add(x, Ordering::SeqCst);
});
assert_eq!(sum.load(Ordering::SeqCst), 15);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_parallel_try_for_each() {
let input = vec![1, 2, 3, 4, 5];
let result = try_for_each(&input, |&x| {
if x % 2 == 0 {
Ok(())
} else {
Err(format!("Odd number {x} encountered"))
}
});
assert!(result.is_err());
assert_eq!(result.unwrap_err(), "Odd number 1 encountered");
}
#[tokio::test(flavor = "multi_thread")]
async fn test_parallel_try_for_each_mut() {
let mut input = vec![1, 2, 3, 4, 5];
let result = try_for_each_mut(&mut input, |x| {
*x += 10;
if *x % 2 == 0 {
Ok(())
} else {
Err(format!("Odd number {} encountered", *x))
}
});
assert!(result.is_err());
assert_eq!(result.unwrap_err(), "Odd number 11 encountered");
assert_eq!(input, vec![11, 12, 13, 14, 15]);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_parallel_for_each_owned() {
let input = vec![1, 2, 3, 4, 5];
let sum = AtomicI32::new(0);
for_each_owned(input, |x| {
sum.fetch_add(x, Ordering::SeqCst);
});
assert_eq!(sum.load(Ordering::SeqCst), 15);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_parallel_map_collect() {
let input = vec![1, 2, 3, 4, 5];
let result: Vec<_> = map_collect(&input, |&x| x * 2);
assert_eq!(result, vec![2, 4, 6, 8, 10]);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_parallel_map_collect_owned() {
let input = vec![1, 2, 3, 4, 5];
let result: Vec<_> = map_collect_owned(input, |x| x * 2);
assert_eq!(result, vec![2, 4, 6, 8, 10]);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_parallel_map_collect_owned_many() {
let input = vec![1; 1000];
let result: Vec<_> = map_collect_owned(input, |x| x * 2);
assert_eq!(result, vec![2; 1000]);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_panic_in_scope() {
let result = catch_unwind(AssertUnwindSafe(|| {
let mut input = vec![1; 1000];
input[744] = 2;
for_each(&input, |x| {
if *x == 2 {
panic!("Intentional panic");
}
});
panic!("Should not get here")
}));
assert!(result.is_err());
assert_eq!(
result.unwrap_err().downcast_ref::<&str>(),
Some(&"Intentional panic")
);
}
}
This diff is collapsed.
use std::{ use std::{
cell::SyncUnsafeCell,
error::Error as StdError, error::Error as StdError,
fmt::{Debug, Display}, fmt::{Debug, Display},
future::Future, future::Future,
hash::{Hash, Hasher}, hash::{Hash, Hasher},
mem::ManuallyDrop,
ops::Deref, ops::Deref,
pin::Pin, pin::Pin,
sync::Arc, sync::Arc,
...@@ -259,3 +261,124 @@ impl<F: Future, W: for<'a> Fn(Pin<&mut F>, &mut Context<'a>) -> Poll<F::Output>> ...@@ -259,3 +261,124 @@ impl<F: Future, W: for<'a> Fn(Pin<&mut F>, &mut Context<'a>) -> Poll<F::Output>>
(this.wrapper)(this.future, cx) (this.wrapper)(this.future, cx)
} }
} }
/// Similar to slice::chunks but for owned data. Chunks are Send and Sync to allow to use it for
/// parallelism.
pub fn into_chunks<T>(data: Vec<T>, chunk_size: usize) -> IntoChunks<T> {
let (ptr, length, capacity) = data.into_raw_parts();
// SAFETY: changing a pointer from T to SyncUnsafeCell<ManuallyDrop<..>> is safe as both types
// have repr(transparent).
let ptr = ptr as *mut SyncUnsafeCell<ManuallyDrop<T>>;
// SAFETY: The ptr, length and capacity were from into_raw_parts(). This is the only place where
// we use ptr.
let data =
unsafe { Vec::<SyncUnsafeCell<ManuallyDrop<T>>>::from_raw_parts(ptr, length, capacity) };
IntoChunks {
data: Arc::new(data),
index: 0,
chunk_size,
}
}
pub struct IntoChunks<T> {
data: Arc<Vec<SyncUnsafeCell<ManuallyDrop<T>>>>,
index: usize,
chunk_size: usize,
}
impl<T> Iterator for IntoChunks<T> {
type Item = Chunk<T>;
fn next(&mut self) -> Option<Self::Item> {
if self.index < self.data.len() {
let end = self.data.len().min(self.index + self.chunk_size);
let item = Chunk {
data: Arc::clone(&self.data),
index: self.index,
end,
};
self.index = end;
Some(item)
} else {
None
}
}
}
impl<T> IntoChunks<T> {
fn next_item(&mut self) -> Option<T> {
if self.index < self.data.len() {
// SAFETY: We are the only owner of this chunk of data and we make sure that this item
// is no longer dropped by moving the index
let item = unsafe { ManuallyDrop::take(&mut *self.data[self.index].get()) };
self.index += 1;
Some(item)
} else {
None
}
}
}
impl<T> Drop for IntoChunks<T> {
fn drop(&mut self) {
// To avoid leaking memory we need to drop the remaining items
while self.next_item().is_some() {}
}
}
pub struct Chunk<T> {
data: Arc<Vec<SyncUnsafeCell<ManuallyDrop<T>>>>,
index: usize,
end: usize,
}
impl<T> Iterator for Chunk<T> {
type Item = T;
fn next(&mut self) -> Option<Self::Item> {
if self.index < self.end {
// SAFETY: We are the only owner of this chunk of data and we make sure that this item
// is no longer dropped by moving the index
let item = unsafe { ManuallyDrop::take(&mut *self.data[self.index].get()) };
self.index += 1;
Some(item)
} else {
None
}
}
}
impl<T> Drop for Chunk<T> {
fn drop(&mut self) {
// To avoid leaking memory we need to drop the remaining items
while self.next().is_some() {}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_chunk_iterator() {
let data = [(); 10]
.into_iter()
.enumerate()
.map(|(i, _)| Arc::new(i))
.collect::<Vec<_>>();
let mut chunks = into_chunks(data.clone(), 3);
let mut first_chunk = chunks.next().unwrap();
let second_chunk = chunks.next().unwrap();
drop(chunks);
assert_eq!(
second_chunk.into_iter().map(|a| *a).collect::<Vec<_>>(),
vec![3, 4, 5]
);
assert_eq!(*first_chunk.next().unwrap(), 0);
assert_eq!(*first_chunk.next().unwrap(), 1);
drop(first_chunk);
for arc in data {
assert_eq!(Arc::strong_count(&arc), 1);
}
}
}
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