Unverified Commit f3beddf1 authored by Sebastian "Sebbie" Silbermann's avatar Sebastian "Sebbie" Silbermann Committed by GitHub

[turbopack] Reuse shared Promise for loaded chunks (#81755)

parent b0280ebb
...@@ -607,7 +607,7 @@ function applyChunkListUpdate(update: ChunkListUpdate) { ...@@ -607,7 +607,7 @@ function applyChunkListUpdate(update: ChunkListUpdate) {
switch (chunkUpdate.type) { switch (chunkUpdate.type) {
case 'added': case 'added':
BACKEND.loadChunk(chunkUrl, { type: SourceType.Update }) BACKEND.loadChunkCached(chunkUrl, { type: SourceType.Update })
break break
case 'total': case 'total':
DEV_BACKEND.reloadChunk?.(chunkUrl) DEV_BACKEND.reloadChunk?.(chunkUrl)
......
...@@ -74,7 +74,10 @@ type SourceInfo = ...@@ -74,7 +74,10 @@ type SourceInfo =
interface RuntimeBackend { interface RuntimeBackend {
registerChunk: (chunkPath: ChunkPath, params?: RuntimeParams) => void registerChunk: (chunkPath: ChunkPath, params?: RuntimeParams) => void
loadChunk: (chunkUrl: ChunkUrl, source: SourceInfo) => Promise<void> /**
* Returns the same Promise for the same chunk URL.
*/
loadChunkCached: (chunkUrl: ChunkUrl, source: SourceInfo) => Promise<void>
} }
interface DevRuntimeBackend { interface DevRuntimeBackend {
...@@ -193,13 +196,22 @@ async function loadChunk( ...@@ -193,13 +196,22 @@ async function loadChunk(
return promise return promise
} }
const instrumentedBackendLoadChunks = new WeakMap<Promise<any>, Promise<any>>() const loadedChunk = Promise.resolve(undefined)
const instrumentedBackendLoadChunks = new WeakMap<
Promise<any>,
Promise<any> | typeof loadedChunk
>()
// Do not make this async. React relies on referential equality of the returned Promise. // Do not make this async. React relies on referential equality of the returned Promise.
function loadChunkByUrl(source: SourceInfo, chunkUrl: ChunkUrl): Promise<any> { function loadChunkByUrl(source: SourceInfo, chunkUrl: ChunkUrl): Promise<any> {
const thenable = BACKEND.loadChunk(chunkUrl, source) const thenable = BACKEND.loadChunkCached(chunkUrl, source)
let entry = instrumentedBackendLoadChunks.get(thenable) let entry = instrumentedBackendLoadChunks.get(thenable)
if (entry === undefined) { if (entry === undefined) {
entry = thenable.catch((error) => { const resolve = instrumentedBackendLoadChunks.set.bind(
instrumentedBackendLoadChunks,
thenable,
loadedChunk
)
entry = thenable.then(resolve).catch((error) => {
let loadReason let loadReason
switch (source.type) { switch (source.type) {
case SourceType.Runtime: case SourceType.Runtime:
...@@ -225,7 +237,6 @@ function loadChunkByUrl(source: SourceInfo, chunkUrl: ChunkUrl): Promise<any> { ...@@ -225,7 +237,6 @@ function loadChunkByUrl(source: SourceInfo, chunkUrl: ChunkUrl): Promise<any> {
: undefined : undefined
) )
}) })
// TODO: Free the Promise once it resolves.
instrumentedBackendLoadChunks.set(thenable, entry) instrumentedBackendLoadChunks.set(thenable, entry)
} }
......
...@@ -94,7 +94,7 @@ const chunkResolvers: Map<ChunkUrl, ChunkResolver> = new Map() ...@@ -94,7 +94,7 @@ const chunkResolvers: Map<ChunkUrl, ChunkResolver> = new Map()
* Loads the given chunk, and returns a promise that resolves once the chunk * Loads the given chunk, and returns a promise that resolves once the chunk
* has been loaded. * has been loaded.
*/ */
loadChunk(chunkUrl, source) { loadChunkCached(chunkUrl, source) {
return doLoadChunk(chunkUrl, source) return doLoadChunk(chunkUrl, source)
}, },
} }
......
...@@ -117,7 +117,7 @@ async function loadWebAssemblyModule( ...@@ -117,7 +117,7 @@ async function loadWebAssemblyModule(
} }
}, },
loadChunk(_chunkUrl, _source) { loadChunkCached(_chunkUrl, _source) {
throw new Error('chunk loading is not supported') throw new Error('chunk loading is not supported')
}, },
} }
......
...@@ -95,7 +95,7 @@ function loadChunk(chunkData: ChunkData, source?: SourceInfo): void { ...@@ -95,7 +95,7 @@ function loadChunk(chunkData: ChunkData, source?: SourceInfo): void {
const loadedChunks = new Set<ChunkPath>() const loadedChunks = new Set<ChunkPath>()
const unsupportedLoadChunk = Promise.resolve(undefined) const unsupportedLoadChunk = Promise.resolve(undefined)
const loadedChunk = Promise.resolve(undefined) const loadedChunk = Promise.resolve(undefined)
const chunkCache = new Map<ChunkPath, Promise<any> | null>() const chunkCache = new Map<ChunkPath, Promise<any> | typeof loadedChunk>()
function clearChunkCache() { function clearChunkCache() {
chunkCache.clear() chunkCache.clear()
...@@ -209,14 +209,14 @@ function loadChunkAsync( ...@@ -209,14 +209,14 @@ function loadChunkAsync(
let entry = chunkCache.get(chunkPath) let entry = chunkCache.get(chunkPath)
if (entry === undefined) { if (entry === undefined) {
const resolve = chunkCache.set.bind(chunkCache, chunkPath, null) const resolve = chunkCache.set.bind(chunkCache, chunkPath, loadedChunk)
// A new Promise ensures callers that don't handle rejection will still trigger one unhandled rejection. // A new Promise ensures callers that don't handle rejection will still trigger one unhandled rejection.
// Handling the rejection will not trigger unhandled rejections. // Handling the rejection will not trigger unhandled rejections.
entry = loadChunkAsyncUncached(source, chunkPath).then(resolve) entry = loadChunkAsyncUncached(source, chunkPath).then(resolve)
chunkCache.set(chunkPath, entry) chunkCache.set(chunkPath, entry)
} }
// TODO: Return an instrumented Promise that React can use instead of relying on referential equality. // TODO: Return an instrumented Promise that React can use instead of relying on referential equality.
return entry === null ? loadedChunk : entry return entry
} }
function loadChunkAsyncByUrl(source: SourceInfo, chunkUrl: string) { function loadChunkAsyncByUrl(source: SourceInfo, chunkUrl: string) {
......
...@@ -587,14 +587,14 @@ function loadChunkAsync(source, chunkData) { ...@@ -587,14 +587,14 @@ function loadChunkAsync(source, chunkData) {
} }
let entry = chunkCache.get(chunkPath); let entry = chunkCache.get(chunkPath);
if (entry === undefined) { if (entry === undefined) {
const resolve = chunkCache.set.bind(chunkCache, chunkPath, null); const resolve = chunkCache.set.bind(chunkCache, chunkPath, loadedChunk);
// A new Promise ensures callers that don't handle rejection will still trigger one unhandled rejection. // A new Promise ensures callers that don't handle rejection will still trigger one unhandled rejection.
// Handling the rejection will not trigger unhandled rejections. // Handling the rejection will not trigger unhandled rejections.
entry = loadChunkAsyncUncached(source, chunkPath).then(resolve); entry = loadChunkAsyncUncached(source, chunkPath).then(resolve);
chunkCache.set(chunkPath, entry); chunkCache.set(chunkPath, entry);
} }
// TODO: Return an instrumented Promise that React can use instead of relying on referential equality. // TODO: Return an instrumented Promise that React can use instead of relying on referential equality.
return entry === null ? loadedChunk : entry; return entry;
} }
function loadChunkAsyncByUrl(source, chunkUrl) { function loadChunkAsyncByUrl(source, chunkUrl) {
const path1 = url.fileURLToPath(new URL(chunkUrl, RUNTIME_ROOT)); const path1 = url.fileURLToPath(new URL(chunkUrl, RUNTIME_ROOT));
......
...@@ -482,13 +482,15 @@ async function loadChunk(source, chunkData) { ...@@ -482,13 +482,15 @@ async function loadChunk(source, chunkData) {
} }
return promise; return promise;
} }
const loadedChunk = Promise.resolve(undefined);
const instrumentedBackendLoadChunks = new WeakMap(); const instrumentedBackendLoadChunks = new WeakMap();
// Do not make this async. React relies on referential equality of the returned Promise. // Do not make this async. React relies on referential equality of the returned Promise.
function loadChunkByUrl(source, chunkUrl) { function loadChunkByUrl(source, chunkUrl) {
const thenable = BACKEND.loadChunk(chunkUrl, source); const thenable = BACKEND.loadChunkCached(chunkUrl, source);
let entry = instrumentedBackendLoadChunks.get(thenable); let entry = instrumentedBackendLoadChunks.get(thenable);
if (entry === undefined) { if (entry === undefined) {
entry = thenable.catch((error)=>{ const resolve = instrumentedBackendLoadChunks.set.bind(instrumentedBackendLoadChunks, thenable, loadedChunk);
entry = thenable.then(resolve).catch((error)=>{
let loadReason; let loadReason;
switch(source.type){ switch(source.type){
case 0: case 0:
...@@ -507,7 +509,6 @@ function loadChunkByUrl(source, chunkUrl) { ...@@ -507,7 +509,6 @@ function loadChunkByUrl(source, chunkUrl) {
cause: error cause: error
} : undefined); } : undefined);
}); });
// TODO: Free the Promise once it resolves.
instrumentedBackendLoadChunks.set(thenable, entry); instrumentedBackendLoadChunks.set(thenable, entry);
} }
return entry; return entry;
...@@ -1089,7 +1090,7 @@ function applyChunkListUpdate(update) { ...@@ -1089,7 +1090,7 @@ function applyChunkListUpdate(update) {
const chunkUrl = getChunkRelativeUrl(chunkPath); const chunkUrl = getChunkRelativeUrl(chunkPath);
switch(chunkUpdate.type){ switch(chunkUpdate.type){
case 'added': case 'added':
BACKEND.loadChunk(chunkUrl, { BACKEND.loadChunkCached(chunkUrl, {
type: SourceType.Update type: SourceType.Update
}); });
break; break;
...@@ -1530,7 +1531,7 @@ async function loadWebAssemblyModule(_source, wasmChunkPath, _edgeModule) { ...@@ -1530,7 +1531,7 @@ async function loadWebAssemblyModule(_source, wasmChunkPath, _edgeModule) {
/** /**
* Loads the given chunk, and returns a promise that resolves once the chunk * Loads the given chunk, and returns a promise that resolves once the chunk
* has been loaded. * has been loaded.
*/ loadChunk (chunkUrl, source) { */ loadChunkCached (chunkUrl, source) {
return doLoadChunk(chunkUrl, source); return doLoadChunk(chunkUrl, source);
} }
}; };
......
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