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):

Vitestwall timeresetAllMocks() CPU across workersmax mocks visited per reset
4.1.11 (Set of strong refs)671 s1838 s~110,000
5.0.0 (WeakRef registry, #11092)189 s465 s~66,000
5.0.0 + this change26 s0.4 s5 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

Review required

Review the changes, then submit to get graded.

Some checks haven’t completed yet

1 check: dojo / review-grader

Keyboard shortcuts

Site-wide

Focus the search bar
/
Show this dialog
?

Pull request

Move to the next file
j
Move to the previous file
k
Filter changed files
t
Comment on the selected lines
c
Select a range of lines
Shiftclick