Skip to content

Commit

Permalink
perf(turbopack): Use ResolvedVc for next-api, part 5 (#73238)
Browse files Browse the repository at this point in the history
### What?

Use `ResolvedVc<T>` instead of `Vc<T>` for struct fields in `next-api`, but split into multiple PRs.

### Why?

To reduce scope of changes of each PRs so I can debug HMR failures.

### How?
  • Loading branch information
kdy1 authored Nov 28, 2024
1 parent 9e376fe commit bcb28d3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 34 deletions.
6 changes: 3 additions & 3 deletions crates/napi/src/next_api/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,15 +692,15 @@ pub fn project_entrypoints_subscribe(
.transpose()?,
pages_document_endpoint: External::new(ExternalEndpoint(VcArc::new(
turbo_tasks.clone(),
entrypoints.pages_document_endpoint,
*entrypoints.pages_document_endpoint,
))),
pages_app_endpoint: External::new(ExternalEndpoint(VcArc::new(
turbo_tasks.clone(),
entrypoints.pages_app_endpoint,
*entrypoints.pages_app_endpoint,
))),
pages_error_endpoint: External::new(ExternalEndpoint(VcArc::new(
turbo_tasks.clone(),
entrypoints.pages_error_endpoint,
*entrypoints.pages_error_endpoint,
))),
},
issues: issues
Expand Down
25 changes: 14 additions & 11 deletions crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub struct AppProject {
}

#[turbo_tasks::value(transparent)]
pub struct OptionAppProject(Option<Vc<AppProject>>);
pub struct OptionAppProject(Option<ResolvedVc<AppProject>>);

impl AppProject {}

Expand Down Expand Up @@ -746,7 +746,7 @@ fn server_utils_modifier() -> Vc<RcStr> {
}

#[turbo_tasks::value(transparent)]
struct OutputAssetsWithAvailability((Vc<OutputAssets>, AvailabilityInfo));
struct OutputAssetsWithAvailability((ResolvedVc<OutputAssets>, AvailabilityInfo));

#[derive(Copy, Clone, Serialize, Deserialize, PartialEq, Eq, Debug, TraceRawVcs)]
enum AppPageEndpointType {
Expand Down Expand Up @@ -981,7 +981,7 @@ impl AppEndpoint {
.iter()
.map(|(k, v)| (*k, v.clone())),
);
visited_modules = result.visited_modules;
visited_modules = *result.visited_modules;
}

client_dynamic_imports
Expand Down Expand Up @@ -1188,7 +1188,7 @@ impl AppEndpoint {
app_entry.original_name.clone(),
client_references,
client_references_chunks,
*app_entry_chunks,
**app_entry_chunks,
Value::new(*app_entry_chunks_availability),
client_chunking_context,
ssr_chunking_context,
Expand Down Expand Up @@ -1241,7 +1241,7 @@ impl AppEndpoint {
file_paths_from_root
.extend(get_js_paths_from_root(&node_root_value, &app_entry_chunks_ref).await?);

let all_output_assets = all_assets_from_entries(*app_entry_chunks).await?;
let all_output_assets = all_assets_from_entries(**app_entry_chunks).await?;

wasm_paths_from_root
.extend(get_wasm_paths_from_root(&node_root_value, &middleware_assets).await?);
Expand Down Expand Up @@ -1466,11 +1466,14 @@ impl AppEndpoint {
{
let _span = tracing::info_span!("Server Components");
Vc::cell((
chunking_context.evaluated_chunk_group_assets(
app_entry.rsc_entry.ident(),
Vc::cell(evaluatable_assets.clone()),
Value::new(AvailabilityInfo::Root),
),
chunking_context
.evaluated_chunk_group_assets(
app_entry.rsc_entry.ident(),
Vc::cell(evaluatable_assets.clone()),
Value::new(AvailabilityInfo::Root),
)
.to_resolved()
.await?,
AvailabilityInfo::Untracked,
))
}
Expand Down Expand Up @@ -1571,7 +1574,7 @@ impl AppEndpoint {
}
.instrument(tracing::trace_span!("server node entrypoint"))
.await?);
Vc::cell((Vc::cell(vec![rsc_chunk]), availability_info))
Vc::cell((ResolvedVc::cell(vec![rsc_chunk]), availability_info))
}
})
}
Expand Down
17 changes: 10 additions & 7 deletions crates/next-api/src/dynamic_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ where
F: FnMut(Vc<Box<dyn ChunkableModule>>) -> Fu,
Fu: Future<Output = Result<Vc<OutputAssets>>> + Send,
{
let mut chunks_hash: HashMap<RcStr, Vc<OutputAssets>> = HashMap::new();
let mut chunks_hash: HashMap<RcStr, ResolvedVc<OutputAssets>> = HashMap::new();
let mut dynamic_import_chunks = FxIndexMap::default();

// Iterate over the collected import mappings, and create a chunk for each
Expand All @@ -59,7 +59,7 @@ where
// naive hash to have additional
// chunks in case if there are same modules being imported in different
// origins.
let chunk_group = build_chunk(*module).await?;
let chunk_group = build_chunk(*module).await?.to_resolved().await?;
chunks_hash.insert(imported_raw_str.clone(), chunk_group);
chunk_group
};
Expand Down Expand Up @@ -106,7 +106,7 @@ pub(crate) async fn collect_evaluated_chunk_group(
#[turbo_tasks::value(shared)]
pub struct NextDynamicImportsResult {
pub client_dynamic_imports: FxIndexMap<ResolvedVc<Box<dyn Module>>, DynamicImportedModules>,
pub visited_modules: Vc<VisitedDynamicImportModules>,
pub visited_modules: ResolvedVc<VisitedDynamicImportModules>,
}

#[turbo_tasks::value(shared)]
Expand Down Expand Up @@ -201,7 +201,7 @@ pub(crate) async fn collect_next_dynamic_imports(

Ok(NextDynamicImportsResult {
client_dynamic_imports: import_mappings,
visited_modules: VisitedDynamicImportModules(visited_modules.0).cell(),
visited_modules: VisitedDynamicImportModules(visited_modules.0).resolved_cell(),
}
.cell())
}
Expand Down Expand Up @@ -338,7 +338,10 @@ async fn build_dynamic_imports_map_for_module(
}
}

Ok(Vc::cell(Some(Vc::cell((server_module, import_sources)))))
Ok(Vc::cell(Some(ResolvedVc::cell((
server_module,
import_sources,
)))))
}

/// A visitor to check if there's import to `next/dynamic`, then collecting the
Expand Down Expand Up @@ -421,7 +424,7 @@ impl Visit for CollectImportSourceVisitor {
}

pub type DynamicImportedModules = Vec<(RcStr, ResolvedVc<Box<dyn Module>>)>;
pub type DynamicImportedOutputAssets = Vec<(RcStr, Vc<OutputAssets>)>;
pub type DynamicImportedOutputAssets = Vec<(RcStr, ResolvedVc<OutputAssets>)>;

/// A struct contains mapping for the dynamic imports to construct chunk per
/// each individual module (Origin Module, Vec<(ImportSourceString, Module)>)
Expand All @@ -430,7 +433,7 @@ pub struct DynamicImportsMap(pub (ResolvedVc<Box<dyn Module>>, DynamicImportedMo

/// An Option wrapper around [DynamicImportsMap].
#[turbo_tasks::value(transparent)]
pub struct OptionDynamicImportsMap(Option<Vc<DynamicImportsMap>>);
pub struct OptionDynamicImportsMap(Option<ResolvedVc<DynamicImportsMap>>);

#[turbo_tasks::value(transparent)]
pub struct DynamicImportedChunks(
Expand Down
8 changes: 4 additions & 4 deletions crates/next-api/src/entrypoints.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexMap, Vc};
use turbo_tasks::{FxIndexMap, ResolvedVc};

use crate::{
project::{Instrumentation, Middleware},
Expand All @@ -11,7 +11,7 @@ pub struct Entrypoints {
pub routes: FxIndexMap<RcStr, Route>,
pub middleware: Option<Middleware>,
pub instrumentation: Option<Instrumentation>,
pub pages_document_endpoint: Vc<Box<dyn Endpoint>>,
pub pages_app_endpoint: Vc<Box<dyn Endpoint>>,
pub pages_error_endpoint: Vc<Box<dyn Endpoint>>,
pub pages_document_endpoint: ResolvedVc<Box<dyn Endpoint>>,
pub pages_app_endpoint: ResolvedVc<Box<dyn Endpoint>>,
pub pages_error_endpoint: ResolvedVc<Box<dyn Endpoint>>,
}
6 changes: 3 additions & 3 deletions crates/next-api/src/global_module_id_strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ impl GlobalModuleIdStrategyBuilder {

let entrypoints = project.entrypoints().await?;

preprocessed_module_ids.push(preprocess_module_ids(entrypoints.pages_error_endpoint));
preprocessed_module_ids.push(preprocess_module_ids(entrypoints.pages_app_endpoint));
preprocessed_module_ids.push(preprocess_module_ids(entrypoints.pages_document_endpoint));
preprocessed_module_ids.push(preprocess_module_ids(*entrypoints.pages_error_endpoint));
preprocessed_module_ids.push(preprocess_module_ids(*entrypoints.pages_app_endpoint));
preprocessed_module_ids.push(preprocess_module_ids(*entrypoints.pages_document_endpoint));

if let Some(middleware) = &entrypoints.middleware {
preprocessed_module_ids.push(preprocess_module_ids(middleware.endpoint));
Expand Down
17 changes: 11 additions & 6 deletions crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,10 @@ impl Project {
pub async fn app_project(self: Vc<Self>) -> Result<Vc<OptionAppProject>> {
let app_dir = find_app_dir(self.project_path()).await?;

Ok(Vc::cell(
app_dir.map(|app_dir| AppProject::new(self, *app_dir)),
))
Ok(match *app_dir {
Some(app_dir) => Vc::cell(Some(AppProject::new(self, *app_dir).to_resolved().await?)),
None => Vc::cell(None),
})
}

#[turbo_tasks::function]
Expand Down Expand Up @@ -903,9 +904,13 @@ impl Project {
}
}

let pages_document_endpoint = self.pages_project().document_endpoint();
let pages_app_endpoint = self.pages_project().app_endpoint();
let pages_error_endpoint = self.pages_project().error_endpoint();
let pages_document_endpoint = self
.pages_project()
.document_endpoint()
.to_resolved()
.await?;
let pages_app_endpoint = self.pages_project().app_endpoint().to_resolved().await?;
let pages_error_endpoint = self.pages_project().error_endpoint().to_resolved().await?;

let middleware = self.find_middleware();
let middleware = if let FindContextFileResult::Found(..) = *middleware.await? {
Expand Down

0 comments on commit bcb28d3

Please sign in to comment.