Repository: vercel/next.js
Pull request: perf(turbopack): Decode sourcemap partially

### What?

Use the forked version of `sourcemap`. We can improve performance by utilizing https://github.com/kdy1/rust-sourcemap/pull/1

### Why?

It's much faster. 3% ~ 12% build performance improvement

![image](https://github.com/user-attachments/assets/f664fc4c-4ce2-4b2c-ab29-3a860ed24da7)


### How?



Closes PACK-4791

Changed files:
filename: Cargo.lock
patch: @@ -7164,8 +7164,7 @@ dependencies = [
 [[package]]
 name = "sourcemap"
 version = "9.2.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e22afbcb92ce02d23815b9795523c005cb9d3c214f8b7a66318541c240ea7935"
+source = "git+https://github.com/kdy1/rust-sourcemap?branch=main#804d663521d6109b3746d800918d483b97e8e108"
 dependencies = [
  "base64-simd 0.8.0",
  "bitvec",
@@ -10097,6 +10096,7 @@ version = "0.1.0"
 dependencies = [
  "anyhow",
  "async-trait",
+ "bitvec",
  "codspeed-criterion-compat",
  "data-encoding",
  "either",

filename: Cargo.toml
patch: @@ -421,3 +421,5 @@ vergen = { version = "9.0.6", features = ["cargo"] }
 vergen-gitcl = { version = "1.0.8", features = ["cargo"] }
 webbrowser = "0.8.7"
 
+[patch.crates-io]
+sourcemap = { git = "https://github.com/kdy1/rust-sourcemap", branch = "main" }

filename: turbopack/crates/turbopack-ecmascript/Cargo.toml
patch: @@ -28,7 +28,7 @@ petgraph = { workspace = true }
 regex = { workspace = true }
 rustc-hash = { workspace = true }
 serde = { workspace = true }
-serde_json = { workspace = true }
+serde_json = { workspace = true, features = ["raw_value"] }
 sourcemap = { workspace = true }
 smallvec = { workspace = true }
 strsim = { workspace = true }
@@ -71,6 +71,7 @@ swc_core = { workspace = true, features = [
   "testing",
   "base",
 ] }
+bitvec = "1.0.1"
 
 [dev-dependencies]
 criterion = { workspace = true, features = ["async_tokio"] }

filename: turbopack/crates/turbopack-ecmascript/src/parse.rs
patch: @@ -105,18 +105,14 @@ pub fn generate_js_source_map(
     original_source_map: Option<&Rope>,
     inline_sources_content: bool,
 ) -> Result<Rope> {
-    let input_map = if let Some(original_source_map) = original_source_map {
-        Some(match sourcemap::decode(original_source_map.read())? {
-            sourcemap::DecodedMap::Regular(source_map) => source_map,
-            // swc only accepts flattened sourcemaps as input
-            sourcemap::DecodedMap::Index(source_map_index) => source_map_index.flatten()?,
-            _ => return Err(sourcemap::Error::IncompatibleSourceMap.into()),
-        })
+    let original_source_map = original_source_map.map(|x| x.to_bytes());
+    let input_map = if let Some(original_source_map) = &original_source_map {
+        Some(sourcemap::lazy::decode(original_source_map)?.into_source_map()?)
     } else {
         None
     };
 
-    let map = files_map.build_source_map(
+    let new_mappings = files_map.build_source_map(
         &mappings,
         None,
         InlineSourcesContentConfig {
@@ -130,18 +126,29 @@ pub fn generate_js_source_map(
         },
     );
 
-    let mut map = match input_map {
-        Some(mut input_map) => {
-            input_map.adjust_mappings(&map);
-            input_map
+    match input_map {
+        Some(mut map) => {
+            // TODO: Make this more efficient
+            map.adjust_mappings(new_mappings);
+
+            // TODO: Enable this when we have a way to handle the ignore list
+            // add_default_ignore_list(&mut map);
+            let map = map.into_raw_sourcemap();
+            let result = serde_json::to_vec(&map)?;
+            Ok(Rope::from(result))
         }
-        None => map,
-    };
-    add_default_ignore_list(&mut map);
+        None => {
+            // We don't convert sourcemap::SourceMap into raw_sourcemap::SourceMap because we don't
+            // need to adjust mappings
+            let mut map = new_mappings;
+
+            add_default_ignore_list(&mut map);
 
-    let mut result = vec![];
-    map.to_writer(&mut result)?;
-    Ok(Rope::from(result))
+            let mut result = vec![];
+            map.to_writer(&mut result)?;
+            Ok(Rope::from(result))
+        }
+    }
 }
 
 /// A config to generate a source map which includes the source content of every