Repository navigation
Conversation
When resolve hooks are registered with module.registerHooks(), every require() that misses the relative resolve cache converts the filename from the default resolver to a URL for the hooks with pathToFileURL(), then converts the URL returned by the hooks back with fileURLToPath(). The same file is usually resolved from many parent directories, so the same conversions repeat. Cache both conversions per filename returned by the default resolver. The filename for the URL returned by the hooks is only taken from the cache when that URL is the one the default resolver produced, so hooks that return a different URL are converted as before. Rooted paths without a drive letter on Windows are not cached, as their conversion depends on the current working directory. Signed-off-by: Domas Bitvinskas <domas.bitvinskas@me.com>
|
Review requested:
|
|
Welcome to Node.js, and thank you for your first contribution! Before review, please take a moment to read:
Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal. Caution AgentScan found account activity patterns that may be consistent with automation. This is a heuristic, not proof that this pull request was opened by an agent or violates policy. AI-assisted contributions are permitted, but automated tooling must not open pull requests without advance approval, and contributors must personally understand, test, verify, and take responsibility for every submitted change. See the AgentScan analysis, AI use policy, and automation policy for additional context. |
When resolve hooks are registered with
module.registerHooks(), everyrequire()that misses the relative resolve cache converts the filename from the default resolver to a URL withpathToFileURL()for the hooks, then converts the URL returned by the hooks back withfileURLToPath(). The same file is usually resolved from many parent directories, so the same conversions repeat: requiringjsdom,@babel/core,eslint/use-at-your-own-riskandwebpackresolves 3,198 files through the hooks, of which 1,126 are unique.This caches both conversions per filename returned by the default resolver. The filename for the URL returned by the hooks is only taken from the cache when that URL is the one the default resolver produced, so hooks that return a different URL are converted as before. Rooted paths without a drive letter on Windows are not cached, as their conversion depends on the current working directory.
Benchmark added in
benchmark/module/module-require-hooks.js(1,000 parent directories each requiring the same 10 packages, with and without a pass-through resolve hook), macOS arm64, 30 runs:The pass-through hook went from 13.7% slower than no hooks to 3.0% slower. On the package graph above, the median time to require it with a pass-through resolve hook went from 177.6 ms to 170.5-173.9 ms (166.5 ms without hooks).