Skip to content

Commit 4bfc429

Browse files
committed
avoid cloning SourceMap for ignore list
1 parent d2711d2 commit 4bfc429

File tree

8 files changed

+45
-50
lines changed

8 files changed

+45
-50
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ clap = { version = "4.5.2", features = ["derive"] }
133133
concurrent-queue = "2.5.0"
134134
console = "0.15.5"
135135
console-subscriber = "0.4.1"
136+
const_format = "0.2.30"
136137
criterion = "0.5.1"
137138
crossbeam-channel = "0.5.8"
138139
dashmap = "6.1.0"

turbopack/crates/turbopack-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ anyhow = { workspace = true }
1717
async-trait = { workspace = true }
1818
auto-hash-map = { workspace = true }
1919
browserslist-rs = { workspace = true }
20+
const_format = { workspace = true }
2021
either = { workspace = true }
2122
futures = { workspace = true }
2223
indexmap = { workspace = true }

turbopack/crates/turbopack-core/src/code_builder.rs

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
};
66

77
use anyhow::{Context, Result};
8-
use indexmap::{IndexMap, IndexSet};
8+
use indexmap::IndexMap;
99
use turbo_tasks::{ResolvedVc, Vc};
1010
use turbo_tasks_fs::{
1111
rope::{Rope, RopeBuilder},
@@ -181,38 +181,14 @@ impl GenerateSourceMap for Code {
181181
last_byte_pos = *byte_pos;
182182

183183
let encoded = match map {
184-
None => SourceMap::empty(),
184+
None => SourceMap::empty().to_resolved().await?,
185185
Some(map) => match *map.generate_source_map().await? {
186-
None => SourceMap::empty(),
187-
Some(map) => {
188-
let map = &*map.await?;
189-
let map = map.to_source_map().await?;
190-
match map.as_regular_source_map() {
191-
None => SourceMap::empty(),
192-
Some(map) => {
193-
let mut map = map.into_owned();
194-
let mut ignored_ids = IndexSet::new();
195-
for (src_id, src) in map.sources().enumerate() {
196-
if src.starts_with("turbopack://[next]")
197-
|| src.starts_with("turbopack://[turbopack]")
198-
|| src.contains("/node_modules/")
199-
{
200-
ignored_ids.insert(src_id);
201-
}
202-
}
203-
204-
for ignored_id in ignored_ids {
205-
map.add_to_ignore_list(ignored_id as _);
206-
}
207-
208-
SourceMap::new_decoded(sourcemap::DecodedMap::Regular(map)).cell()
209-
}
210-
}
211-
}
186+
None => SourceMap::empty().to_resolved().await?,
187+
Some(map) => map,
212188
},
213189
};
214190

215-
sections.push(SourceMapSection::new(pos, encoded.to_resolved().await?))
191+
sections.push(SourceMapSection::new(pos, encoded))
216192
}
217193

218194
Ok(Vc::cell(Some(

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

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::{borrow::Cow, io::Write, ops::Deref, sync::Arc};
22

33
use anyhow::Result;
4-
use indexmap::IndexSet;
54
use once_cell::sync::Lazy;
65
use ref_cast::RefCast;
76
use regex::Regex;
@@ -15,11 +14,12 @@ use turbo_tasks_fs::{
1514
};
1615

1716
use crate::{
18-
asset::AssetContent, source::Source, source_pos::SourcePos, virtual_source::VirtualSource,
19-
SOURCE_MAP_PREFIX,
17+
asset::AssetContent, source::Source, source_map::utils::add_default_ignore_list,
18+
source_pos::SourcePos, virtual_source::VirtualSource, SOURCE_MAP_PREFIX,
2019
};
2120

2221
pub(crate) mod source_map_asset;
22+
pub mod utils;
2323

2424
pub use source_map_asset::SourceMapAsset;
2525

@@ -200,6 +200,11 @@ impl SourceMap {
200200
SourceMap::Decoded(InnerSourceMap::new(map))
201201
}
202202

203+
/// Creates a new SourceMap::Decoded Vc out of a [InnerSourceMap] instance.
204+
pub fn new_inner(map: InnerSourceMap) -> Self {
205+
SourceMap::Decoded(map)
206+
}
207+
203208
/// Creates a new SourceMap::Sectioned Vc out of a collection of source map
204209
/// sections.
205210
pub fn new_sectioned(sections: Vec<SourceMapSection>) -> Self {
@@ -416,28 +421,15 @@ impl SourceMap {
416421
.collect::<Vec<_>>();
417422
let mut new_sources = Vec::with_capacity(count);
418423
let mut new_source_contents = Vec::with_capacity(count);
419-
let mut ignored_sources = IndexSet::new();
420-
for (src_id, (source, source_content)) in sources
421-
.into_iter()
422-
.zip(source_contents.into_iter())
423-
.enumerate()
424-
{
424+
for (source, source_content) in sources.into_iter().zip(source_contents.into_iter()) {
425425
let (source, name) = resolve_source(source, source_content, origin).await?;
426-
if source.starts_with("turbopack://[next]")
427-
|| source.starts_with("turbopack://[turbopack]")
428-
|| source.contains("/node_modules/")
429-
{
430-
ignored_sources.insert(src_id);
431-
}
432426
new_sources.push(source);
433427
new_source_contents.push(Some(name));
434428
}
435429
let mut map =
436430
RegularMap::new(file, tokens, names, new_sources, Some(new_source_contents));
437431

438-
for ignored_source in ignored_sources {
439-
map.add_to_ignore_list(ignored_source as _);
440-
}
432+
add_default_ignore_list(&mut map);
441433

442434
Ok(map)
443435
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::collections::HashSet;
2+
3+
use const_format::concatcp;
4+
use sourcemap::SourceMap;
5+
6+
use crate::SOURCE_MAP_PREFIX;
7+
8+
pub fn add_default_ignore_list(map: &mut SourceMap) {
9+
let mut ignored_ids = HashSet::new();
10+
11+
for (source_id, source) in map.sources().enumerate() {
12+
if source.starts_with(concatcp!(SOURCE_MAP_PREFIX, "[next]"))
13+
|| source.starts_with(concatcp!(SOURCE_MAP_PREFIX, "[turbopack]"))
14+
|| source.contains("/node_modules/")
15+
{
16+
ignored_ids.insert(source_id);
17+
}
18+
}
19+
20+
for ignored_id in ignored_ids {
21+
map.add_to_ignore_list(ignored_id as _);
22+
}
23+
}

turbopack/crates/turbopack-ecmascript/src/parse.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use turbopack_core::{
3030
error::PrettyPrintError,
3131
issue::{Issue, IssueExt, IssueSeverity, IssueStage, OptionStyledString, StyledString},
3232
source::Source,
33-
source_map::{GenerateSourceMap, OptionSourceMap, SourceMap},
33+
source_map::{utils::add_default_ignore_list, GenerateSourceMap, OptionSourceMap, SourceMap},
3434
SOURCE_MAP_PREFIX,
3535
};
3636
use turbopack_swc_utils::emitter::IssueEmitter;
@@ -124,11 +124,12 @@ impl GenerateSourceMap for ParseResultSourceMap {
124124
} else {
125125
None
126126
};
127-
let map = self.files_map.build_source_map_with_config(
127+
let mut map = self.files_map.build_source_map_with_config(
128128
&self.mappings,
129129
input_map.as_deref(),
130130
InlineSourcesContentConfig {},
131131
);
132+
add_default_ignore_list(&mut map);
132133
Ok(Vc::cell(Some(SourceMap::new_regular(map).resolved_cell())))
133134
}
134135
}

turbopack/crates/turbopack-node/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ workspace = true
2020
anyhow = { workspace = true }
2121
async-stream = "0.3.4"
2222
async-trait = { workspace = true }
23-
const_format = "0.2.30"
23+
const_format = { workspace = true }
2424
either = { workspace = true, features = ["serde"] }
2525
futures = { workspace = true }
2626
futures-retry = { workspace = true }

0 commit comments

Comments
 (0)