Unverified Commit af3dad83 authored by Sam Poder's avatar Sam Poder Committed by GitHub

[turbopack] Fix de-opt in getChunkRelativeUrl() (#96183)

Used Claude + this tool (https://github.com/vercel/next.js/pull/96158)
to look at Turbopack's runtime. With a large fixture with a lot of
imports, this part was bailing out:

```
{
  "kind": "deopt",
  "bailoutType": "deopt-eager",
  "reason": "wrong map",
  "functionName": "getChunkRelativeUrl",
  "original": {
    "source": "turbopack:///[turbopack]/browser/runtime/base/runtime-base.ts",
    "line": 486,
    "column": 11
  },
  "severity": "high"
}
```

After the fix, this went away.

---------
Co-authored-by: 's avatarLuke Sandberg <lukesandberg@users.noreply.github.com>
parent af9a3928
...@@ -475,6 +475,12 @@ function instantiateRuntimeModule( ...@@ -475,6 +475,12 @@ function instantiateRuntimeModule(
): Module { ): Module {
return instantiateModule(moduleId, SourceType.Runtime, chunkPath) return instantiateModule(moduleId, SourceType.Runtime, chunkPath)
} }
/**
* Matches any character `encodeURIComponent` escapes. The path separator is
* excluded because chunk paths are encoded a segment at a time.
*/
const CHUNK_PATH_NEEDS_ENCODING = /[^A-Za-z0-9\-_.!~*'()/]/
/** /**
* Returns the URL relative to the origin where a chunk can be fetched from. * Returns the URL relative to the origin where a chunk can be fetched from.
*/ */
...@@ -482,10 +488,11 @@ function getChunkRelativeUrl( ...@@ -482,10 +488,11 @@ function getChunkRelativeUrl(
chunkPath: ChunkPath | ChunkListPath, chunkPath: ChunkPath | ChunkListPath,
basePath: string = CHUNK_BASE_PATH basePath: string = CHUNK_BASE_PATH
): ChunkUrl { ): ChunkUrl {
return `${basePath}${chunkPath // Most chunk paths need no escaping.
.split('/') const encodedPath = CHUNK_PATH_NEEDS_ENCODING.test(chunkPath)
.map((p) => encodeURIComponent(p)) ? chunkPath.split('/').map(encodeURIComponent).join('/')
.join('/')}${ASSET_SUFFIX}` as ChunkUrl : chunkPath
return `${basePath}${encodedPath}${ASSET_SUFFIX}` as ChunkUrl
} }
// Shared runtime primitives consumed by the bundled `createWorker` helper, // Shared runtime primitives consumed by the bundled `createWorker` helper,
......
;!function(){try { var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&((e._debugIds|| (e._debugIds={}))[n]="c2de8f36-af88-3f8a-2bd9-03966fc0cb0d")}catch(e){}}(); ;!function(){try { var e="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof global?global:"undefined"!=typeof window?window:"undefined"!=typeof self?self:{},n=(new e.Error).stack;n&&((e._debugIds|| (e._debugIds={}))[n]="8be33d93-70f0-5795-483a-8e4593e9e07e")}catch(e){}}();
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([ (globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
"output/1i9t_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_19boa0e.js", "output/1i9t_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_19boa0e.js",
{"otherChunks":["output/1do3_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_03ibyvs.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/debug-ids/browser/input/index.js [test] (ecmascript)"]} {"otherChunks":["output/1do3_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_03ibyvs.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/debug-ids/browser/input/index.js [test] (ecmascript)"]}
...@@ -949,10 +949,16 @@ browserContextPrototype.q = exportUrl; ...@@ -949,10 +949,16 @@ browserContextPrototype.q = exportUrl;
*/ function instantiateRuntimeModule(moduleId, chunkPath) { */ function instantiateRuntimeModule(moduleId, chunkPath) {
return instantiateModule(moduleId, SourceType.Runtime, chunkPath); return instantiateModule(moduleId, SourceType.Runtime, chunkPath);
} }
/**
* Matches any character `encodeURIComponent` escapes. The path separator is
* excluded because chunk paths are encoded a segment at a time.
*/ const CHUNK_PATH_NEEDS_ENCODING = /[^A-Za-z0-9\-_.!~*'()/]/;
/** /**
* Returns the URL relative to the origin where a chunk can be fetched from. * Returns the URL relative to the origin where a chunk can be fetched from.
*/ function getChunkRelativeUrl(chunkPath, basePath = CHUNK_BASE_PATH) { */ function getChunkRelativeUrl(chunkPath, basePath = CHUNK_BASE_PATH) {
return `${basePath}${chunkPath.split('/').map((p)=>encodeURIComponent(p)).join('/')}${ASSET_SUFFIX}`; // Most chunk paths need no escaping.
const encodedPath = CHUNK_PATH_NEEDS_ENCODING.test(chunkPath) ? chunkPath.split('/').map(encodeURIComponent).join('/') : chunkPath;
return `${basePath}${encodedPath}${ASSET_SUFFIX}`;
} }
// Shared runtime primitives consumed by the bundled `createWorker` helper, // Shared runtime primitives consumed by the bundled `createWorker` helper,
// exposed as `__turbopack_chunk_base_path__` and `__turbopack_chunk_asset_suffix__`. // exposed as `__turbopack_chunk_base_path__` and `__turbopack_chunk_asset_suffix__`.
...@@ -2475,5 +2481,5 @@ chunkListsToRegister.forEach(registerChunkList); ...@@ -2475,5 +2481,5 @@ chunkListsToRegister.forEach(registerChunkList);
})(); })();
//# debugId=c2de8f36-af88-3f8a-2bd9-03966fc0cb0d //# debugId=8be33d93-70f0-5795-483a-8e4593e9e07e
//# sourceMappingURL=1do3_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_19boa0e.js.map //# sourceMappingURL=1do3_crates_turbopack-tests_tests_snapshot_debug-ids_browser_input_index_19boa0e.js.map
\ No newline at end of file
...@@ -948,10 +948,16 @@ browserContextPrototype.q = exportUrl; ...@@ -948,10 +948,16 @@ browserContextPrototype.q = exportUrl;
*/ function instantiateRuntimeModule(moduleId, chunkPath) { */ function instantiateRuntimeModule(moduleId, chunkPath) {
return instantiateModule(moduleId, SourceType.Runtime, chunkPath); return instantiateModule(moduleId, SourceType.Runtime, chunkPath);
} }
/**
* Matches any character `encodeURIComponent` escapes. The path separator is
* excluded because chunk paths are encoded a segment at a time.
*/ const CHUNK_PATH_NEEDS_ENCODING = /[^A-Za-z0-9\-_.!~*'()/]/;
/** /**
* Returns the URL relative to the origin where a chunk can be fetched from. * Returns the URL relative to the origin where a chunk can be fetched from.
*/ function getChunkRelativeUrl(chunkPath, basePath = CHUNK_BASE_PATH) { */ function getChunkRelativeUrl(chunkPath, basePath = CHUNK_BASE_PATH) {
return `${basePath}${chunkPath.split('/').map((p)=>encodeURIComponent(p)).join('/')}${ASSET_SUFFIX}`; // Most chunk paths need no escaping.
const encodedPath = CHUNK_PATH_NEEDS_ENCODING.test(chunkPath) ? chunkPath.split('/').map(encodeURIComponent).join('/') : chunkPath;
return `${basePath}${encodedPath}${ASSET_SUFFIX}`;
} }
// Shared runtime primitives consumed by the bundled `createWorker` helper, // Shared runtime primitives consumed by the bundled `createWorker` helper,
// exposed as `__turbopack_chunk_base_path__` and `__turbopack_chunk_asset_suffix__`. // exposed as `__turbopack_chunk_base_path__` and `__turbopack_chunk_asset_suffix__`.
......
...@@ -1343,13 +1343,17 @@ browserContextPrototype.q = exportUrl; ...@@ -1343,13 +1343,17 @@ browserContextPrototype.q = exportUrl;
*/ function instantiateRuntimeModule(moduleId, chunkPath) { */ function instantiateRuntimeModule(moduleId, chunkPath) {
return instantiateModule(moduleId, SourceType.Runtime, chunkPath); return instantiateModule(moduleId, SourceType.Runtime, chunkPath);
} }
/**
* Matches any character `encodeURIComponent` escapes. The path separator is
* excluded because chunk paths are encoded a segment at a time.
*/ var CHUNK_PATH_NEEDS_ENCODING = /[^A-Za-z0-9\-_.!~*'()/]/;
/** /**
* Returns the URL relative to the origin where a chunk can be fetched from. * Returns the URL relative to the origin where a chunk can be fetched from.
*/ function getChunkRelativeUrl(chunkPath) { */ function getChunkRelativeUrl(chunkPath) {
var basePath = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : CHUNK_BASE_PATH; var basePath = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : CHUNK_BASE_PATH;
return `${basePath}${chunkPath.split('/').map(function(p) { // Most chunk paths need no escaping.
return encodeURIComponent(p); var encodedPath = CHUNK_PATH_NEEDS_ENCODING.test(chunkPath) ? chunkPath.split('/').map(encodeURIComponent).join('/') : chunkPath;
}).join('/')}${ASSET_SUFFIX}`; return `${basePath}${encodedPath}${ASSET_SUFFIX}`;
} }
// Shared runtime primitives consumed by the bundled `createWorker` helper, // Shared runtime primitives consumed by the bundled `createWorker` helper,
// exposed as `__turbopack_chunk_base_path__` and `__turbopack_chunk_asset_suffix__`. // exposed as `__turbopack_chunk_base_path__` and `__turbopack_chunk_asset_suffix__`.
......
...@@ -948,10 +948,16 @@ browserContextPrototype.q = exportUrl; ...@@ -948,10 +948,16 @@ browserContextPrototype.q = exportUrl;
*/ function instantiateRuntimeModule(moduleId, chunkPath) { */ function instantiateRuntimeModule(moduleId, chunkPath) {
return instantiateModule(moduleId, SourceType.Runtime, chunkPath); return instantiateModule(moduleId, SourceType.Runtime, chunkPath);
} }
/**
* Matches any character `encodeURIComponent` escapes. The path separator is
* excluded because chunk paths are encoded a segment at a time.
*/ const CHUNK_PATH_NEEDS_ENCODING = /[^A-Za-z0-9\-_.!~*'()/]/;
/** /**
* Returns the URL relative to the origin where a chunk can be fetched from. * Returns the URL relative to the origin where a chunk can be fetched from.
*/ function getChunkRelativeUrl(chunkPath, basePath = CHUNK_BASE_PATH) { */ function getChunkRelativeUrl(chunkPath, basePath = CHUNK_BASE_PATH) {
return `${basePath}${chunkPath.split('/').map((p)=>encodeURIComponent(p)).join('/')}${ASSET_SUFFIX}`; // Most chunk paths need no escaping.
const encodedPath = CHUNK_PATH_NEEDS_ENCODING.test(chunkPath) ? chunkPath.split('/').map(encodeURIComponent).join('/') : chunkPath;
return `${basePath}${encodedPath}${ASSET_SUFFIX}`;
} }
// Shared runtime primitives consumed by the bundled `createWorker` helper, // Shared runtime primitives consumed by the bundled `createWorker` helper,
// exposed as `__turbopack_chunk_base_path__` and `__turbopack_chunk_asset_suffix__`. // exposed as `__turbopack_chunk_base_path__` and `__turbopack_chunk_asset_suffix__`.
......
...@@ -948,10 +948,16 @@ browserContextPrototype.q = exportUrl; ...@@ -948,10 +948,16 @@ browserContextPrototype.q = exportUrl;
*/ function instantiateRuntimeModule(moduleId, chunkPath) { */ function instantiateRuntimeModule(moduleId, chunkPath) {
return instantiateModule(moduleId, SourceType.Runtime, chunkPath); return instantiateModule(moduleId, SourceType.Runtime, chunkPath);
} }
/**
* Matches any character `encodeURIComponent` escapes. The path separator is
* excluded because chunk paths are encoded a segment at a time.
*/ const CHUNK_PATH_NEEDS_ENCODING = /[^A-Za-z0-9\-_.!~*'()/]/;
/** /**
* Returns the URL relative to the origin where a chunk can be fetched from. * Returns the URL relative to the origin where a chunk can be fetched from.
*/ function getChunkRelativeUrl(chunkPath, basePath = CHUNK_BASE_PATH) { */ function getChunkRelativeUrl(chunkPath, basePath = CHUNK_BASE_PATH) {
return `${basePath}${chunkPath.split('/').map((p)=>encodeURIComponent(p)).join('/')}${ASSET_SUFFIX}`; // Most chunk paths need no escaping.
const encodedPath = CHUNK_PATH_NEEDS_ENCODING.test(chunkPath) ? chunkPath.split('/').map(encodeURIComponent).join('/') : chunkPath;
return `${basePath}${encodedPath}${ASSET_SUFFIX}`;
} }
// Shared runtime primitives consumed by the bundled `createWorker` helper, // Shared runtime primitives consumed by the bundled `createWorker` helper,
// exposed as `__turbopack_chunk_base_path__` and `__turbopack_chunk_asset_suffix__`. // exposed as `__turbopack_chunk_base_path__` and `__turbopack_chunk_asset_suffix__`.
......
...@@ -948,10 +948,16 @@ browserContextPrototype.q = exportUrl; ...@@ -948,10 +948,16 @@ browserContextPrototype.q = exportUrl;
*/ function instantiateRuntimeModule(moduleId, chunkPath) { */ function instantiateRuntimeModule(moduleId, chunkPath) {
return instantiateModule(moduleId, SourceType.Runtime, chunkPath); return instantiateModule(moduleId, SourceType.Runtime, chunkPath);
} }
/**
* Matches any character `encodeURIComponent` escapes. The path separator is
* excluded because chunk paths are encoded a segment at a time.
*/ const CHUNK_PATH_NEEDS_ENCODING = /[^A-Za-z0-9\-_.!~*'()/]/;
/** /**
* Returns the URL relative to the origin where a chunk can be fetched from. * Returns the URL relative to the origin where a chunk can be fetched from.
*/ function getChunkRelativeUrl(chunkPath, basePath = CHUNK_BASE_PATH) { */ function getChunkRelativeUrl(chunkPath, basePath = CHUNK_BASE_PATH) {
return `${basePath}${chunkPath.split('/').map((p)=>encodeURIComponent(p)).join('/')}${ASSET_SUFFIX}`; // Most chunk paths need no escaping.
const encodedPath = CHUNK_PATH_NEEDS_ENCODING.test(chunkPath) ? chunkPath.split('/').map(encodeURIComponent).join('/') : chunkPath;
return `${basePath}${encodedPath}${ASSET_SUFFIX}`;
} }
// Shared runtime primitives consumed by the bundled `createWorker` helper, // Shared runtime primitives consumed by the bundled `createWorker` helper,
// exposed as `__turbopack_chunk_base_path__` and `__turbopack_chunk_asset_suffix__`. // exposed as `__turbopack_chunk_base_path__` and `__turbopack_chunk_asset_suffix__`.
......
...@@ -948,10 +948,16 @@ browserContextPrototype.q = exportUrl; ...@@ -948,10 +948,16 @@ browserContextPrototype.q = exportUrl;
*/ function instantiateRuntimeModule(moduleId, chunkPath) { */ function instantiateRuntimeModule(moduleId, chunkPath) {
return instantiateModule(moduleId, SourceType.Runtime, chunkPath); return instantiateModule(moduleId, SourceType.Runtime, chunkPath);
} }
/**
* Matches any character `encodeURIComponent` escapes. The path separator is
* excluded because chunk paths are encoded a segment at a time.
*/ const CHUNK_PATH_NEEDS_ENCODING = /[^A-Za-z0-9\-_.!~*'()/]/;
/** /**
* Returns the URL relative to the origin where a chunk can be fetched from. * Returns the URL relative to the origin where a chunk can be fetched from.
*/ function getChunkRelativeUrl(chunkPath, basePath = CHUNK_BASE_PATH) { */ function getChunkRelativeUrl(chunkPath, basePath = CHUNK_BASE_PATH) {
return `${basePath}${chunkPath.split('/').map((p)=>encodeURIComponent(p)).join('/')}${ASSET_SUFFIX}`; // Most chunk paths need no escaping.
const encodedPath = CHUNK_PATH_NEEDS_ENCODING.test(chunkPath) ? chunkPath.split('/').map(encodeURIComponent).join('/') : chunkPath;
return `${basePath}${encodedPath}${ASSET_SUFFIX}`;
} }
// Shared runtime primitives consumed by the bundled `createWorker` helper, // Shared runtime primitives consumed by the bundled `createWorker` helper,
// exposed as `__turbopack_chunk_base_path__` and `__turbopack_chunk_asset_suffix__`. // exposed as `__turbopack_chunk_base_path__` and `__turbopack_chunk_asset_suffix__`.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment