Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Turbopack] remove exceeding cells #69059

Closed
wants to merge 49 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
9d1592f
add new backend
sokra Jul 26, 2024
023f16e
new backend
sokra Jul 29, 2024
32b447a
add more backend method implementations
sokra Aug 2, 2024
fcea2fd
very basic operation working
sokra Aug 7, 2024
c6f4e70
add own cells reading
sokra Aug 7, 2024
db0bc43
call transient tasks
sokra Aug 7, 2024
ca6cbb4
call persistent tasks
sokra Aug 7, 2024
da0f04c
run tests multiple times to test caching
sokra Aug 7, 2024
41d716c
add cell/output reading and creating cached tasks
sokra Aug 7, 2024
f573143
serialization fixup
sokra Aug 9, 2024
5029e1c
remove unused stuff
sokra Aug 9, 2024
ed8b087
clippy
sokra Aug 9, 2024
2fb17f3
remove things not yet used (persistence)
sokra Aug 9, 2024
0d8a666
clippy
sokra Aug 9, 2024
37df4cc
add more tests for new backend
sokra Aug 9, 2024
eb0b6c9
add support for connect_child
sokra Aug 13, 2024
78af7a5
set root type on root tasks
sokra Aug 9, 2024
ffc9e21
remove old edges when task recomputation completes
sokra Aug 12, 2024
c448018
add try_get_function_id
sokra Aug 14, 2024
b0977cb
Revert "remove things not yet used (persistence)"
sokra Aug 9, 2024
dd40246
add lmdb persisting and restoring
sokra Aug 8, 2024
bdec3c3
pass test name to test_config to construct db name
sokra Aug 9, 2024
2cce8d9
continue uncompleted operations
sokra Aug 9, 2024
d3fcc67
create dir and logging
sokra Aug 12, 2024
024c2f1
improve error messages
sokra Aug 13, 2024
95e3f2c
handle keys larger than 511 bytes
sokra Aug 13, 2024
6dac853
set root type on root tasks
sokra Aug 9, 2024
4d5b42e
move backend impl into type alias
sokra Aug 12, 2024
733d42a
add new backend feature
sokra Aug 12, 2024
c736b0d
add missing import
sokra Aug 14, 2024
36b118c
initial aggregation update
sokra Aug 13, 2024
03abe58
enable new backend
sokra Aug 13, 2024
686c198
WIP: remove todos
sokra Aug 13, 2024
e126a75
more aggregation operations
sokra Aug 13, 2024
bc62ead
handle state serialization
sokra Aug 14, 2024
aa5ec04
validate serialization and improve errors
sokra Aug 14, 2024
c39ec7d
avoid some serde skip and untagged
sokra Aug 14, 2024
40aab63
use TransientState
sokra Aug 14, 2024
1aa5572
validate serialization and improve errors
sokra Aug 14, 2024
4835ae1
store project options in state
sokra Aug 14, 2024
44e902a
show lookup error
sokra Aug 14, 2024
d4f7561
log restored db entries
sokra Aug 14, 2024
26b7652
improve error
sokra Aug 14, 2024
2a0dd5a
improve benchmark_file_io for persistent cache
sokra Aug 14, 2024
4866761
WIP: print new tasks
sokra Aug 14, 2024
6db46f3
gracefully stop turbo-tasks to allow persisting to complete
sokra Aug 14, 2024
a96143d
avoid storing transient tasks
sokra Aug 14, 2024
92aff18
WIP: logging
sokra Aug 14, 2024
15950e6
remove exceeding cells
sokra Aug 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add new backend feature
  • Loading branch information
sokra committed Sep 4, 2024
commit 733d42a582b7bcf96b10ffe7fc7aae6fb690f051
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ swc-ast-explorer = { path = "turbopack/crates/turbopack-swc-ast-explorer" }
turbo-prehash = { path = "turbopack/crates/turbo-prehash" }
turbo-tasks-malloc = { path = "turbopack/crates/turbo-tasks-malloc", default-features = false }
turbo-tasks = { path = "turbopack/crates/turbo-tasks" }
turbo-tasks-backend = { path = "turbopack/crates/turbo-tasks-backend" }
turbo-tasks-build = { path = "turbopack/crates/turbo-tasks-build" }
turbo-tasks-bytes = { path = "turbopack/crates/turbo-tasks-bytes" }
turbo-tasks-env = { path = "turbopack/crates/turbo-tasks-env" }
Expand Down
3 changes: 3 additions & 0 deletions crates/napi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ __internal_dhat-heap = ["dhat"]
# effectively does nothing.
__internal_dhat-ad-hoc = ["dhat"]

new-backend = ["dep:turbo-tasks-backend"]

# Enable specific tls features per-target.
[target.'cfg(all(target_os = "windows", target_arch = "aarch64"))'.dependencies]
next-core = { workspace = true, features = ["native-tls"] }
Expand Down Expand Up @@ -105,6 +107,7 @@ lightningcss-napi = { workspace = true }
tokio = { workspace = true, features = ["full"] }
turbo-tasks = { workspace = true }
turbo-tasks-memory = { workspace = true }
turbo-tasks-backend = { workspace = true, optional = true }
turbo-tasks-fs = { workspace = true }
next-api = { workspace = true }
next-build = { workspace = true }
Expand Down
57 changes: 28 additions & 29 deletions crates/napi/src/next_api/project.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io::Write, path::PathBuf, sync::Arc, thread, time::Duration};
use std::{path::PathBuf, sync::Arc, thread, time::Duration};

use anyhow::{anyhow, bail, Context, Result};
use napi::{
Expand Down Expand Up @@ -43,8 +43,8 @@ use url::Url;
use super::{
endpoint::ExternalEndpoint,
utils::{
get_diagnostics, get_issues, subscribe, NapiDiagnostic, NapiIssue, NextBackend, RootTask,
TurbopackResult, VcArc,
create_turbo_tasks, get_diagnostics, get_issues, subscribe, NapiDiagnostic, NapiIssue,
NextBackend, RootTask, TurbopackResult, VcArc,
},
};
use crate::register;
Expand Down Expand Up @@ -88,7 +88,7 @@ pub struct NapiProjectOptions {

/// next.config's distDir. Project initialization occurs eariler than
/// deserializing next.config, so passing it as separate option.
pub dist_dir: Option<String>,
pub dist_dir: String,

/// Whether to watch he filesystem for file changes.
pub watch: bool,
Expand Down Expand Up @@ -279,10 +279,7 @@ pub async fn project_new(
let subscriber = Registry::default();

let subscriber = subscriber.with(EnvFilter::builder().parse(trace).unwrap());
let dist_dir = options
.dist_dir
.as_ref()
.map_or_else(|| ".next".to_string(), |d| d.to_string());
let dist_dir = options.dist_dir.clone();

let internal_dir = PathBuf::from(&options.project_path).join(dist_dir);
std::fs::create_dir_all(&internal_dir)
Expand All @@ -308,27 +305,29 @@ pub async fn project_new(
subscriber.init();
}

let turbo_tasks = TurboTasks::new(NextBackend::new(
turbo_engine_options
.memory_limit
.map(|m| m as usize)
.unwrap_or(usize::MAX),
));
let stats_path = std::env::var_os("NEXT_TURBOPACK_TASK_STATISTICS");
if let Some(stats_path) = stats_path {
let task_stats = turbo_tasks.backend().task_statistics().enable().clone();
exit.on_exit(async move {
tokio::task::spawn_blocking(move || {
let mut file = std::fs::File::create(&stats_path)
.with_context(|| format!("failed to create or open {stats_path:?}"))?;
serde_json::to_writer(&file, &task_stats)
.context("failed to serialize or write task statistics")?;
file.flush().context("failed to flush file")
})
.await
.unwrap()
.unwrap();
});
let memory_limit = turbo_engine_options
.memory_limit
.map(|m| m as usize)
.unwrap_or(usize::MAX);
let turbo_tasks = create_turbo_tasks(PathBuf::from(&options.dist_dir), memory_limit)?;
#[cfg(not(feature = "new-backend"))]
{
let stats_path = std::env::var_os("NEXT_TURBOPACK_TASK_STATISTICS");
if let Some(stats_path) = stats_path {
let task_stats = turbo_tasks.backend().task_statistics().enable().clone();
exit.on_exit(async move {
tokio::task::spawn_blocking(move || {
let mut file = std::fs::File::create(&stats_path)
.with_context(|| format!("failed to create or open {stats_path:?}"))?;
serde_json::to_writer(&file, &task_stats)
.context("failed to serialize or write task statistics")?;
file.flush().context("failed to flush file")
})
.await
.unwrap()
.unwrap();
});
}
}
let options = options.into();
let container = turbo_tasks
Expand Down
22 changes: 19 additions & 3 deletions crates/napi/src/next_api/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, future::Future, ops::Deref, sync::Arc};
use std::{collections::HashMap, future::Future, ops::Deref, path::PathBuf, sync::Arc};

use anyhow::{anyhow, Context, Result};
use napi::{
Expand All @@ -9,15 +9,31 @@ use napi::{
use serde::Serialize;
use turbo_tasks::{ReadRef, TaskId, TryJoinIterExt, TurboTasks, Vc};
use turbo_tasks_fs::FileContent;
use turbo_tasks_memory::MemoryBackend;
use turbopack_core::{
diagnostics::{Diagnostic, DiagnosticContextExt, PlainDiagnostic},
error::PrettyPrintError,
issue::{IssueDescriptionExt, PlainIssue, PlainIssueSource, PlainSource, StyledString},
source_pos::SourcePos,
};

pub type NextBackend = MemoryBackend;
#[cfg(not(feature = "new-backend"))]
pub type NextBackend = turbo_tasks_memory::MemoryBackend;
#[cfg(feature = "new-backend")]
pub type NextBackend = turbo_tasks_backend::TurboTasksBackend;

#[allow(unused_variables, reason = "feature-gated")]
pub fn create_turbo_tasks(
output_path: PathBuf,
memory_limit: usize,
) -> Result<Arc<TurboTasks<NextBackend>>> {
#[cfg(not(feature = "new-backend"))]
let backend = TurboTasks::new(turbo_tasks_memory::MemoryBackend::new(memory_limit));
#[cfg(feature = "new-backend")]
let backend = TurboTasks::new(turbo_tasks_backend::TurboTasksBackend::new(Arc::new(
turbo_tasks_backend::LmdbBackingStorage::new(&output_path.join("cache/turbopack"))?,
)));
Ok(backend)
}

/// A helper type to hold both a Vc operation and the TurboTasks root process.
/// Without this, we'd need to pass both individually all over the place
Expand Down
25 changes: 12 additions & 13 deletions crates/napi/src/turbotrace.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{path::PathBuf, sync::Arc};

use napi::bindgen_prelude::*;
use node_file_trace::{start, Args};
Expand All @@ -8,29 +8,28 @@ use turbopack::{
resolve_options_context::ResolveOptionsContext,
};

use crate::next_api::utils::NextBackend;
use crate::next_api::utils::{self, NextBackend};

#[napi]
pub fn create_turbo_tasks(memory_limit: Option<i64>) -> External<Arc<TurboTasks<NextBackend>>> {
let turbo_tasks = TurboTasks::new(NextBackend::new(
memory_limit.map(|m| m as usize).unwrap_or(usize::MAX),
));
External::new_with_size_hint(
turbo_tasks,
memory_limit.map(|u| u as usize).unwrap_or(usize::MAX),
)
pub fn create_turbo_tasks(
output_path: String,
memory_limit: Option<i64>,
) -> External<Arc<TurboTasks<NextBackend>>> {
let limit = memory_limit.map(|u| u as usize).unwrap_or(usize::MAX);
let turbo_tasks = utils::create_turbo_tasks(PathBuf::from(&output_path), limit)
.expect("Failed to create TurboTasks");
External::new_with_size_hint(turbo_tasks, limit)
}

#[napi]
pub async fn run_turbo_tracing(
options: Buffer,
turbo_tasks: Option<External<Arc<TurboTasks<NextBackend>>>>,
turbo_tasks: External<Arc<TurboTasks<NextBackend>>>,
) -> napi::Result<Vec<String>> {
let args: Args = serde_json::from_slice(options.as_ref())?;
let turbo_tasks = turbo_tasks.map(|t| t.clone());
let files = start(
Arc::new(args),
turbo_tasks.as_ref(),
turbo_tasks.clone(),
Some(ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext {
enable_types: true,
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/build/collect-build-traces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export async function collectBuildTraces({
let turbotraceOutputPath: string | undefined
let turbotraceFiles: string[] | undefined
turboTasksForTrace = bindings.turbo.createTurboTasks(
distDir,
(config.experimental.turbotrace?.memoryLimit ??
TURBO_TRACE_DEFAULT_MEMORY_LIMIT) *
1024 *
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1266,6 +1266,7 @@ export default async function build(
{
projectPath: dir,
rootPath: config.outputFileTracingRoot || dir,
distDir,
nextConfig: config,
jsConfig: await getTurbopackJsConfig(dir, config),
watch: false,
Expand Down
14 changes: 11 additions & 3 deletions packages/next/src/build/swc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ export interface ProjectOptions {
*/
projectPath: string

/**
* The path to the .next directory.
*/
distDir: string

/**
* The next.config.js contents.
*/
Expand Down Expand Up @@ -1547,15 +1552,18 @@ function loadNative(importPath?: string) {
initHeapProfiler: bindings.initHeapProfiler,
teardownHeapProfiler: bindings.teardownHeapProfiler,
turbo: {
startTrace: (options = {}, turboTasks: unknown) => {
startTrace: (options = {}, turboTasks: { __napi: 'TurboTasks' }) => {
initHeapProfiler()
return (customBindings ?? bindings).runTurboTracing(
toBuffer({ exact: true, ...options }),
turboTasks
)
},
createTurboTasks: (memoryLimit?: number): unknown =>
bindings.createTurboTasks(memoryLimit),
createTurboTasks: (
outputPath: string,
memoryLimit?: number
): { __napi: 'TurboTasks' } =>
bindings.createTurboTasks(outputPath, memoryLimit),
entrypoints: {
stream: (
turboTasks: any,
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/server/dev/hot-reloader-turbopack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export async function createHotReloaderTurbopack(
{
projectPath: dir,
rootPath: opts.nextConfig.outputFileTracingRoot || dir,
distDir,
nextConfig: opts.nextConfig,
jsConfig: await getTurbopackJsConfig(dir, nextConfig),
watch: true,
Expand Down
14 changes: 8 additions & 6 deletions test/development/basic/next-rs-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,13 +191,20 @@ describe('next.rs api', () => {
console.log(next.testDir)
const nextConfig = await loadConfig(PHASE_DEVELOPMENT_SERVER, next.testDir)
const bindings = await loadBindings()
const distDir = path.join(
process.env.NEXT_SKIP_ISOLATE
? path.resolve(__dirname, '../../..')
: next.testDir,
'.next'
)
project = await bindings.turbo.createProject({
env: {},
jsConfig: {
compilerOptions: {},
},
nextConfig: nextConfig,
projectPath: next.testDir,
distDir,
rootPath: process.env.NEXT_SKIP_ISOLATE
? path.resolve(__dirname, '../../..')
: next.testDir,
Expand All @@ -208,12 +215,7 @@ describe('next.rs api', () => {
clientRouterFilters: undefined,
config: nextConfig,
dev: true,
distDir: path.join(
process.env.NEXT_SKIP_ISOLATE
? path.resolve(__dirname, '../../..')
: next.testDir,
'.next'
),
distDir: distDir,
fetchCacheKeyPrefix: undefined,
hasRewrites: false,
middlewareMatchers: undefined,
Expand Down
Loading