Commit 7849455
committed
module: cache negative stat results in the CJS loader
The CommonJS loader keeps a per-require-tree `statCache` to avoid
re-stat-ing the same path while resolving a module tree, but it only
caches successful stats. Negative results (e.g. -ENOENT) fall through
and are re-probed every time the same missing path is looked up again
within the same top-level require.
These misses are extremely common during resolution and recur across
sibling and descendant modules: `tryExtensions` probes .js/.json/.node
in order (every extension before the real one is a miss), and bare
specifiers walk the node_modules chain upward through many non-existent
ancestor directories. None of these negatives were cached, so they were
re-stat-ed repeatedly within a single resolution pass.
Cache negative stat results alongside positive ones. The staleness
window is identical and already accepted for positive results: the
cache is tree-scoped, created when a top-level require begins
(requireDepth === 0) and cleared when it completes, so a stale entry
can only survive the duration of one top-level require.
Signed-off-by: Maxime David <maxday@amazon.com>1 parent db3a8d8 commit 7849455
2 files changed
Lines changed: 44 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
279 | 279 | | |
280 | 280 | | |
281 | 281 | | |
282 | | - | |
283 | | - | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
284 | 292 | | |
285 | 293 | | |
286 | 294 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
0 commit comments