-
Notifications
You must be signed in to change notification settings - Fork 493
perf: virtualize FormDropdownMenu to reduce DOM nodes and image requests #8476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
christian-byrne
wants to merge
2
commits into
main
Choose a base branch
from
perf/image-dropdown-assets-perf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+419
−46
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
browser_tests/tests/vueNodes/widgets/imageDropdownVirtualization.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { | ||
| comfyExpect as expect, | ||
| comfyPageFixture as test | ||
| } from '../../../fixtures/ComfyPage' | ||
|
|
||
| test.describe('Image Dropdown Virtualization', { tag: '@widget' }, () => { | ||
| test.beforeEach(async ({ comfyPage }) => { | ||
| await comfyPage.setSetting('Comfy.VueNodes.Enabled', true) | ||
| await comfyPage.vueNodes.waitForNodes() | ||
| }) | ||
|
|
||
| test('should virtualize items when dropdown has many entries', async ({ | ||
| comfyPage | ||
| }) => { | ||
| await comfyPage.loadWorkflow('widgets/load_image_widget') | ||
| await comfyPage.vueNodes.waitForNodes() | ||
|
|
||
| const totalItems = await comfyPage.page.evaluate(() => { | ||
| const node = window['graph']._nodes_by_id['10'] | ||
| const widget = node.widgets.find( | ||
| (w: { name: string }) => w.name === 'image' | ||
| ) | ||
| const count = 60 | ||
| const values = Array.from( | ||
| { length: count }, | ||
| (_, i) => `test_image_${i}.png` | ||
| ) | ||
| widget.options.values = values | ||
| widget.value = values[0] | ||
| return count | ||
| }) | ||
|
|
||
| const loadImageNode = comfyPage.vueNodes.getNodeByTitle('Load Image') | ||
| const dropdownButton = loadImageNode.locator( | ||
| 'button:has(span:has-text("test_image_0.png"))' | ||
| ) | ||
| await dropdownButton.waitFor({ state: 'visible' }) | ||
| await dropdownButton.click() | ||
|
|
||
| const virtualGridItems = comfyPage.page.locator('[data-virtual-grid-item]') | ||
| await expect(virtualGridItems.first()).toBeVisible() | ||
|
|
||
| const renderedCount = await virtualGridItems.count() | ||
| expect(renderedCount).toBeLessThan(totalItems) | ||
| expect(renderedCount).toBeGreaterThan(0) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| import { mount } from '@vue/test-utils' | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { nextTick, ref } from 'vue' | ||
|
|
||
| import VirtualGrid from './VirtualGrid.vue' | ||
|
|
||
| type TestItem = { key: string; name: string } | ||
|
|
||
| const mockedWidth = ref(400) | ||
| const mockedHeight = ref(200) | ||
| const mockedScrollY = ref(0) | ||
|
|
||
| vi.mock('@vueuse/core', async () => { | ||
| const actual = await vi.importActual<Record<string, unknown>>('@vueuse/core') | ||
| return { | ||
| ...actual, | ||
| useElementSize: () => ({ width: mockedWidth, height: mockedHeight }), | ||
| useScroll: () => ({ y: mockedScrollY }) | ||
| } | ||
| }) | ||
|
|
||
| function createItems(count: number): TestItem[] { | ||
| return Array.from({ length: count }, (_, i) => ({ | ||
| key: `item-${i}`, | ||
| name: `Item ${i}` | ||
| })) | ||
| } | ||
|
|
||
| describe('VirtualGrid', () => { | ||
| const defaultGridStyle = { | ||
| display: 'grid', | ||
| gridTemplateColumns: 'repeat(4, 1fr)', | ||
| gap: '1rem' | ||
| } | ||
|
|
||
| it('renders items within the visible range', async () => { | ||
| const items = createItems(100) | ||
| mockedWidth.value = 400 | ||
| mockedHeight.value = 200 | ||
| mockedScrollY.value = 0 | ||
|
|
||
| const wrapper = mount(VirtualGrid<TestItem>, { | ||
| props: { | ||
| items, | ||
| gridStyle: defaultGridStyle, | ||
| defaultItemHeight: 100, | ||
| defaultItemWidth: 100, | ||
| maxColumns: 4, | ||
| bufferRows: 1 | ||
| }, | ||
| slots: { | ||
| item: `<template #item="{ item }"> | ||
| <div class="test-item">{{ item.name }}</div> | ||
| </template>` | ||
| }, | ||
| attachTo: document.body | ||
| }) | ||
|
|
||
| await nextTick() | ||
|
|
||
| const renderedItems = wrapper.findAll('.test-item') | ||
| expect(renderedItems.length).toBeGreaterThan(0) | ||
| expect(renderedItems.length).toBeLessThan(items.length) | ||
|
|
||
| wrapper.unmount() | ||
| }) | ||
|
|
||
| it('provides correct index in slot props', async () => { | ||
| const items = createItems(20) | ||
| const receivedIndices: number[] = [] | ||
| mockedWidth.value = 400 | ||
| mockedHeight.value = 200 | ||
| mockedScrollY.value = 0 | ||
|
|
||
| const wrapper = mount(VirtualGrid<TestItem>, { | ||
| props: { | ||
| items, | ||
| gridStyle: defaultGridStyle, | ||
| defaultItemHeight: 50, | ||
| defaultItemWidth: 100, | ||
| maxColumns: 1, | ||
| bufferRows: 0 | ||
| }, | ||
| slots: { | ||
| item: ({ index }: { index: number }) => { | ||
| receivedIndices.push(index) | ||
| return null | ||
| } | ||
| }, | ||
| attachTo: document.body | ||
| }) | ||
|
|
||
| await nextTick() | ||
|
|
||
| expect(receivedIndices.length).toBeGreaterThan(0) | ||
| expect(receivedIndices[0]).toBe(0) | ||
| for (let i = 1; i < receivedIndices.length; i++) { | ||
| expect(receivedIndices[i]).toBe(receivedIndices[i - 1] + 1) | ||
| } | ||
|
|
||
| wrapper.unmount() | ||
| }) | ||
|
|
||
| it('respects maxColumns prop', async () => { | ||
| const items = createItems(10) | ||
| mockedWidth.value = 400 | ||
| mockedHeight.value = 200 | ||
| mockedScrollY.value = 0 | ||
|
|
||
| const wrapper = mount(VirtualGrid<TestItem>, { | ||
| props: { | ||
| items, | ||
| gridStyle: defaultGridStyle, | ||
| maxColumns: 2 | ||
| }, | ||
| attachTo: document.body | ||
| }) | ||
|
|
||
| await nextTick() | ||
|
|
||
| const gridElement = wrapper.find('[style*="display: grid"]') | ||
| expect(gridElement.exists()).toBe(true) | ||
|
|
||
| const gridEl = gridElement.element as HTMLElement | ||
| expect(gridEl.style.gridTemplateColumns).toBe('repeat(2, minmax(0, 1fr))') | ||
|
|
||
| wrapper.unmount() | ||
| }) | ||
LittleSound marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| it('renders empty when no items provided', async () => { | ||
| const wrapper = mount(VirtualGrid<TestItem>, { | ||
| props: { | ||
| items: [], | ||
| gridStyle: defaultGridStyle | ||
| }, | ||
| slots: { | ||
| item: `<template #item="{ item }"> | ||
| <div class="test-item">{{ item.name }}</div> | ||
| </template>` | ||
| } | ||
| }) | ||
|
|
||
| await nextTick() | ||
|
|
||
| const renderedItems = wrapper.findAll('.test-item') | ||
| expect(renderedItems.length).toBe(0) | ||
| }) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| it('forces cols to maxColumns when maxColumns is finite', async () => { | ||
| mockedWidth.value = 100 | ||
| mockedHeight.value = 200 | ||
| mockedScrollY.value = 0 | ||
|
|
||
| const items = createItems(20) | ||
| const wrapper = mount(VirtualGrid<TestItem>, { | ||
| props: { | ||
| items, | ||
| gridStyle: defaultGridStyle, | ||
| defaultItemHeight: 50, | ||
| defaultItemWidth: 200, | ||
| maxColumns: 4, | ||
| bufferRows: 0 | ||
| }, | ||
| slots: { | ||
| item: `<template #item="{ item }"> | ||
| <div class="test-item">{{ item.name }}</div> | ||
| </template>` | ||
| }, | ||
| attachTo: document.body | ||
| }) | ||
|
|
||
| await nextTick() | ||
|
|
||
| const renderedItems = wrapper.findAll('.test-item') | ||
| expect(renderedItems.length).toBeGreaterThan(0) | ||
| expect(renderedItems.length % 4).toBe(0) | ||
|
|
||
| wrapper.unmount() | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/renderer/extensions/vueNodes/widgets/components/form/dropdown/FormDropdownMenu.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| import { mount } from '@vue/test-utils' | ||
| import { describe, expect, it } from 'vitest' | ||
| import { nextTick } from 'vue' | ||
|
|
||
| import FormDropdownMenu from './FormDropdownMenu.vue' | ||
| import type { FormDropdownItem, LayoutMode } from './types' | ||
|
|
||
| function createItem(id: string, name: string): FormDropdownItem { | ||
| return { | ||
| id, | ||
| preview_url: '', | ||
| name, | ||
| label: name | ||
| } | ||
| } | ||
|
|
||
| describe('FormDropdownMenu', () => { | ||
| const defaultProps = { | ||
| items: [createItem('1', 'Item 1'), createItem('2', 'Item 2')], | ||
| isSelected: () => false, | ||
| filterOptions: [], | ||
| sortOptions: [] | ||
| } | ||
|
|
||
| it('renders empty state when no items', async () => { | ||
| const wrapper = mount(FormDropdownMenu, { | ||
| props: { | ||
| ...defaultProps, | ||
| items: [] | ||
| }, | ||
| global: { | ||
| stubs: { | ||
| FormDropdownMenuFilter: true, | ||
| FormDropdownMenuActions: true, | ||
| VirtualGrid: true | ||
| }, | ||
| mocks: { | ||
| $t: (key: string) => key | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| await nextTick() | ||
|
|
||
| const emptyIcon = wrapper.find('.icon-\\[lucide--circle-off\\]') | ||
| expect(emptyIcon.exists()).toBe(true) | ||
| }) | ||
|
|
||
| it('renders VirtualGrid when items exist', async () => { | ||
| const wrapper = mount(FormDropdownMenu, { | ||
| props: defaultProps, | ||
| global: { | ||
| stubs: { | ||
| FormDropdownMenuFilter: true, | ||
| FormDropdownMenuActions: true, | ||
| VirtualGrid: true | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| await nextTick() | ||
|
|
||
| const virtualGrid = wrapper.findComponent({ name: 'VirtualGrid' }) | ||
| expect(virtualGrid.exists()).toBe(true) | ||
| }) | ||
|
|
||
| it('transforms items to include key property for VirtualGrid', async () => { | ||
| const items = [createItem('1', 'Item 1'), createItem('2', 'Item 2')] | ||
| const wrapper = mount(FormDropdownMenu, { | ||
| props: { | ||
| ...defaultProps, | ||
| items | ||
| }, | ||
| global: { | ||
| stubs: { | ||
| FormDropdownMenuFilter: true, | ||
| FormDropdownMenuActions: true, | ||
| VirtualGrid: true | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| await nextTick() | ||
|
|
||
| const virtualGrid = wrapper.findComponent({ name: 'VirtualGrid' }) | ||
| const virtualItems = virtualGrid.props('items') | ||
|
|
||
| expect(virtualItems).toHaveLength(2) | ||
| expect(virtualItems[0]).toHaveProperty('key', '1') | ||
| expect(virtualItems[1]).toHaveProperty('key', '2') | ||
| }) | ||
|
|
||
| it('uses single column layout for list modes', async () => { | ||
| const wrapper = mount(FormDropdownMenu, { | ||
| props: { | ||
| ...defaultProps, | ||
| layoutMode: 'list' as LayoutMode | ||
| }, | ||
| global: { | ||
| stubs: { | ||
| FormDropdownMenuFilter: true, | ||
| FormDropdownMenuActions: true, | ||
| VirtualGrid: true | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| await nextTick() | ||
|
|
||
| const virtualGrid = wrapper.findComponent({ name: 'VirtualGrid' }) | ||
| expect(virtualGrid.props('maxColumns')).toBe(1) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.