From 5936e61c3c1ac8226a7525514504198ca71a2b36 Mon Sep 17 00:00:00 2001 From: Christian Byrne Date: Sun, 27 Sep 2026 21:56:46 -0700 Subject: [PATCH] perf: skip document-wide modal scans while typing (#19057) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - skip document-wide modal scans for native text-editing events while preserving modal handling for other inputs - honor store-backed auto-queue mode and cover native editor fast paths - measure composer typing during the workload, with two unmeasured type-and-clear warm-ups and named Playwright phases - close both frame-measurement boundaries and recover cleanly from page closure or bounded rAF timeouts This is the measured optimization following the original harness in [#19051](https://github.com/Comfy-Org/ComfyUI_frontend/pull/19051) and the protocol-compliant warmed baseline in [#19159](https://github.com/Comfy-Org/ComfyUI_frontend/pull/19159). - [Warmed baseline exact-head run](https://github.com/Comfy-Org/ComfyUI_frontend/actions/runs/36371643330): 46/46 localhost and 4/4 cloud performance tests passed; retained `perf-metrics` contains 49 measurements, including two samples for each agent-composer arm. - The current exact-head run compares the same 73-character workload after rebasing onto that warmed baseline. - The durable regression assertion verifies native contenteditable, input, and textarea events do not invoke the document-wide modal scan. - `pnpm vitest run src/scripts/changeTracker.test.ts` — 80 passed - `pnpm vitest run browser_tests/fixtures/helpers/PerformanceHelper.test.ts` — 2 passed - real Chromium immediate-block frame falsifier — 1 passed - `pnpm typecheck:browser` - focused formatting and lint checks - pre-push Knip and changed-file Fallow audit passed Backport targets: core/1.54, cloud/1.54, core/1.55, cloud/1.55. --------- Co-authored-by: Alexander Brown (cherry picked from commit ad31025727e30ca96772edfd54046d636f911789) --- src/scripts/changeTracker.test.ts | 72 +++++++++++++++++++++++++++++++ src/scripts/changeTracker.ts | 22 ++++++---- 2 files changed, 85 insertions(+), 9 deletions(-) diff --git a/src/scripts/changeTracker.test.ts b/src/scripts/changeTracker.test.ts index 13c2539fd1d..6c1bc211b83 100644 --- a/src/scripts/changeTracker.test.ts +++ b/src/scripts/changeTracker.test.ts @@ -314,6 +314,78 @@ describe('ChangeTracker', () => { expect(undo).toHaveBeenCalledTimes(undoCalls) } ) + + it.for([ + { editor: 'INPUT', createElement: () => document.createElement('input') }, + { + editor: 'TEXTAREA', + createElement: () => document.createElement('textarea') + }, + { + editor: 'contenteditable', + createElement: () => { + const element = document.createElement('div') + Object.defineProperty(element, 'isContentEditable', { value: true }) + return element + } + } + ])( + 'leaves $editor history to the editor without scanning modals', + ({ createElement }) => { + vi.spyOn(document, 'activeElement', 'get').mockReturnValue( + createElement() + ) + useQueueSettingsStore().mode = 'disabled' + const querySelectorAll = vi.spyOn(document, 'querySelectorAll') + const frames: FrameRequestCallback[] = [] + vi.spyOn(window, 'requestAnimationFrame').mockImplementation((frame) => + frames.push(frame) + ) + const addEventListener = vi + .spyOn(window, 'addEventListener') + .mockImplementation(() => {}) + ChangeTracker.init() + const keydown = addEventListener.mock.calls.find( + ([type]) => type === 'keydown' + )?.[1] + if (typeof keydown !== 'function') + throw new Error('keydown listener missing') + + keydown(new KeyboardEvent('keydown', { key: 'a' })) + + expect(querySelectorAll).not.toHaveBeenCalled() + expect(frames).toHaveLength(0) + } + ) + + it('captures editor changes when store-backed auto-queue uses change mode', async () => { + const editor = document.createElement('input') + vi.spyOn(document, 'activeElement', 'get').mockReturnValue(editor) + useQueueSettingsStore().mode = 'change' + app.ui.autoQueueEnabled = false + const tracker = createTracker(createState(1)) + mockCanvasState(createState(2)) + const frames: FrameRequestCallback[] = [] + vi.spyOn(window, 'requestAnimationFrame').mockImplementation((frame) => + frames.push(frame) + ) + const addEventListener = vi + .spyOn(window, 'addEventListener') + .mockImplementation(() => {}) + ChangeTracker.init() + const keydown = addEventListener.mock.calls.find( + ([type]) => type === 'keydown' + )?.[1] + if (typeof keydown !== 'function') + throw new Error('keydown listener missing') + + keydown(new KeyboardEvent('keydown', { key: 'a' })) + expect(frames).toHaveLength(1) + await frames[0](0) + + expect(dispatchedEventNames()).toContain('autoQueueGraphChanged') + expect(tracker.activeState.nodes).toHaveLength(2) + }) }) describe('captureCanvasState', () => { diff --git a/src/scripts/changeTracker.ts b/src/scripts/changeTracker.ts index 77f1b181aff..b2814b7a42c 100644 --- a/src/scripts/changeTracker.ts +++ b/src/scripts/changeTracker.ts @@ -531,24 +531,28 @@ export class ChangeTracker { // If the mask editor is opened, we don't want to trigger on key events const comfyApp = app.constructor as typeof ComfyApp if (comfyApp.maskeditor_is_opended?.()) return + + const activeEl = document.activeElement + if ( + !isAutoQueueOnChange() && + (activeEl?.tagName === 'INPUT' || + activeEl?.tagName === 'TEXTAREA' || + (activeEl instanceof HTMLElement && activeEl.isContentEditable)) + ) { + // Text editors own their history. Bail out before the document-wide + // modal probes, whose cost otherwise scales with transcript size. + return + } if (isModalOpen(dialogStore.dialogStack.length)) return // The layer editor has its own session-local undo history if (useDialogStore().isDialogOpen(LAYER_EDITOR_DIALOG_KEY)) return - const activeEl = document.activeElement const selectOnlyAtKeydown = isSelectOnly(app.canvas) requestAnimationFrame(async () => { let bindInputEl: Element | null = null // If we are auto queue in change mode then we do want to trigger on inputs - if (!app.ui.autoQueueEnabled || app.ui.autoQueueMode === 'instant') { - if ( - activeEl?.tagName === 'INPUT' || - (activeEl && 'type' in activeEl && activeEl.type === 'textarea') - ) { - // Ignore events on inputs, they have their native history - return - } + if (!isAutoQueueOnChange()) { bindInputEl = activeEl }