@@ -229,7 +229,7 @@ let { startTimer, endTimer } = debugWithTimer('module_timer', (start, end) => { |
229 | 229 | const { tracingChannel } = require('diagnostics_channel'); |
230 | 230 | const onRequire = getLazy(() => tracingChannel('module.require')); |
231 | 231 |
|
232 | | -const relativeResolveCache = { __proto__: null }; |
| 232 | +const relativeResolveCache = new SafeMap(); |
233 | 233 |
|
234 | 234 | let requireDepth = 0; |
235 | 235 | let isPreloading = false; |
@@ -1312,17 +1312,19 @@ function loadBuiltinWithHooks(id, url, format) { |
1312 | 1312 | * @returns {object} |
1313 | 1313 | */ |
1314 | 1314 | Module._load = function(request, parent, isMain, internalOptions = kEmptyObject) { |
1315 | | - let relResolveCacheIdentifier; |
| 1315 | + let parentPath; |
| 1316 | + let relResolveCacheByDir; |
1316 | 1317 | if (parent) { |
1317 | 1318 | debug('Module._load REQUEST %s parent: %s', request, parent.id); |
1318 | | - // Fast path for (lazy loaded) modules in the same directory. The indirect |
1319 | | - // caching is required to allow cache invalidation without changing the old |
1320 | | - // cache key names. |
1321 | | - relResolveCacheIdentifier = `${parent.path}\x00${request}`; |
1322 | | - const filename = relativeResolveCache[relResolveCacheIdentifier]; |
1323 | | - reportModuleToWatchMode(filename); |
1324 | | - reportModuleToWatchModeFromWorker(filename); |
| 1319 | + // Fast path for (lazy loaded) modules in the same directory. Keyed by |
| 1320 | + // parent directory and then request, so no concatenated cache key |
| 1321 | + // string is allocated per require() call. |
| 1322 | + parentPath = parent.path; |
| 1323 | + relResolveCacheByDir = relativeResolveCache.get(parentPath); |
| 1324 | + const filename = relResolveCacheByDir?.[request]; |
1325 | 1325 | if (filename !== undefined) { |
| 1326 | + reportModuleToWatchMode(filename); |
| 1327 | + reportModuleToWatchModeFromWorker(filename); |
1326 | 1328 | const cachedModule = Module._cache[filename]; |
1327 | 1329 | if (cachedModule !== undefined) { |
1328 | 1330 | updateChildren(parent, cachedModule, true); |
@@ -1331,7 +1333,7 @@ Module._load = function(request, parent, isMain, internalOptions = kEmptyObject) |
1331 | 1333 | } |
1332 | 1334 | return cachedModule.exports; |
1333 | 1335 | } |
1334 | | - delete relativeResolveCache[relResolveCacheIdentifier]; |
| 1336 | + delete relResolveCacheByDir[request]; |
1335 | 1337 | } |
1336 | 1338 | } |
1337 | 1339 |
|
@@ -1434,8 +1436,16 @@ Module._load = function(request, parent, isMain, internalOptions = kEmptyObject) |
1434 | 1436 | module[kFormat] ??= format; |
1435 | 1437 | } |
1436 | 1438 |
|
1437 | | - if (parent !== undefined) { |
1438 | | - relativeResolveCache[relResolveCacheIdentifier] = filename; |
| 1439 | + if (parent) { |
| 1440 | + // Only create the per-directory bucket once there is an entry to store, |
| 1441 | + // so builtins and failed resolutions don't leave empty buckets behind. |
| 1442 | + if (relResolveCacheByDir === undefined) { |
| 1443 | + // A plain object handles dynamically built specifier strings |
| 1444 | + // better than a Map here. |
| 1445 | + relResolveCacheByDir = { __proto__: null }; |
| 1446 | + relativeResolveCache.set(parentPath, relResolveCacheByDir); |
| 1447 | + } |
| 1448 | + relResolveCacheByDir[request] = filename; |
1439 | 1449 | } |
1440 | 1450 |
|
1441 | 1451 | let threw = true; |
@@ -1446,7 +1456,9 @@ Module._load = function(request, parent, isMain, internalOptions = kEmptyObject) |
1446 | 1456 | if (threw) { |
1447 | 1457 | delete Module._cache[filename]; |
1448 | 1458 | if (parent !== undefined) { |
1449 | | - delete relativeResolveCache[relResolveCacheIdentifier]; |
| 1459 | + if (relResolveCacheByDir !== undefined) { |
| 1460 | + delete relResolveCacheByDir[request]; |
| 1461 | + } |
1450 | 1462 | const children = parent?.children; |
1451 | 1463 | if (ArrayIsArray(children)) { |
1452 | 1464 | const index = ArrayPrototypeIndexOf(children, module); |
|