Skip to content

Commit a50782c

Browse files
authored
Turbopack: make IssueSource a TaskInput (#75542)
Meaning fewer cells and fewer tasks No measureable perf difference
1 parent d70cac3 commit a50782c

File tree

26 files changed

+174
-213
lines changed

26 files changed

+174
-213
lines changed

crates/napi/src/next_api/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ impl From<&PlainIssue> for NapiIssue {
289289
.map(|styled| serde_json::to_value(StyledStringSerialize::from(styled)).unwrap()),
290290
documentation_link: issue.documentation_link.to_string(),
291291
severity: issue.severity.as_str().to_string(),
292-
source: issue.source.as_deref().map(|source| source.into()),
292+
source: issue.source.as_ref().map(|source| source.into()),
293293
title: serde_json::to_value(StyledStringSerialize::from(&issue.title)).unwrap(),
294294
sub_issues: issue
295295
.sub_issues

crates/next-core/src/app_segment_config.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ impl NextSegmentConfig {
177177
pub struct NextSegmentConfigParsingIssue {
178178
ident: ResolvedVc<AssetIdent>,
179179
detail: ResolvedVc<StyledString>,
180-
source: ResolvedVc<IssueSource>,
180+
source: IssueSource,
181181
}
182182

183183
#[turbo_tasks::value_impl]
@@ -186,7 +186,7 @@ impl NextSegmentConfigParsingIssue {
186186
pub fn new(
187187
ident: ResolvedVc<AssetIdent>,
188188
detail: ResolvedVc<StyledString>,
189-
source: ResolvedVc<IssueSource>,
189+
source: IssueSource,
190190
) -> Vc<Self> {
191191
Self {
192192
ident,
@@ -249,15 +249,15 @@ impl Issue for NextSegmentConfigParsingIssue {
249249
Ok(Vc::cell(Some(
250250
self.source
251251
.resolve_source_map(self.ident.path())
252-
.to_resolved()
253-
.await?,
252+
.await?
253+
.into_owned(),
254254
)))
255255
}
256256
}
257257

258258
#[turbo_tasks::function]
259259
pub async fn parse_segment_config_from_source(
260-
source: Vc<Box<dyn Source>>,
260+
source: ResolvedVc<Box<dyn Source>>,
261261
) -> Result<Vc<NextSegmentConfig>> {
262262
let path = source.ident().path().await?;
263263

@@ -273,7 +273,7 @@ pub async fn parse_segment_config_from_source(
273273
}
274274

275275
let result = &*parse(
276-
source,
276+
*source,
277277
turbo_tasks::Value::new(if path.path.ends_with(".ts") {
278278
EcmascriptModuleAssetType::Typescript {
279279
tsx: false,
@@ -348,20 +348,20 @@ pub async fn parse_segment_config_from_source(
348348
Ok(config.cell())
349349
}
350350

351-
fn issue_source(source: Vc<Box<dyn Source>>, span: Span) -> Vc<IssueSource> {
351+
fn issue_source(source: ResolvedVc<Box<dyn Source>>, span: Span) -> IssueSource {
352352
IssueSource::from_swc_offsets(source, span.lo.to_usize(), span.hi.to_usize())
353353
}
354354

355355
async fn parse_config_value(
356-
source: Vc<Box<dyn Source>>,
356+
source: ResolvedVc<Box<dyn Source>>,
357357
config: &mut NextSegmentConfig,
358358
ident: &Ident,
359359
init: &Expr,
360360
eval_context: &EvalContext,
361361
) -> Result<()> {
362362
let span = init.span();
363363
async fn invalid_config(
364-
source: Vc<Box<dyn Source>>,
364+
source: ResolvedVc<Box<dyn Source>>,
365365
span: Span,
366366
detail: &str,
367367
value: &JsValue,

turbopack/crates/turbopack-core/src/issue/analyze.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct AnalyzeIssue {
1616
pub title: ResolvedVc<RcStr>,
1717
pub message: ResolvedVc<StyledString>,
1818
pub code: Option<RcStr>,
19-
pub source: Option<ResolvedVc<IssueSource>>,
19+
pub source: Option<IssueSource>,
2020
}
2121

2222
#[turbo_tasks::value_impl]
@@ -28,7 +28,7 @@ impl AnalyzeIssue {
2828
title: ResolvedVc<RcStr>,
2929
message: ResolvedVc<StyledString>,
3030
code: Option<RcStr>,
31-
source: Option<ResolvedVc<IssueSource>>,
31+
source: Option<IssueSource>,
3232
) -> Vc<Self> {
3333
Self {
3434
severity,
@@ -81,12 +81,12 @@ impl Issue for AnalyzeIssue {
8181

8282
#[turbo_tasks::function]
8383
async fn source(&self) -> Result<Vc<OptionIssueSource>> {
84-
Ok(Vc::cell(match self.source {
84+
Ok(Vc::cell(match &self.source {
8585
Some(source) => Some(
8686
source
8787
.resolve_source_map(self.source_ident.path())
88-
.to_resolved()
89-
.await?,
88+
.await?
89+
.into_owned(),
9090
),
9191
None => None,
9292
}))

turbopack/crates/turbopack-core/src/issue/mod.rs

Lines changed: 47 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ pub mod module;
44
pub mod resolve;
55

66
use std::{
7+
borrow::Cow,
78
cmp::{min, Ordering},
89
fmt::{Display, Formatter},
910
};
1011

1112
use anyhow::{anyhow, Result};
1213
use async_trait::async_trait;
1314
use auto_hash_map::AutoSet;
14-
use serde::Serialize;
15+
use serde::{Deserialize, Serialize};
1516
use turbo_rcstr::RcStr;
1617
use turbo_tasks::{
17-
emit, CollectiblesSource, OperationVc, RawVc, ReadRef, ResolvedVc, TransientInstance,
18-
TransientValue, TryJoinIterExt, Upcast, ValueToString, Vc,
18+
emit, trace::TraceRawVcs, CollectiblesSource, NonLocalValue, OperationVc, RawVc, ReadRef,
19+
ResolvedVc, TaskInput, TransientInstance, TransientValue, TryJoinIterExt, Upcast,
20+
ValueToString, Vc,
1921
};
2022
use turbo_tasks_fs::{FileContent, FileLine, FileLinesContent, FileSystemPath};
2123
use turbo_tasks_hash::{DeterministicHash, Xxh3Hash64Hasher};
@@ -172,7 +174,7 @@ pub trait Issue {
172174
detail,
173175
documentation_link: self.documentation_link().await?.clone_value(),
174176
source: {
175-
if let Some(s) = *self.source().await? {
177+
if let Some(s) = &*self.source().await? {
176178
Some(s.into_plain().await?)
177179
} else {
178180
None
@@ -435,79 +437,71 @@ impl CapturedIssues {
435437
}
436438
}
437439

438-
#[turbo_tasks::value]
439-
#[derive(Clone, Debug)]
440+
#[derive(
441+
Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash, TaskInput, TraceRawVcs, NonLocalValue,
442+
)]
440443
pub struct IssueSource {
441444
source: ResolvedVc<Box<dyn Source>>,
442-
range: Option<ResolvedVc<SourceRange>>,
445+
range: Option<SourceRange>,
443446
}
444447

445448
/// The end position is the first character after the range
446-
#[turbo_tasks::value]
447-
#[derive(Clone, Debug)]
449+
#[derive(
450+
Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash, TaskInput, TraceRawVcs, NonLocalValue,
451+
)]
448452
enum SourceRange {
449453
LineColumn(SourcePos, SourcePos),
450454
ByteOffset(usize, usize),
451455
}
452456

453-
#[turbo_tasks::value_impl]
454457
impl IssueSource {
455458
// Sometimes we only have the source file that causes an issue, not the
456459
// exact location, such as as in some generated code.
457-
#[turbo_tasks::function]
458-
pub fn from_source_only(source: ResolvedVc<Box<dyn Source>>) -> Vc<Self> {
459-
Self::cell(IssueSource {
460+
pub fn from_source_only(source: ResolvedVc<Box<dyn Source>>) -> Self {
461+
IssueSource {
460462
source,
461463
range: None,
462-
})
464+
}
463465
}
464466

465-
#[turbo_tasks::function]
466467
pub fn from_line_col(
467468
source: ResolvedVc<Box<dyn Source>>,
468469
start: SourcePos,
469470
end: SourcePos,
470-
) -> Vc<Self> {
471-
Self::cell(IssueSource {
471+
) -> Self {
472+
IssueSource {
472473
source,
473-
range: Some(SourceRange::LineColumn(start, end).resolved_cell()),
474-
})
474+
range: Some(SourceRange::LineColumn(start, end)),
475+
}
475476
}
476477

477-
#[turbo_tasks::function]
478-
pub async fn resolve_source_map(
479-
self: Vc<Self>,
480-
origin: Vc<FileSystemPath>,
481-
) -> Result<Vc<Self>> {
482-
let this = self.await?;
483-
484-
if let Some(range) = this.range {
485-
let (start, end) = match &*range.await? {
478+
pub async fn resolve_source_map(&self, origin: Vc<FileSystemPath>) -> Result<Cow<'_, Self>> {
479+
if let Some(range) = &self.range {
480+
let (start, end) = match range {
486481
SourceRange::LineColumn(start, end) => (*start, *end),
487-
488482
SourceRange::ByteOffset(start, end) => {
489-
if let FileLinesContent::Lines(lines) = &*this.source.content().lines().await? {
483+
if let FileLinesContent::Lines(lines) = &*self.source.content().lines().await? {
490484
let start = find_line_and_column(lines.as_ref(), *start);
491485
let end = find_line_and_column(lines.as_ref(), *end);
492486
(start, end)
493487
} else {
494-
return Ok(self);
488+
return Ok(Cow::Borrowed(self));
495489
}
496490
}
497491
};
498492

499493
// If we have a source map, map the line/column to the original source.
500-
let mapped = source_pos(this.source, origin, start, end).await?;
494+
let mapped = source_pos(self.source, origin, start, end).await?;
501495

502496
if let Some((source, start, end)) = mapped {
503-
return Ok(Self::cell(IssueSource {
497+
return Ok(Cow::Owned(IssueSource {
504498
source,
505-
range: Some(SourceRange::LineColumn(start, end).resolved_cell()),
499+
range: Some(SourceRange::LineColumn(start, end)),
506500
}));
507501
}
508502
}
509503

510-
Ok(self)
504+
Ok(Cow::Borrowed(self))
511505
}
512506

513507
/// Create a [`IssueSource`] from byte offsets given by an swc ast node
@@ -518,26 +512,18 @@ impl IssueSource {
518512
/// * `source`: The source code in which to look up the byte offsets.
519513
/// * `start`: The start index of the span. Must use **1-based** indexing.
520514
/// * `end`: The end index of the span. Must use **1-based** indexing.
521-
#[turbo_tasks::function]
522-
pub fn from_swc_offsets(
523-
source: ResolvedVc<Box<dyn Source>>,
524-
start: usize,
525-
end: usize,
526-
) -> Vc<Self> {
527-
Self::cell(IssueSource {
515+
pub fn from_swc_offsets(source: ResolvedVc<Box<dyn Source>>, start: usize, end: usize) -> Self {
516+
IssueSource {
528517
source,
529518
range: match (start == 0, end == 0) {
530519
(true, true) => None,
531-
(false, false) => Some(SourceRange::ByteOffset(start - 1, end - 1).resolved_cell()),
532-
(false, true) => {
533-
Some(SourceRange::ByteOffset(start - 1, start - 1).resolved_cell())
534-
}
535-
(true, false) => Some(SourceRange::ByteOffset(end - 1, end - 1).resolved_cell()),
520+
(false, false) => Some(SourceRange::ByteOffset(start - 1, end - 1)),
521+
(false, true) => Some(SourceRange::ByteOffset(start - 1, start - 1)),
522+
(true, false) => Some(SourceRange::ByteOffset(end - 1, end - 1)),
536523
},
537-
})
524+
}
538525
}
539526

540-
#[turbo_tasks::function]
541527
/// Returns an `IssueSource` representing a span of code in the `source`.
542528
/// Positions are derived from byte offsets and stored as lines and columns.
543529
/// Requires a binary search of the source text to perform this.
@@ -551,21 +537,20 @@ impl IssueSource {
551537
source: ResolvedVc<Box<dyn Source>>,
552538
start: usize,
553539
end: usize,
554-
) -> Result<Vc<Self>> {
555-
Ok(Self::cell(IssueSource {
540+
) -> Result<Self> {
541+
Ok(IssueSource {
556542
source,
557543
range: if let FileLinesContent::Lines(lines) = &*source.content().lines().await? {
558544
let start = find_line_and_column(lines.as_ref(), start);
559545
let end = find_line_and_column(lines.as_ref(), end);
560-
Some(SourceRange::LineColumn(start, end).resolved_cell())
546+
Some(SourceRange::LineColumn(start, end))
561547
} else {
562548
None
563549
},
564-
}))
550+
})
565551
}
566552

567553
/// Returns the file path for the source file.
568-
#[turbo_tasks::function]
569554
pub fn file_path(&self) -> Vc<FileSystemPath> {
570555
self.source.ident().path()
571556
}
@@ -574,8 +559,8 @@ impl IssueSource {
574559
impl IssueSource {
575560
/// Returns bytes offsets corresponding the source range in the format used by swc's Spans.
576561
pub async fn to_swc_offsets(&self) -> Result<Option<(usize, usize)>> {
577-
Ok(match self.range {
578-
Some(range) => match &*range.await? {
562+
Ok(match &self.range {
563+
Some(range) => match range {
579564
SourceRange::ByteOffset(start, end) => Some((*start + 1, *end + 1)),
580565
SourceRange::LineColumn(start, end) => {
581566
if let FileLinesContent::Lines(lines) = &*self.source.content().lines().await? {
@@ -647,7 +632,7 @@ async fn source_pos(
647632
}
648633

649634
#[turbo_tasks::value(transparent)]
650-
pub struct OptionIssueSource(Option<ResolvedVc<IssueSource>>);
635+
pub struct OptionIssueSource(Option<IssueSource>);
651636

652637
#[turbo_tasks::value(transparent)]
653638
pub struct OptionStyledString(Option<ResolvedVc<StyledString>>);
@@ -707,7 +692,7 @@ pub struct PlainIssue {
707692
pub detail: Option<StyledString>,
708693
pub documentation_link: RcStr,
709694

710-
pub source: Option<ReadRef<PlainIssueSource>>,
695+
pub source: Option<PlainIssueSource>,
711696
pub sub_issues: Vec<ReadRef<PlainIssue>>,
712697
pub processing_path: ReadRef<PlainIssueProcessingPath>,
713698
}
@@ -779,14 +764,12 @@ pub struct PlainIssueSource {
779764
pub range: Option<(SourcePos, SourcePos)>,
780765
}
781766

782-
#[turbo_tasks::value_impl]
783767
impl IssueSource {
784-
#[turbo_tasks::function]
785-
pub async fn into_plain(&self) -> Result<Vc<PlainIssueSource>> {
768+
pub async fn into_plain(&self) -> Result<PlainIssueSource> {
786769
Ok(PlainIssueSource {
787770
asset: PlainSource::from_source(*self.source).await?,
788-
range: match self.range {
789-
Some(range) => match &*range.await? {
771+
range: match &self.range {
772+
Some(range) => match range {
790773
SourceRange::LineColumn(start, end) => Some((*start, *end)),
791774
SourceRange::ByteOffset(start, end) => {
792775
if let FileLinesContent::Lines(lines) =
@@ -802,8 +785,7 @@ impl IssueSource {
802785
},
803786
_ => None,
804787
},
805-
}
806-
.cell())
788+
})
807789
}
808790
}
809791

turbopack/crates/turbopack-core/src/issue/resolve.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub struct ResolvingIssue {
2323
pub file_path: ResolvedVc<FileSystemPath>,
2424
pub resolve_options: ResolvedVc<ResolveOptions>,
2525
pub error_message: Option<String>,
26-
pub source: Option<ResolvedVc<IssueSource>>,
26+
pub source: Option<IssueSource>,
2727
}
2828

2929
#[turbo_tasks::value_impl]
@@ -122,12 +122,12 @@ impl Issue for ResolvingIssue {
122122

123123
#[turbo_tasks::function]
124124
async fn source(&self) -> Result<Vc<OptionIssueSource>> {
125-
Ok(Vc::cell(match self.source {
125+
Ok(Vc::cell(match &self.source {
126126
Some(source) => Some(
127127
source
128128
.resolve_source_map(*self.file_path)
129-
.to_resolved()
130-
.await?,
129+
.await?
130+
.into_owned(),
131131
),
132132
None => None,
133133
}))

0 commit comments

Comments
 (0)