Skip to content

Commit 7be071e

Browse files
committed
refactor(turbopack): Only perform strongly consistent reads/resolves on VcOperation
1 parent 6761881 commit 7be071e

File tree

24 files changed

+451
-279
lines changed

24 files changed

+451
-279
lines changed

crates/napi/src/next_api/endpoint.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,17 @@ impl Deref for ExternalEndpoint {
102102
// Await the source and return fatal issues if there are any, otherwise
103103
// propagate any actual error results.
104104
async fn strongly_consistent_catch_collectables<R: VcValueType + Send>(
105-
source: OperationVc<R>,
105+
source_op: OperationVc<R>,
106106
) -> Result<(
107107
Option<ReadRef<R>>,
108108
Arc<Vec<ReadRef<PlainIssue>>>,
109109
Arc<Vec<ReadRef<PlainDiagnostic>>>,
110110
Arc<Effects>,
111111
)> {
112-
let result = source.connect().strongly_consistent().await;
113-
let issues = get_issues(source).await?;
114-
let diagnostics = get_diagnostics(source).await?;
115-
let effects = Arc::new(get_effects(source).await?);
112+
let result = source_op.read_strongly_consistent().await;
113+
let issues = get_issues(source_op).await?;
114+
let diagnostics = get_diagnostics(source_op).await?;
115+
let effects = Arc::new(get_effects(source_op).await?);
116116

117117
let result = if result.is_err() && issues.iter().any(|i| i.severity <= IssueSeverity::Error) {
118118
None
@@ -131,13 +131,13 @@ struct WrittenEndpointWithIssues {
131131
effects: Arc<Effects>,
132132
}
133133

134-
#[turbo_tasks::function]
135-
async fn get_written_endpoint_with_issues(
134+
#[turbo_tasks::function(operation)]
135+
async fn get_written_endpoint_with_issues_operation(
136136
endpoint_op: OperationVc<Box<dyn Endpoint>>,
137137
) -> Result<Vc<WrittenEndpointWithIssues>> {
138-
let write_to_disk = endpoint_write_to_disk_operation(endpoint_op);
138+
let write_to_disk_op = endpoint_write_to_disk_operation(endpoint_op);
139139
let (written, issues, diagnostics, effects) =
140-
strongly_consistent_catch_collectables(write_to_disk).await?;
140+
strongly_consistent_catch_collectables(write_to_disk_op).await?;
141141
Ok(WrittenEndpointWithIssues {
142142
written,
143143
issues,
@@ -156,13 +156,16 @@ pub async fn endpoint_write_to_disk(
156156
let endpoint_op = ***endpoint;
157157
let (written, issues, diags) = turbo_tasks
158158
.run_once(async move {
159-
let operation = get_written_endpoint_with_issues(endpoint_op);
159+
let written_entrypoint_with_issues_op =
160+
get_written_endpoint_with_issues_operation(endpoint_op);
160161
let WrittenEndpointWithIssues {
161162
written,
162163
issues,
163164
diagnostics,
164165
effects,
165-
} = &*operation.strongly_consistent().await?;
166+
} = &*written_entrypoint_with_issues_op
167+
.read_strongly_consistent()
168+
.await?;
166169
effects.apply().await?;
167170

168171
Ok((written.clone(), issues.clone(), diagnostics.clone()))
@@ -189,8 +192,8 @@ pub fn endpoint_server_changed_subscribe(
189192
func,
190193
move || {
191194
async move {
192-
let vc = subscribe_issues_and_diags(endpoint, issues);
193-
let result = vc.strongly_consistent().await?;
195+
let issues_and_diags_op = subscribe_issues_and_diags_operation(endpoint, issues);
196+
let result = issues_and_diags_op.read_strongly_consistent().await?;
194197
result.effects.apply().await?;
195198
Ok(result)
196199
}
@@ -237,16 +240,16 @@ impl PartialEq for EndpointIssuesAndDiags {
237240

238241
impl Eq for EndpointIssuesAndDiags {}
239242

240-
#[turbo_tasks::function]
241-
async fn subscribe_issues_and_diags(
243+
#[turbo_tasks::function(operation)]
244+
async fn subscribe_issues_and_diags_operation(
242245
endpoint_op: OperationVc<Box<dyn Endpoint>>,
243246
should_include_issues: bool,
244247
) -> Result<Vc<EndpointIssuesAndDiags>> {
245-
let changed = endpoint_server_changed_operation(endpoint_op);
248+
let changed_op = endpoint_server_changed_operation(endpoint_op);
246249

247250
if should_include_issues {
248251
let (changed_value, issues, diagnostics, effects) =
249-
strongly_consistent_catch_collectables(changed).await?;
252+
strongly_consistent_catch_collectables(changed_op).await?;
250253
Ok(EndpointIssuesAndDiags {
251254
changed: changed_value,
252255
issues,
@@ -255,7 +258,7 @@ async fn subscribe_issues_and_diags(
255258
}
256259
.cell())
257260
} else {
258-
let changed_value = changed.connect().strongly_consistent().await?;
261+
let changed_value = changed_op.read_strongly_consistent().await?;
259262
Ok(EndpointIssuesAndDiags {
260263
changed: Some(changed_value),
261264
issues: Arc::new(vec![]),
@@ -266,22 +269,29 @@ async fn subscribe_issues_and_diags(
266269
}
267270
}
268271

272+
#[turbo_tasks::function(operation)]
273+
fn endpoint_client_changed_operation(
274+
endpoint_op: OperationVc<Box<dyn Endpoint>>,
275+
) -> Vc<Completion> {
276+
endpoint_op.connect().client_changed()
277+
}
278+
269279
#[napi(ts_return_type = "{ __napiType: \"RootTask\" }")]
270280
pub fn endpoint_client_changed_subscribe(
271281
#[napi(ts_arg_type = "{ __napiType: \"Endpoint\" }")] endpoint: External<ExternalEndpoint>,
272282
func: JsFunction,
273283
) -> napi::Result<External<RootTask>> {
274284
let turbo_tasks = endpoint.turbo_tasks().clone();
275-
let endpoint = ***endpoint;
285+
let endpoint_op = ***endpoint;
276286
subscribe(
277287
turbo_tasks,
278288
func,
279289
move || {
280290
async move {
281-
let changed = endpoint.connect().client_changed();
291+
let changed_op = endpoint_client_changed_operation(endpoint_op);
282292
// We don't capture issues and diagnostics here since we don't want to be
283293
// notified when they change
284-
changed.strongly_consistent().await?;
294+
let _ = changed_op.resolve_strongly_consistent().await?;
285295
Ok(())
286296
}
287297
.instrument(tracing::info_span!("client changes subscription"))

crates/napi/src/next_api/project.rs

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ pub async fn project_new(
408408
let container = turbo_tasks
409409
.run_once(async move {
410410
let project = ProjectContainer::new("next.js".into(), options.dev);
411-
let project = project.resolve().await?;
411+
let project = project.to_resolved().await?;
412412
project.initialize(options).await?;
413413
Ok(project)
414414
})
@@ -423,7 +423,7 @@ pub async fn project_new(
423423
Ok(External::new_with_size_hint(
424424
ProjectInstance {
425425
turbo_tasks,
426-
container,
426+
container: *container,
427427
exit_receiver: tokio::sync::Mutex::new(Some(exit_receiver)),
428428
},
429429
100,
@@ -659,16 +659,13 @@ struct EntrypointsWithIssues {
659659
effects: Arc<Effects>,
660660
}
661661

662-
#[turbo_tasks::function]
663-
async fn get_entrypoints_with_issues(
662+
#[turbo_tasks::function(operation)]
663+
async fn get_entrypoints_with_issues_operation(
664664
container: ResolvedVc<ProjectContainer>,
665665
) -> Result<Vc<EntrypointsWithIssues>> {
666666
let entrypoints_operation =
667667
EntrypointsOperation::new(project_container_entrypoints_operation(container));
668-
let entrypoints = entrypoints_operation
669-
.connect()
670-
.strongly_consistent()
671-
.await?;
668+
let entrypoints = entrypoints_operation.read_strongly_consistent().await?;
672669
let issues = get_issues(entrypoints_operation).await?;
673670
let diagnostics = get_diagnostics(entrypoints_operation).await?;
674671
let effects = Arc::new(get_effects(entrypoints_operation).await?);
@@ -702,13 +699,16 @@ pub fn project_entrypoints_subscribe(
702699
func,
703700
move || {
704701
async move {
705-
let operation = get_entrypoints_with_issues(container);
702+
let entrypoints_with_issues_op =
703+
get_entrypoints_with_issues_operation(container.to_resolved().await?);
706704
let EntrypointsWithIssues {
707705
entrypoints,
708706
issues,
709707
diagnostics,
710708
effects,
711-
} = &*operation.strongly_consistent().await?;
709+
} = &*entrypoints_with_issues_op
710+
.read_strongly_consistent()
711+
.await?;
712712
effects.apply().await?;
713713
Ok((entrypoints.clone(), issues.clone(), diagnostics.clone()))
714714
}
@@ -771,17 +771,17 @@ struct HmrUpdateWithIssues {
771771
effects: Arc<Effects>,
772772
}
773773

774-
#[turbo_tasks::function]
775-
async fn hmr_update(
774+
#[turbo_tasks::function(operation)]
775+
async fn hmr_update_with_issues_operation(
776776
project: ResolvedVc<Project>,
777777
identifier: RcStr,
778778
state: ResolvedVc<VersionState>,
779779
) -> Result<Vc<HmrUpdateWithIssues>> {
780-
let update_operation = project_hmr_update_operation(project, identifier, state);
781-
let update = update_operation.connect().strongly_consistent().await?;
782-
let issues = get_issues(update_operation).await?;
783-
let diagnostics = get_diagnostics(update_operation).await?;
784-
let effects = Arc::new(get_effects(update_operation).await?);
780+
let update_op = project_hmr_update_operation(project, identifier, state);
781+
let update = update_op.read_strongly_consistent().await?;
782+
let issues = get_issues(update_op).await?;
783+
let diagnostics = get_diagnostics(update_op).await?;
784+
let effects = Arc::new(get_effects(update_op).await?);
785785
Ok(HmrUpdateWithIssues {
786786
update,
787787
issues,
@@ -819,11 +819,15 @@ pub fn project_hmr_events(
819819
let identifier: RcStr = outer_identifier.clone().into();
820820
let session = session.clone();
821821
async move {
822-
let project = project.project().resolve().await?;
823-
let state = project.hmr_version_state(identifier.clone(), session);
824-
825-
let operation = hmr_update(project, identifier.clone(), state);
826-
let update = operation.strongly_consistent().await?;
822+
let project = project.project().to_resolved().await?;
823+
let state = project
824+
.hmr_version_state(identifier.clone(), session)
825+
.to_resolved()
826+
.await?;
827+
828+
let update_op =
829+
hmr_update_with_issues_operation(project, identifier.clone(), state);
830+
let update = update_op.read_strongly_consistent().await?;
827831
let HmrUpdateWithIssues {
828832
update,
829833
issues,
@@ -898,18 +902,15 @@ struct HmrIdentifiersWithIssues {
898902
effects: Arc<Effects>,
899903
}
900904

901-
#[turbo_tasks::function]
902-
async fn get_hmr_identifiers_with_issues(
905+
#[turbo_tasks::function(operation)]
906+
async fn get_hmr_identifiers_with_issues_operation(
903907
container: ResolvedVc<ProjectContainer>,
904908
) -> Result<Vc<HmrIdentifiersWithIssues>> {
905-
let hmr_identifiers_operation = project_container_hmr_identifiers_operation(container);
906-
let hmr_identifiers = hmr_identifiers_operation
907-
.connect()
908-
.strongly_consistent()
909-
.await?;
910-
let issues = get_issues(hmr_identifiers_operation).await?;
911-
let diagnostics = get_diagnostics(hmr_identifiers_operation).await?;
912-
let effects = Arc::new(get_effects(hmr_identifiers_operation).await?);
909+
let hmr_identifiers_op = project_container_hmr_identifiers_operation(container);
910+
let hmr_identifiers = hmr_identifiers_op.read_strongly_consistent().await?;
911+
let issues = get_issues(hmr_identifiers_op).await?;
912+
let diagnostics = get_diagnostics(hmr_identifiers_op).await?;
913+
let effects = Arc::new(get_effects(hmr_identifiers_op).await?);
913914
Ok(HmrIdentifiersWithIssues {
914915
identifiers: hmr_identifiers,
915916
issues,
@@ -937,13 +938,16 @@ pub fn project_hmr_identifiers_subscribe(
937938
turbo_tasks.clone(),
938939
func,
939940
move || async move {
940-
let operation = get_hmr_identifiers_with_issues(container);
941+
let hmr_identifiers_with_issues_op =
942+
get_hmr_identifiers_with_issues_operation(container.to_resolved().await?);
941943
let HmrIdentifiersWithIssues {
942944
identifiers,
943945
issues,
944946
diagnostics,
945947
effects,
946-
} = &*operation.strongly_consistent().await?;
948+
} = &*hmr_identifiers_with_issues_op
949+
.read_strongly_consistent()
950+
.await?;
947951
effects.apply().await?;
948952

949953
Ok((identifiers.clone(), issues.clone(), diagnostics.clone()))

crates/next-api/src/module_graph.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -587,10 +587,12 @@ pub async fn get_reduced_graphs_for_endpoint(
587587
// TODO get rid of this function once everything inside of
588588
// `get_reduced_graphs_for_endpoint_inner` calls `take_collectibles()` when needed
589589
let result_op = get_reduced_graphs_for_endpoint_inner_operation(module_graph, is_single_page);
590-
let result_vc = result_op.connect();
591-
if !is_single_page {
592-
result_vc.strongly_consistent().await?;
590+
let result_vc = if !is_single_page {
591+
let result_vc = result_op.resolve_strongly_consistent().await?;
593592
let _issues = result_op.take_collectibles::<Box<dyn Issue>>();
594-
}
593+
*result_vc
594+
} else {
595+
result_op.connect()
596+
};
595597
Ok(result_vc)
596598
}

crates/next-api/src/project.rs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -273,23 +273,37 @@ impl ProjectContainer {
273273
}
274274
}
275275

276+
#[turbo_tasks::function(operation)]
277+
fn project_fs_operation(project: ResolvedVc<Project>) -> Vc<DiskFileSystem> {
278+
project.project_fs()
279+
}
280+
281+
#[turbo_tasks::function(operation)]
282+
fn output_fs_operation(project: ResolvedVc<Project>) -> Vc<DiskFileSystem> {
283+
project.project_fs()
284+
}
285+
276286
impl ProjectContainer {
277287
#[tracing::instrument(level = "info", name = "initialize project", skip_all)]
278-
pub async fn initialize(self: Vc<Self>, options: ProjectOptions) -> Result<()> {
288+
pub async fn initialize(self: ResolvedVc<Self>, options: ProjectOptions) -> Result<()> {
279289
let watch = options.watch;
280290

281291
self.await?.options_state.set(Some(options));
282292

283-
let project = self.project();
284-
let project_fs = project.project_fs().strongly_consistent().await?;
293+
let project = self.project().to_resolved().await?;
294+
let project_fs = project_fs_operation(project)
295+
.read_strongly_consistent()
296+
.await?;
285297
if watch.enable {
286298
project_fs
287299
.start_watching_with_invalidation_reason(watch.poll_interval)
288300
.await?;
289301
} else {
290302
project_fs.invalidate_with_reason();
291303
}
292-
let output_fs = project.output_fs().strongly_consistent().await?;
304+
let output_fs = output_fs_operation(project)
305+
.read_strongly_consistent()
306+
.await?;
293307
output_fs.invalidate_with_reason();
294308
Ok(())
295309
}
@@ -355,13 +369,22 @@ impl ProjectContainer {
355369
// TODO: Handle mode switch, should prevent mode being switched.
356370
let watch = new_options.watch;
357371

358-
let project = self.project();
359-
let prev_project_fs = project.project_fs().strongly_consistent().await?;
360-
let prev_output_fs = project.output_fs().strongly_consistent().await?;
372+
let project = self.project().to_resolved().await?;
373+
let prev_project_fs = project_fs_operation(project)
374+
.read_strongly_consistent()
375+
.await?;
376+
let prev_output_fs = output_fs_operation(project)
377+
.read_strongly_consistent()
378+
.await?;
361379

362380
this.options_state.set(Some(new_options));
363-
let project_fs = project.project_fs().strongly_consistent().await?;
364-
let output_fs = project.output_fs().strongly_consistent().await?;
381+
let project = self.project().to_resolved().await?;
382+
let project_fs = project_fs_operation(project)
383+
.read_strongly_consistent()
384+
.await?;
385+
let output_fs = output_fs_operation(project)
386+
.read_strongly_consistent()
387+
.await?;
365388

366389
if !ReadRef::ptr_eq(&prev_project_fs, &project_fs) {
367390
if watch.enable {
@@ -868,11 +891,10 @@ impl Project {
868891
#[turbo_tasks::function]
869892
pub async fn whole_app_module_graphs(self: ResolvedVc<Self>) -> Result<Vc<ModuleGraphs>> {
870893
async move {
871-
let operation = whole_app_module_graph_operation(self);
872-
let module_graphs = operation.connect();
873-
let _ = module_graphs.resolve_strongly_consistent().await?;
874-
let _ = operation.take_issues_with_path().await?;
875-
Ok(module_graphs)
894+
let module_graphs_op = whole_app_module_graph_operation(self);
895+
let module_graphs_vc = module_graphs_op.resolve_strongly_consistent().await?;
896+
let _ = module_graphs_op.take_issues_with_path().await?;
897+
Ok(*module_graphs_vc)
876898
}
877899
.instrument(tracing::info_span!("module graph for app"))
878900
.await

0 commit comments

Comments
 (0)