Skip to content

Check installed files on disk once per hot test file - #974

Open
danfry1 wants to merge 4 commits into
mainfrom
935-hot-within-file-repeats
Open

danfry1 wants to merge 4 commits into
mainfrom
935-hot-within-file-repeats

Conversation

@danfry1

@danfry1 danfry1 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Summary

Under the hot runtime, each installed package's file is checked on disk once per test file instead of on every load. On Rocket.Chat, the engine's repeated checks within a test file drop from 316,738 to 22,936 (−93%), with identical test results.

Why

A hot worker re-stat'd and re-read installed files it had already checked earlier in the same test file. It did this for the resolution memo's hit check, the compiled output's key, and the ESM loader's source comparison. Within one test file these answers can't change in a way the engine must notice. Jest keeps the same per-file view: its Runtime holds one file-content cache per test file (_cacheFS, created in jest-runner's runTest.js).

Changes

  • src/native/file-facts.mjs holds the stat and the bytes of files under node_modules until the hot generation advances at the next test file. The three call sites read through it.
  • Project files stay fresh within a test file, and so does every file without the hot generation (per-file workers).
  • Under the hot runtime, the within-file edit and removal cases from perf(native): stop re-resolving and re-reading a re-loaded library graph in every test file #364 are now cross-file pairs (hot runtime and freshRuntime): a change made in one file is seen by the next. Without the hot runtime the removal cases still run within one file.
  • A hot jest-compat test pins that jest.resetModules() keeps installed packages loaded, which is why the memo isn't cleared there.

Notes

  • Behaviour change: editing or removing an installed file mid-file is seen from the next test file on. The changeset gives the action.
  • Jest empties its file cache on jest.resetModules() too. That makes no difference here, because a reset never loads an installed file again (documented in the jest-compat guide). Jest's resolver caches last the whole run (jest-resolve fileWalkers.js), so resolutions here are fresher than Jest's.
  • About 20k loader reads remain on Rocket.Chat: one per path per test file. They compare Node's just-read source with the file; removing them is out of scope.

Fixes #935.

Under the hot runtime a worker re-stat'd and re-read installed files it had
already checked in the same test file: the resolution memo's hit stat, the
transform's key stat, and the ESM loader's source comparison. These answers are
now kept per test file for paths under node_modules and forgotten when the hot
generation advances at the next file's start.
Installed files are checked once per hot test file, as Jest keeps one file
cache per test file. The within-file edit and removal cases become cross-file
pairs under the hot runtime and freshRuntime, and the changeset records the
behaviour change.
The per-file memo of installed files is not cleared at jest.resetModules(),
because a reset renews project modules only and never loads an installed file
again. A hot jest-compat test pins that, so a change to the reset's scope must
revisit the memo. The memo's comment states the comparison with Jest.
They prove that a load outside the hot runtime still goes to the disk, so they
run there and are skipped only under the hot runtime, where the boundary pairs
cover removal. Rewrap a comment and state which Node versions the import test's
read count applies to.
@danfry1
danfry1 marked this pull request as ready for review December 2, 2026 23:09

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hot workers re-check and re-read files already handled in the same test file

1 participant