perf(spy): reset only mocks that were called or reconfigured #11197
Openjohan-gorter wants to merge 1 commit into main from johan-gorter/perf-reset-all-mocks· opened 2 days ago
+50−15
johan-gorter commented 2 days agoAuthor
Description
vi.resetAllMocks() iterates REGISTERED_MOCKS, which holds a WeakRef to every mock ever created in
the worker, and calls mockReset() on each one that is still alive. Its cost therefore grows with the
number of live mocks, not with the number of mocks a test actually touched.
With isolate: false this becomes the dominant cost of a run. One worker executes many test files, so
module-level mocks and every vi.fn() that is still reachable keep accumulating. deref() also adds each
mock to the WeakRef "keep during job" list, so the mocks a test created cannot be collected until the
runner yields to the event loop, which rarely happens inside a file. With mockReset: true every test
then pays for every mock in the worker.
Measured on a large suite (364 files, 6733 tests, pool: 'forks', isolate: false, mockReset: true,
coverage off, 4 workers):
Vitest
wall time
resetAllMocks() CPU across workers
max mocks visited per reset
4.1.11 (Set of strong refs)
671 s
1838 s
~110,000
5.0.0 (WeakRef registry, #11092)
189 s
465 s
~66,000
5.0.0 + this change
26 s
0.4 s
5 to 50
This PR applies the approach #11092 introduced for clearAllMocks() to resetAllMocks():
DIRTY_MOCK_STATES already tracks mocks that were called since their last mockClear().
A new DIRTY_MOCK_CONFIGS set tracks mocks whose config changed since their last mockReset():
mockImplementation, mockImplementationOnce, withImplementation, and mockName add the mock,
mockReset() removes it. Every mockReturnValue*, mockResolvedValue*, mockRejectedValue*, and
mockThrow* helper goes through one of these.
resetAllMocks() iterates the two sets. mockReset() removes the mock from both, so a mock present in
both is visited once.
A mock that is in neither set is already in its reset state, so behavior is unchanged. clearAllMocks()
is unaffected: it still only touches DIRTY_MOCK_STATES, and a mock that was reconfigured but never
called stays in DIRTY_MOCK_CONFIGS until resetAllMocks() sees it (covered by the new tests).
With this change nothing reads REGISTERED_MOCKS anymore, so the WeakRef registry and the
FinalizationRegistry are removed. That also removes the per-mock WeakRef allocation and the deref()
pass that pinned every live mock for the rest of the job. If you prefer to keep the registry for future
use, that part of the diff can be dropped; the perf gain comes from the two loops in resetAllMocks().
Related: #9492, #11092.
AI disclosure: Claude (Claude Code) assisted with drafting the change, the tests and this description; the
diff was reviewed and measured by me.
johan-gorter added 1 commit 2 days ago
perf(spy): reset only mocks that were called or reconfiguredeadb2e9
Description
vi.resetAllMocks()iteratesREGISTERED_MOCKS, which holds aWeakRefto every mock ever created in the worker, and callsmockReset()on each one that is still alive. Its cost therefore grows with the number of live mocks, not with the number of mocks a test actually touched.With
isolate: falsethis becomes the dominant cost of a run. One worker executes many test files, so module-level mocks and everyvi.fn()that is still reachable keep accumulating.deref()also adds each mock to the WeakRef "keep during job" list, so the mocks a test created cannot be collected until the runner yields to the event loop, which rarely happens inside a file. WithmockReset: trueevery test then pays for every mock in the worker.Measured on a large suite (364 files, 6733 tests,
pool: 'forks',isolate: false,mockReset: true, coverage off, 4 workers):resetAllMocks()CPU across workersSetof strong refs)WeakRefregistry, #11092)This PR applies the approach #11092 introduced for
clearAllMocks()toresetAllMocks():DIRTY_MOCK_STATESalready tracks mocks that were called since their lastmockClear().DIRTY_MOCK_CONFIGSset tracks mocks whose config changed since their lastmockReset():mockImplementation,mockImplementationOnce,withImplementation, andmockNameadd the mock,mockReset()removes it. EverymockReturnValue*,mockResolvedValue*,mockRejectedValue*, andmockThrow*helper goes through one of these.resetAllMocks()iterates the two sets.mockReset()removes the mock from both, so a mock present in both is visited once.A mock that is in neither set is already in its reset state, so behavior is unchanged.
clearAllMocks()is unaffected: it still only touchesDIRTY_MOCK_STATES, and a mock that was reconfigured but never called stays inDIRTY_MOCK_CONFIGSuntilresetAllMocks()sees it (covered by the new tests).With this change nothing reads
REGISTERED_MOCKSanymore, so theWeakRefregistry and theFinalizationRegistryare removed. That also removes the per-mockWeakRefallocation and thederef()pass that pinned every live mock for the rest of the job. If you prefer to keep the registry for future use, that part of the diff can be dropped; the perf gain comes from the two loops inresetAllMocks().Related: #9492, #11092.
AI disclosure: Claude (Claude Code) assisted with drafting the change, the tests and this description; the diff was reviewed and measured by me.