module: avoid allocating a cache key string for every require()#63884
Open
MarshallOfSound wants to merge 1 commit into
Open
module: avoid allocating a cache key string for every require()#63884MarshallOfSound wants to merge 1 commit into
MarshallOfSound wants to merge 1 commit into
Conversation
Collaborator
|
Review requested:
|
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 <sattard@anthropic.com>
147ca94 to
96860f3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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 inModule._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
SafeMapkeyed byparent.path(a string the module system already retains, with a V8-cached hash) and an inner{ __proto__: null }object keyed by the raw request.Mapon purpose: property access internalizes the key, which handles dynamically built specifiers (require(base + name)) far better thanMap.get's per-call hash-and-compare — and it preserves the string coercion the old template literal performed.finally, anddelete require.cache[...]+ re-require behaves as before.Observable edge:
parent.pathis now an identity Map key rather than being string-coerced, and is read twice on the first load into a directory — only reachable ifmodule.pathis replaced with a non-string or an accessor, neither of which is documented usage.benchmark/compare.jsshows significant wins on every relevantmodule-loader.jsconfig — 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):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
test/parallel/test-module-*,test-require-*,test-cjs-*passdelete require.cache+ re-require, failed-load retry, circular requires,require.resolvewithpaths