Unverified Commit 450b07e8 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

Turbopack: allow to customize the parallel execution of turbo-persistence (#82668)

<!-- 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 e21ac64c
......@@ -3,7 +3,7 @@
use std::path::PathBuf;
use anyhow::{Context, Result, bail};
use turbo_persistence::{MetaFileEntryInfo, TurboPersistence};
use turbo_persistence::{MetaFileEntryInfo, SerialScheduler, TurboPersistence};
fn main() -> Result<()> {
// Get CLI argument
......@@ -16,7 +16,7 @@ fn main() -> Result<()> {
bail!("The provided path does not exist: {}", path.display());
}
let db = TurboPersistence::open_read_only(path)?;
let db: TurboPersistence<SerialScheduler> = TurboPersistence::open_read_only(path)?;
let meta_info = db
.meta_info()
.context("Failed to retrieve meta information")?;
......
......@@ -22,7 +22,6 @@ memmap2 = "0.9.5"
parking_lot = { workspace = true }
qfilter = { version = "0.2.4", features = ["serde"] }
quick_cache = { workspace = true }
rayon = { workspace = true }
rustc-hash = { workspace = true }
smallvec = { workspace = true }
thread_local = { workspace = true }
......@@ -32,6 +31,7 @@ zstd = { version = "0.13.2", features = ["zdict_builder"] }
[dev-dependencies]
rand = { workspace = true, features = ["small_rng"] }
rayon = { workspace = true }
tempfile = { workspace = true }
[lints]
......
use std::mem::take;
use crate::{
ValueBuffer,
collector_entry::{CollectorEntry, CollectorEntryValue, EntryKey},
......@@ -111,4 +113,11 @@ impl<K: StoreKey, const SIZE_SHIFT: usize> Collector<K, SIZE_SHIFT> {
self.total_value_size = 0;
self.entries.drain(..)
}
/// Clears the collector and drops the capacity
pub fn drop_contents(&mut self) {
drop(take(&mut self.entries));
self.total_key_size = 0;
self.total_value_size = 0;
}
}
......@@ -13,19 +13,21 @@ mod db;
mod key;
mod lookup_entry;
mod merge_iter;
mod meta_file;
mod meta_file_builder;
mod parallel_scheduler;
mod sst_filter;
mod static_sorted_file;
mod static_sorted_file_builder;
mod value_buf;
mod write_batch;
mod meta_file;
mod meta_file_builder;
mod sst_filter;
#[cfg(test)]
mod tests;
mod value_buf;
pub use arc_slice::ArcSlice;
pub use db::{CompactConfig, MetaFileEntryInfo, MetaFileInfo, TurboPersistence};
pub use key::{KeyBase, QueryKey, StoreKey};
pub use parallel_scheduler::{ParallelScheduler, SerialScheduler};
pub use value_buf::ValueBuffer;
pub use write_batch::WriteBatch;
pub trait ParallelScheduler: Clone + Sync + Send {
fn parallel_for_each<T>(&self, items: &[T], f: impl Fn(&T) + Send + Sync)
where
T: Sync;
fn try_parallel_for_each<'l, T, E>(
&self,
items: &'l [T],
f: impl (Fn(&'l T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Sync,
E: Send + 'static;
fn try_parallel_for_each_mut<'l, T, E>(
&self,
items: &'l mut [T],
f: impl (Fn(&'l mut T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Send + Sync,
E: Send + 'static;
fn try_parallel_for_each_owned<T, E>(
&self,
items: Vec<T>,
f: impl (Fn(T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Send + Sync,
E: Send + 'static;
fn parallel_map_collect<'l, Item, PerItemResult, Result>(
&self,
items: &'l [Item],
f: impl Fn(&'l Item) -> PerItemResult + Send + Sync,
) -> Result
where
Item: Sync,
PerItemResult: Send + Sync + 'l,
Result: FromIterator<PerItemResult>;
fn parallel_map_collect_owned<Item, PerItemResult, Result>(
&self,
items: Vec<Item>,
f: impl Fn(Item) -> PerItemResult + Send + Sync,
) -> Result
where
Item: Send + Sync,
PerItemResult: Send + Sync,
Result: FromIterator<PerItemResult>;
}
#[derive(Clone, Copy, Default)]
pub struct SerialScheduler;
impl ParallelScheduler for SerialScheduler {
fn parallel_for_each<T>(&self, items: &[T], f: impl Fn(&T) + Send + Sync)
where
T: Sync,
{
for item in items {
f(item);
}
}
fn try_parallel_for_each<'l, T, E>(
&self,
items: &'l [T],
f: impl (Fn(&'l T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Sync,
E: Send,
{
for item in items {
f(item)?;
}
Ok(())
}
fn try_parallel_for_each_mut<'l, T, E>(
&self,
items: &'l mut [T],
f: impl (Fn(&'l mut T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Sync,
E: Send,
{
for item in items {
f(item)?;
}
Ok(())
}
fn try_parallel_for_each_owned<T, E>(
&self,
items: Vec<T>,
f: impl (Fn(T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Sync,
E: Send,
{
for item in items {
f(item)?;
}
Ok(())
}
fn parallel_map_collect<'l, Item, PerItemResult, Result>(
&self,
items: &'l [Item],
f: impl Fn(&'l Item) -> PerItemResult + Send + Sync,
) -> Result
where
Item: Sync,
PerItemResult: Send + Sync + 'l,
Result: FromIterator<PerItemResult>,
{
items.iter().map(f).collect()
}
fn parallel_map_collect_owned<Item, PerItemResult, Result>(
&self,
items: Vec<Item>,
f: impl Fn(Item) -> PerItemResult + Send + Sync,
) -> Result
where
Item: Send + Sync,
PerItemResult: Send + Sync,
Result: FromIterator<PerItemResult>,
{
items.into_iter().map(f).collect()
}
}
......@@ -9,9 +9,12 @@ use turbo_tasks::{JoinHandle, message_queue::TimingEvent, spawn, turbo_tasks};
use crate::database::{
key_value_database::{KeySpace, KeyValueDatabase},
turbo::parallel_scheduler::TurboTasksParallelScheduler,
write_batch::{BaseWriteBatch, ConcurrentWriteBatch, WriteBatch, WriteBuffer},
};
mod parallel_scheduler;
const MB: u64 = 1024 * 1024;
const COMPACT_CONFIG: CompactConfig = CompactConfig {
min_merge_count: 3,
......@@ -24,7 +27,7 @@ const COMPACT_CONFIG: CompactConfig = CompactConfig {
};
pub struct TurboKeyValueDatabase {
db: Arc<TurboPersistence>,
db: Arc<TurboPersistence<TurboTasksParallelScheduler>>,
compact_join_handle: Mutex<Option<JoinHandle<Result<()>>>>,
is_ci: bool,
is_short_session: bool,
......@@ -116,7 +119,7 @@ impl KeyValueDatabase for TurboKeyValueDatabase {
}
fn do_compact(
db: &TurboPersistence,
db: &TurboPersistence<TurboTasksParallelScheduler>,
message: &'static str,
max_merge_segment_count: usize,
) -> Result<()> {
......@@ -135,8 +138,8 @@ fn do_compact(
}
pub struct TurboWriteBatch<'a> {
batch: turbo_persistence::WriteBatch<WriteBuffer<'static>, 5>,
db: &'a Arc<TurboPersistence>,
batch: turbo_persistence::WriteBatch<WriteBuffer<'static>, TurboTasksParallelScheduler, 5>,
db: &'a Arc<TurboPersistence<TurboTasksParallelScheduler>>,
compact_join_handle: Option<&'a Mutex<Option<JoinHandle<Result<()>>>>>,
}
......
use turbo_persistence::ParallelScheduler;
use turbo_tasks::parallel;
#[derive(Clone, Copy, Default)]
pub struct TurboTasksParallelScheduler;
impl ParallelScheduler for TurboTasksParallelScheduler {
fn parallel_for_each<T>(&self, items: &[T], f: impl Fn(&T) + Send + Sync)
where
T: Sync,
{
parallel::for_each(items, f);
}
fn try_parallel_for_each<'l, T, E>(
&self,
items: &'l [T],
f: impl (Fn(&'l T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Sync,
E: Send + 'static,
{
parallel::try_for_each(items, f)
}
fn try_parallel_for_each_mut<'l, T, E>(
&self,
items: &'l mut [T],
f: impl (Fn(&'l mut T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Send + Sync,
E: Send + 'static,
{
parallel::try_for_each_mut(items, f)
}
fn try_parallel_for_each_owned<T, E>(
&self,
items: Vec<T>,
f: impl (Fn(T) -> Result<(), E>) + Send + Sync,
) -> Result<(), E>
where
T: Send + Sync,
E: Send + 'static,
{
parallel::try_for_each_owned(items, f)
}
fn parallel_map_collect<'l, T, I, R>(
&self,
items: &'l [T],
f: impl Fn(&'l T) -> I + Send + Sync,
) -> R
where
T: Sync,
I: Send + Sync + 'l,
R: FromIterator<I>,
{
parallel::map_collect(items, f)
}
fn parallel_map_collect_owned<T, I, R>(
&self,
items: Vec<T>,
f: impl Fn(T) -> I + Send + Sync,
) -> R
where
T: Send + Sync,
I: Send + Sync,
R: FromIterator<I>,
{
parallel::map_collect_owned(items, f)
}
}
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