Commit 939b0a3e authored by Will Binns-Smith's avatar Will Binns-Smith

Make moduleCache a Map

parent 44d2756a
/// <reference path="./runtime-base.ts" /> /// <reference path="./runtime-base.ts" />
/// <reference path="./dummy.ts" /> /// <reference path="./dummy.ts" />
const moduleCache: ModuleCache<Module> = {} const moduleCache: ModuleCache<Module> = new Map()
contextPrototype.c = moduleCache contextPrototype.c = moduleCache
/** /**
...@@ -13,7 +13,7 @@ function getOrInstantiateRuntimeModule( ...@@ -13,7 +13,7 @@ function getOrInstantiateRuntimeModule(
chunkPath: ChunkPath, chunkPath: ChunkPath,
moduleId: ModuleId moduleId: ModuleId
): Module { ): Module {
const module = moduleCache[moduleId] const module = moduleCache.get(moduleId)
if (module) { if (module) {
if (module.error) { if (module.error) {
throw module.error throw module.error
...@@ -33,7 +33,7 @@ function getOrInstantiateRuntimeModule( ...@@ -33,7 +33,7 @@ function getOrInstantiateRuntimeModule(
const getOrInstantiateModuleFromParent: GetOrInstantiateModuleFromParent< const getOrInstantiateModuleFromParent: GetOrInstantiateModuleFromParent<
Module Module
> = (id, sourceModule) => { > = (id, sourceModule) => {
const module = moduleCache[id] const module = moduleCache.get(id)
if (module) { if (module) {
if (module.error) { if (module.error) {
...@@ -61,7 +61,7 @@ function instantiateModule( ...@@ -61,7 +61,7 @@ function instantiateModule(
const module: Module = createModuleObject(id) const module: Module = createModuleObject(id)
const exports = module.exports const exports = module.exports
moduleCache[id] = module moduleCache.set(id, module)
// NOTE(alexkirsz) This can fail when the module encounters a runtime error. // NOTE(alexkirsz) This can fail when the module encounters a runtime error.
const context = new (Context as any as ContextConstructor<Module>)( const context = new (Context as any as ContextConstructor<Module>)(
......
...@@ -17,7 +17,7 @@ const devContextPrototype = Context.prototype as TurbopackDevContext ...@@ -17,7 +17,7 @@ const devContextPrototype = Context.prototype as TurbopackDevContext
/* eslint-disable @typescript-eslint/no-unused-vars */ /* eslint-disable @typescript-eslint/no-unused-vars */
// Assign browser's module cache and runtime modules to shared HMR state // Assign browser's module cache and runtime modules to shared HMR state
devModuleCache = Object.create(null) devModuleCache = new Map()
devContextPrototype.c = devModuleCache devContextPrototype.c = devModuleCache
runtimeModules = new Set() runtimeModules = new Set()
...@@ -86,7 +86,7 @@ function getOrInstantiateRuntimeModule( ...@@ -86,7 +86,7 @@ function getOrInstantiateRuntimeModule(
chunkPath: ChunkPath, chunkPath: ChunkPath,
moduleId: ModuleId moduleId: ModuleId
): Module { ): Module {
const module = devModuleCache[moduleId] const module = devModuleCache.get(moduleId)
if (module) { if (module) {
if (module.error) { if (module.error) {
throw module.error throw module.error
...@@ -111,7 +111,7 @@ const getOrInstantiateModuleFromParent: GetOrInstantiateModuleFromParent< ...@@ -111,7 +111,7 @@ const getOrInstantiateModuleFromParent: GetOrInstantiateModuleFromParent<
) )
} }
const module = devModuleCache[id] const module = devModuleCache.get(id)
if (sourceModule.children.indexOf(id) === -1) { if (sourceModule.children.indexOf(id) === -1) {
sourceModule.children.push(id) sourceModule.children.push(id)
......
...@@ -58,7 +58,7 @@ function instantiateModule( ...@@ -58,7 +58,7 @@ function instantiateModule(
const module: ModuleWithDirection = createModuleWithDirection(id) const module: ModuleWithDirection = createModuleWithDirection(id)
const exports = module.exports const exports = module.exports
moduleCache[id] = module moduleCache.set(id, module)
const context = const context =
new (Context as any as ContextConstructor<ModuleWithDirection>)( new (Context as any as ContextConstructor<ModuleWithDirection>)(
...@@ -90,7 +90,7 @@ function getOrInstantiateModuleFromParent( ...@@ -90,7 +90,7 @@ function getOrInstantiateModuleFromParent(
id: ModuleId, id: ModuleId,
sourceModule: ModuleWithDirection sourceModule: ModuleWithDirection
): ModuleWithDirection { ): ModuleWithDirection {
const module = moduleCache[id] const module = moduleCache.get(id)
if (module) { if (module) {
if (module.error) { if (module.error) {
...@@ -121,7 +121,7 @@ function getOrInstantiateRuntimeModule( ...@@ -121,7 +121,7 @@ function getOrInstantiateRuntimeModule(
chunkPath: ChunkPath, chunkPath: ChunkPath,
moduleId: ModuleId moduleId: ModuleId
): ModuleWithDirection { ): ModuleWithDirection {
const module = moduleCache[moduleId] const module = moduleCache.get(moduleId)
if (module) { if (module) {
if (module.error) { if (module.error) {
......
...@@ -103,7 +103,7 @@ function getOrInstantiateRuntimeModule( ...@@ -103,7 +103,7 @@ function getOrInstantiateRuntimeModule(
chunkPath: ChunkPath, chunkPath: ChunkPath,
moduleId: ModuleId moduleId: ModuleId
): HotModule { ): HotModule {
const module = devModuleCache[moduleId] const module = devModuleCache.get(moduleId)
if (module) { if (module) {
if (module.error) { if (module.error) {
...@@ -125,9 +125,9 @@ function getOrInstantiateModuleFromParent( ...@@ -125,9 +125,9 @@ function getOrInstantiateModuleFromParent(
sourceModule: HotModule sourceModule: HotModule
): HotModule { ): HotModule {
// Track parent-child relationship // Track parent-child relationship
trackModuleImport(sourceModule, id, devModuleCache[id]) trackModuleImport(sourceModule, id, devModuleCache.get(id))
const module = devModuleCache[id] const module = devModuleCache.get(id)
if (module) { if (module) {
if (module.error) { if (module.error) {
......
...@@ -7,7 +7,7 @@ ...@@ -7,7 +7,7 @@
declare global { declare global {
var __turbopack_module_factories__: ModuleFactories var __turbopack_module_factories__: ModuleFactories
var __turbopack_module_cache__: Record<ModuleId, any> var __turbopack_module_cache__: ModuleCache<ModuleId>
var __turbopack_runtime_modules__: Set<ModuleId> var __turbopack_runtime_modules__: Set<ModuleId>
} }
......
...@@ -22,7 +22,7 @@ if (!globalThis.__turbopack_module_factories__) { ...@@ -22,7 +22,7 @@ if (!globalThis.__turbopack_module_factories__) {
globalThis.__turbopack_module_factories__ = new Map() globalThis.__turbopack_module_factories__ = new Map()
} }
if (!globalThis.__turbopack_module_cache__) { if (!globalThis.__turbopack_module_cache__) {
globalThis.__turbopack_module_cache__ = Object.create(null) globalThis.__turbopack_module_cache__ = new Map()
} }
const moduleFactories: ModuleFactories = const moduleFactories: ModuleFactories =
...@@ -82,9 +82,7 @@ function clearChunkCache() { ...@@ -82,9 +82,7 @@ function clearChunkCache() {
chunkCache.clear() chunkCache.clear()
loadedChunks.clear() loadedChunks.clear()
moduleFactories.clear() moduleFactories.clear()
for (const key in moduleCache) { moduleCache.clear()
delete moduleCache[key]
}
} }
function loadRuntimeChunkPath( function loadRuntimeChunkPath(
......
...@@ -21,7 +21,7 @@ type HotModuleFactoryFunction = ModuleFactoryFunction< ...@@ -21,7 +21,7 @@ type HotModuleFactoryFunction = ModuleFactoryFunction<
* Browser runtime declares this directly. * Browser runtime declares this directly.
* Node.js runtime assigns globalThis.__turbopack_module_cache__ to this. * Node.js runtime assigns globalThis.__turbopack_module_cache__ to this.
*/ */
let devModuleCache: Record<ModuleId, any> let devModuleCache: ModuleCache<HotModule>
/** /**
* Module IDs that are instantiated as part of the runtime of a chunk. * Module IDs that are instantiated as part of the runtime of a chunk.
...@@ -157,7 +157,7 @@ function getAffectedModuleEffects( ...@@ -157,7 +157,7 @@ function getAffectedModuleEffects(
} }
} }
const module = devModuleCache[moduleId] const module = devModuleCache.get(moduleId)
const hotState = moduleHotState.get(module)! const hotState = moduleHotState.get(module)!
if ( if (
...@@ -190,7 +190,7 @@ function getAffectedModuleEffects( ...@@ -190,7 +190,7 @@ function getAffectedModuleEffects(
} }
for (const parentId of module.parents) { for (const parentId of module.parents) {
const parent = devModuleCache[parentId] const parent = devModuleCache.get(parentId)
if (!parent) { if (!parent) {
// TODO(alexkirsz) Is this even possible? // TODO(alexkirsz) Is this even possible?
...@@ -385,7 +385,7 @@ function computeOutdatedSelfAcceptedModules( ...@@ -385,7 +385,7 @@ function computeOutdatedSelfAcceptedModules(
errorHandler: true | Function errorHandler: true | Function
}[] = [] }[] = []
for (const moduleId of outdatedModules) { for (const moduleId of outdatedModules) {
const module = devModuleCache[moduleId] const module = devModuleCache.get(moduleId)
const hotState = moduleHotState.get(module) const hotState = moduleHotState.get(module)
if (module && hotState?.selfAccepted && !hotState.selfInvalidated) { if (module && hotState?.selfAccepted && !hotState.selfInvalidated) {
outdatedSelfAcceptedModules.push({ outdatedSelfAcceptedModules.push({
...@@ -405,7 +405,7 @@ function computeOutdatedSelfAcceptedModules( ...@@ -405,7 +405,7 @@ function computeOutdatedSelfAcceptedModules(
* This must be done in a separate step afterwards. * This must be done in a separate step afterwards.
*/ */
function disposeModule(moduleId: ModuleId, mode: 'clear' | 'replace') { function disposeModule(moduleId: ModuleId, mode: 'clear' | 'replace') {
const module = devModuleCache[moduleId] const module = devModuleCache.get(moduleId)
if (!module) { if (!module) {
return return
} }
...@@ -437,7 +437,7 @@ function disposeModule(moduleId: ModuleId, mode: 'clear' | 'replace') { ...@@ -437,7 +437,7 @@ function disposeModule(moduleId: ModuleId, mode: 'clear' | 'replace') {
// It will be added back once the module re-instantiates and imports its // It will be added back once the module re-instantiates and imports its
// children again. // children again.
for (const childId of module.children) { for (const childId of module.children) {
const child = devModuleCache[childId] const child = devModuleCache.get(childId)
if (!child) { if (!child) {
continue continue
} }
...@@ -450,7 +450,7 @@ function disposeModule(moduleId: ModuleId, mode: 'clear' | 'replace') { ...@@ -450,7 +450,7 @@ function disposeModule(moduleId: ModuleId, mode: 'clear' | 'replace') {
switch (mode) { switch (mode) {
case 'clear': case 'clear':
delete devModuleCache[module.id] devModuleCache.delete(module.id)
moduleHotData.delete(module.id) moduleHotData.delete(module.id)
break break
case 'replace': case 'replace':
...@@ -482,9 +482,9 @@ function disposePhase( ...@@ -482,9 +482,9 @@ function disposePhase(
// We also want to keep track of previous parents of the outdated modules. // We also want to keep track of previous parents of the outdated modules.
const outdatedModuleParents = new Map<ModuleId, Array<ModuleId>>() const outdatedModuleParents = new Map<ModuleId, Array<ModuleId>>()
for (const moduleId of outdatedModules) { for (const moduleId of outdatedModules) {
const oldModule = devModuleCache[moduleId] const oldModule = devModuleCache.get(moduleId)
outdatedModuleParents.set(moduleId, oldModule?.parents) outdatedModuleParents.set(moduleId, oldModule?.parents)
delete devModuleCache[moduleId] devModuleCache.delete(moduleId)
} }
// TODO(alexkirsz) Dependencies: remove outdated dependency from module // TODO(alexkirsz) Dependencies: remove outdated dependency from module
...@@ -551,7 +551,7 @@ function instantiateModuleShared( ...@@ -551,7 +551,7 @@ function instantiateModuleShared(
module.children = [] module.children = []
module.hot = hot module.hot = hot
devModuleCache[id] = module devModuleCache.set(id, module)
moduleHotState.set(module, hotState) moduleHotState.set(module, hotState)
// 5. Module execution (React Refresh hooks are platform-specific) // 5. Module execution (React Refresh hooks are platform-specific)
...@@ -741,7 +741,7 @@ function applyPhase( ...@@ -741,7 +741,7 @@ function applyPhase(
} catch (err) { } catch (err) {
if (typeof errorHandler === 'function') { if (typeof errorHandler === 'function') {
try { try {
errorHandler(err, { moduleId, module: devModuleCache[moduleId] }) errorHandler(err, { moduleId, module: devModuleCache.get(moduleId) })
} catch (err2) { } catch (err2) {
reportError(err2) reportError(err2)
reportError(err) reportError(err)
......
...@@ -73,7 +73,7 @@ type LoadWebAssemblyModule = ( ...@@ -73,7 +73,7 @@ type LoadWebAssemblyModule = (
edgeModule: () => WebAssembly.Module edgeModule: () => WebAssembly.Module
) => WebAssembly.Module ) => WebAssembly.Module
type ModuleCache<M> = Record<ModuleId, M> type ModuleCache<M> = Map<ModuleId, M>
// TODO properly type values here // TODO properly type values here
type ModuleFactories = Map<ModuleId, Function> type ModuleFactories = Map<ModuleId, Function>
/** /**
......
...@@ -109,12 +109,12 @@ function getOverwrittenModule( ...@@ -109,12 +109,12 @@ function getOverwrittenModule(
moduleCache: ModuleCache<Module>, moduleCache: ModuleCache<Module>,
id: ModuleId id: ModuleId
): Module { ): Module {
let module = moduleCache[id] let module = moduleCache.get(id)
if (!module) { if (!module) {
// This is invoked when a module is merged into another module, thus it wasn't invoked via // This is invoked when a module is merged into another module, thus it wasn't invoked via
// instantiateModule and the cache entry wasn't created yet. // instantiateModule and the cache entry wasn't created yet.
module = createModuleObject(id) module = createModuleObject(id)
moduleCache[id] = module moduleCache.set(id, module)
} }
return module return module
} }
......
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