GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — module: avoid allocating a cache key string for every require() by MarshallOfSound · Pull Request #63884 · nodejs/node

Conversation

@MarshallOfSound

@MarshallOfSound MarshallOfSound commented Jun 12, 2026

Copy link
Copy Markdown
Member

Description of Change

Every require() call — including fully cached ones — allocates a fresh `${parent.path}\x00${request}` string to key the relative-resolve fast path in Module._load. On cache hits that allocation and its GC churn are the largest per-call cost.

This PR splits the cache into two levels so hits do two allocation-free lookups instead: an outer SafeMap keyed by parent.path (a string the module system already retains, with a V8-cached hash) and an inner { __proto__: null } object keyed by the raw request.

  • The inner level is a plain object rather than a Map on purpose: property access internalizes the key, which handles dynamically built specifiers (require(base + name)) far better than Map.get's per-call hash-and-compare — and it preserves the string coercion the old template literal performed.
  • Invalidation is unchanged: stale hits delete the inner entry, failed loads clean up in the existing finally, and delete require.cache[...] + re-require behaves as before.
  • Memory is neutral (12.97 → 12.91 MB heap after loading a 2,000-directory tree) since the concatenated keys are no longer retained.

Observable edge: parent.path is now an identity Map key rather than being string-coerced, and is read twice on the first load into a directory — only reachable if module.path is replaced with a non-string or an accessor, neither of which is documented usage.

benchmark/compare.js shows significant wins on every relevant module-loader.js config — cached +7.8…10.7%, cold +5.6…8.0% — with clean nulls on the circular/deep benchmarks where this path isn't hot.

Full benchmark results

10 samples per configuration, Welch t-test (*** p<0.001, ** p<0.01, * p<0.05):

module/module-loader.js cache='true'  n=1000 files=500 dir='abs' name=''            +10.72% ***
module/module-loader.js cache='true'  n=1000 files=500 dir='abs' name='/'            +8.08% ***
module/module-loader.js cache='true'  n=1000 files=500 dir='abs' name='/index.js'    +7.79% ***
module/module-loader.js cache='true'  n=1000 files=500 dir='rel' name=''            +10.30% ***
module/module-loader.js cache='true'  n=1000 files=500 dir='rel' name='/'           +10.20% ***
module/module-loader.js cache='true'  n=1000 files=500 dir='rel' name='/index.js'    +9.24% ***
module/module-loader.js cache='false' n=1000 files=500 dir='abs' name=''             +6.20% ***
module/module-loader.js cache='false' n=1000 files=500 dir='abs' name='/'            +6.18% ***
module/module-loader.js cache='false' n=1000 files=500 dir='abs' name='/index.js'    +7.89% **
module/module-loader.js cache='false' n=1000 files=500 dir='rel' name=''             +8.01% ***
module/module-loader.js cache='false' n=1000 files=500 dir='rel' name='/'            +5.61% *
module/module-loader.js cache='false' n=1000 files=500 dir='rel' name='/index.js'    +6.79% ***
module/module-require.js n=10000 type='.js'                                          +3.06% **
module/module-require.js n=10000 type='.json'                                        +2.67% ***
module/module-require.js n=10000 type='dir'                                          +0.02%
module/module-loader-circular.js n=1000                                              +0.22%
module/module-loader-deep.js cache='true' files=1000 (both exts)                  ±1.9% (n.s.)

Ad-hoc workload-shape microbenchmarks land between −9% (300 dynamic-specifier children) and −58% (cached-require hot loop) load-phase time, neutral on cold loads — scripts and fixture generators: https://gist.github.com/MarshallOfSound/ad35b07ad5ebf9c5fec8e4daa4ec04b4

Checklist

  • I have built and tested this change
  • test/parallel/test-module-*, test-require-*, test-cjs-* pass
  • Manually verified cache-hit identity, delete require.cache + re-require, failed-load retry, circular requires, require.resolve with paths

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@nodejs-github-bot

nodejs-github-bot commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/loaders
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot nodejs-github-bot added module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run. labels Jun 12, 2026
@MarshallOfSound
module: avoid allocating a cache key string for every require()
96860f3
The relative resolve cache was keyed by a concatenated
${parent.path}\x00${request} string, allocating a new key for every
require() call including fully cached ones. Key the cache by the
parent directory first (a Map keyed by the already-retained
module.path string) and then by the request (a dictionary object,
whose property access internalizes dynamically-constructed
specifiers). Faster on every measured workload shape and slightly
smaller in memory, since the concatenated keys are no longer
retained.

Signed-off-by: Sam Attard <[email protected]>
@MarshallOfSound
MarshallOfSound force-pushed the perf/cache-only branch from 147ca94 to 96860f3 Compare June 12, 2026 18:17
@MarshallOfSound
MarshallOfSound marked this pull request as ready for review June 12, 2026 18:23
mcollina
mcollina approved these changes Jun 12, 2026
View reviewed changes

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
trivikr
trivikr requested changes Aug 23, 2026
View reviewed changes
Comment thread lib/internal/modules/cjs/loader.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread lib/internal/modules/cjs/loader.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

@MarshallOfSound
module: create relative resolve cache bucket lazily
a50d67a
Only create the per-directory relative resolve cache bucket once
there is a resolved filename to store in it, so builtin loads and
failed resolutions no longer leave empty buckets behind. Read
parent.path once per Module._load() call.

Signed-off-by: Sam Attard <[email protected]>
Assisted-by: Claude
@MarshallOfSound
MarshallOfSound requested a review from trivikr September 25, 2026 00:21
@codecov

codecov Bot commented Sep 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 90.33%. Comparing base (141a504) to head (a50d67a).
⚠️ Report is 1674 commits behind head on main.

Files with missing lines Patch % Lines
lib/internal/modules/cjs/loader.js 96.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main   #63884       +/-   ##
===========================================
- Coverage   92.00%   90.33%    -1.67%     
===========================================
  Files         379      789      +410     
  Lines      166972   273294   +106322     
  Branches    25569    52227    +26658     
===========================================
+ Hits       153621   246889    +93268     
- Misses      13057    16862     +3805     
- Partials      294     9543     +9249     
Files with missing lines Coverage Δ
lib/internal/modules/cjs/loader.js 97.99% <96.00%> (+18.27%) ⬆️

... and 667 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

trivikr
trivikr approved these changes Sep 28, 2026
View reviewed changes
@trivikr trivikr added author ready PRs with CI started, the required approvals, and no outstanding review comments. request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. commit-queue-squash PRs the Commit Queue should land as one squashed commit. labels Sep 28, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Sep 28, 2026
@nodejs-github-bot

nodejs-github-bot commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

CI: https://ci.nodejs.org/job/node-test-pull-request/78059/

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@trivikr trivikr added the commit-queue PRs queued for automated landing through the Commit Queue. label Sep 28, 2026
@nodejs-github-bot
nodejs-github-bot merged commit 471fe81 into nodejs:main Sep 28, 2026
80 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot

nodejs-github-bot commented Sep 28, 2026

Copy link
Copy Markdown
Collaborator

Landed in 471fe81

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@nodejs-github-bot nodejs-github-bot removed the commit-queue PRs queued for automated landing through the Commit Queue. label Sep 28, 2026
HoonDongKang pushed a commit to HoonDongKang/node that referenced this pull request Sep 29, 2026
@MarshallOfSound @HoonDongKang
module: avoid allocating a cache key string for every require()
b209773
The relative resolve cache was keyed by a concatenated
${parent.path}\x00${request} string, allocating a new key for every
require() call including fully cached ones. Key the cache by the
parent directory first (a Map keyed by the already-retained
module.path string) and then by the request (a dictionary object,
whose property access internalizes dynamically-constructed
specifiers). Faster on every measured workload shape and slightly
smaller in memory, since the concatenated keys are no longer
retained.

Signed-off-by: Sam Attard <[email protected]>
PR-URL: nodejs#63884
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
aduh95 pushed a commit that referenced this pull request Oct 3, 2026
@MarshallOfSound @aduh95
module: avoid allocating a cache key string for every require()
f36efeb
The relative resolve cache was keyed by a concatenated
${parent.path}\x00${request} string, allocating a new key for every
require() call including fully cached ones. Key the cache by the
parent directory first (a Map keyed by the already-retained
module.path string) and then by the request (a dictionary object,
whose property access internalizes dynamically-constructed
specifiers). Faster on every measured workload shape and slightly
smaller in memory, since the concatenated keys are no longer
retained.

Signed-off-by: Sam Attard <[email protected]>
PR-URL: #63884
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
@github-actions github-actions Bot mentioned this pull request Oct 5, 2026
2026-10-07, Version 26.11.0 (Current) #66546
Merged
aduh95 pushed a commit that referenced this pull request Oct 23, 2026
@MarshallOfSound @aduh95
module: avoid allocating a cache key string for every require()
128d28c
The relative resolve cache was keyed by a concatenated
${parent.path}\x00${request} string, allocating a new key for every
require() call including fully cached ones. Key the cache by the
parent directory first (a Map keyed by the already-retained
module.path string) and then by the request (a dictionary object,
whose property access internalizes dynamically-constructed
specifiers). Faster on every measured workload shape and slightly
smaller in memory, since the concatenated keys are no longer
retained.

Signed-off-by: Sam Attard <[email protected]>
PR-URL: #63884
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
aduh95 pushed a commit that referenced this pull request Oct 28, 2026
@MarshallOfSound @aduh95
module: avoid allocating a cache key string for every require()
79c5dae
The relative resolve cache was keyed by a concatenated
${parent.path}\x00${request} string, allocating a new key for every
require() call including fully cached ones. Key the cache by the
parent directory first (a Map keyed by the already-retained
module.path string) and then by the request (a dictionary object,
whose property access internalizes dynamically-constructed
specifiers). Faster on every measured workload shape and slightly
smaller in memory, since the concatenated keys are no longer
retained.

Signed-off-by: Sam Attard <[email protected]>
PR-URL: #63884
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
@leapmux-renovate leapmux-renovate Bot mentioned this pull request Nov 1, 2026
chore(deps): Update Node.js to v26 leapmux/leapmux#578
Merged
aduh95 pushed a commit that referenced this pull request Nov 11, 2026
@MarshallOfSound @aduh95
module: avoid allocating a cache key string for every require()
13c620f
The relative resolve cache was keyed by a concatenated
${parent.path}\x00${request} string, allocating a new key for every
require() call including fully cached ones. Key the cache by the
parent directory first (a Map keyed by the already-retained
module.path string) and then by the request (a dictionary object,
whose property access internalizes dynamically-constructed
specifiers). Faster on every measured workload shape and slightly
smaller in memory, since the concatenated keys are no longer
retained.

Signed-off-by: Sam Attard <[email protected]>
PR-URL: #63884
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Trivikram Kamat <[email protected]>
@github-actions github-actions Bot mentioned this pull request Nov 20, 2026
2026-11-30, Version 24.23.0 'Krypton' (LTS) #67663
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@mcollina mcollina mcollina approved these changes
@trivikr trivikr trivikr approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. commit-queue-squash PRs the Commit Queue should land as one squashed commit. module Issues and PRs related to the module subsystem. needs-ci PRs that need a full CI run.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@MarshallOfSound @nodejs-github-bot @mcollina @trivikr
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.