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