Repository navigation
perf: add viewport culling to reduce Vue node mount/unmount GC churn #10481
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
Draft
christian-byrne
wants to merge
8
commits into
main
Choose a base branch
from
perf/fix-viewport-gc-churn
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.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ae51e7b
test: add perf test for viewport pan sweep GC churn
christian-byrne c5dd55e
fix: correct domNodes log label — absolute count, not delta
christian-byrne aaa5faf
perf: add viewport culling to reduce Vue node mount/unmount GC churn
christian-byrne 08b357a
perf: skip shallowRef update when mounted node set is unchanged
christian-byrne ca0e41a
fix: add ResizeObserver on canvas element for sidebar/splitter resizes
christian-byrne 84fad7f
fix: resolve merge conflict with main in performance.spec.ts
christian-byrne 3517e14
Revert performance.spec.ts to pre-merge state
christian-byrne 11a52c3
Merge origin/main into perf/fix-viewport-gc-churn
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
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
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,270 @@ | ||
| import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
| import { computed, nextTick, ref } from 'vue' | ||
|
|
||
| import type { NodeId, NodeLayout } from '@/renderer/core/layout/types' | ||
| import type { NodeState } from '@/types/nodeState' | ||
| import type { UUID } from '@/utils/uuid' | ||
|
|
||
| const mockIsNodeInViewport = vi.fn() | ||
| const mockGetNodeLayout = vi.fn() | ||
|
|
||
| vi.mock('@/renderer/core/layout/transform/useTransformState', () => ({ | ||
| useTransformState: () => ({ | ||
| isNodeInViewport: mockIsNodeInViewport, | ||
| camera: { x: 0, y: 0, z: 1 } | ||
| }) | ||
| })) | ||
|
|
||
| vi.mock('@/renderer/core/layout/store/layoutStore', () => ({ | ||
| layoutStore: { | ||
| getNodeLayout: (...args: unknown[]) => mockGetNodeLayout(...args), | ||
| get nodeGeometryVersion() { | ||
| return mockGeometryVersion.value | ||
| } | ||
| } | ||
| })) | ||
|
|
||
| const mockGeometryVersion = ref(0) | ||
|
|
||
| // Must import after mock setup | ||
| const { useViewportCulling } = | ||
| await import('@/composables/graph/useViewportCulling') | ||
|
|
||
| const ROOT_GRAPH_ID = 'root-graph' as UUID | ||
|
|
||
| function makeNode(id: string): NodeState { | ||
| return { | ||
| id: id as NodeId, | ||
| graphId: ROOT_GRAPH_ID as unknown as NodeState['graphId'], | ||
| title: `Node ${id}`, | ||
| type: 'test', | ||
| mode: 0, | ||
| flags: {}, | ||
| inputs: [], | ||
| outputs: [], | ||
| properties: {} | ||
| } | ||
| } | ||
|
|
||
| let layoutCounter = 0 | ||
|
|
||
| function makeLayout(x: number, y: number, w = 200, h = 100): NodeLayout { | ||
| return { | ||
| id: `layout-${layoutCounter++}` as NodeId, | ||
| position: { x, y }, | ||
| size: { width: w, height: h }, | ||
| bounds: { x, y, width: w, height: h }, | ||
| zIndex: 0, | ||
| visible: true | ||
| } | ||
| } | ||
|
|
||
| /** Wires the getNodeLayout mock to look up layouts from a plain id->layout map. */ | ||
| function useLayouts(layouts: Map<string, NodeLayout>) { | ||
| mockGetNodeLayout.mockImplementation((_graphId: UUID, nodeId: NodeId) => | ||
| layouts.get(nodeId as unknown as string) | ||
| ) | ||
| } | ||
|
|
||
| describe('useViewportCulling', () => { | ||
| const isTransforming = ref(false) | ||
| const viewport = { width: 1000, height: 600 } | ||
| const rootGraphId = computed<UUID | undefined>(() => ROOT_GRAPH_ID) | ||
|
|
||
| beforeEach(() => { | ||
| vi.useFakeTimers() | ||
| isTransforming.value = false | ||
| mockGeometryVersion.value = 0 | ||
| mockIsNodeInViewport.mockReturnValue(true) | ||
| mockGetNodeLayout.mockReturnValue(undefined) | ||
| }) | ||
|
|
||
| it('mounts all nodes when all are visible', () => { | ||
| const nodes = [makeNode('1'), makeNode('2'), makeNode('3')] | ||
| useLayouts( | ||
| new Map([ | ||
| ['1', makeLayout(100, 100)], | ||
| ['2', makeLayout(300, 100)], | ||
| ['3', makeLayout(500, 100)] | ||
| ]) | ||
| ) | ||
|
|
||
| const { mountedNodeIds } = useViewportCulling({ | ||
| rawNodes: computed(() => nodes), | ||
| rootGraphId, | ||
| getViewportSize: () => viewport, | ||
| isTransforming | ||
| }) | ||
|
|
||
| expect(mountedNodeIds.value.size).toBe(3) | ||
| expect(mountedNodeIds.value.has('1')).toBe(true) | ||
| expect(mountedNodeIds.value.has('2')).toBe(true) | ||
| expect(mountedNodeIds.value.has('3')).toBe(true) | ||
| }) | ||
|
|
||
| it('culls nodes outside the viewport after debounce', async () => { | ||
| const nodes = [makeNode('1'), makeNode('2')] | ||
| useLayouts( | ||
| new Map([ | ||
| ['1', makeLayout(100, 100)], | ||
| ['2', makeLayout(5000, 5000)] | ||
| ]) | ||
| ) | ||
|
|
||
| mockIsNodeInViewport.mockImplementation( | ||
| (pos: [number, number]) => pos[0] < 2000 && pos[1] < 2000 | ||
| ) | ||
|
|
||
| const { mountedNodeIds } = useViewportCulling({ | ||
| rawNodes: computed(() => nodes), | ||
| rootGraphId, | ||
| getViewportSize: () => viewport, | ||
| isTransforming | ||
| }) | ||
|
|
||
| // Node 2 is outside viewport, but hasn't been pruned yet on | ||
| // initial mount since it was never in the set to begin with | ||
| // and computeVisibleNodeIds runs immediately | ||
| expect(mountedNodeIds.value.has('1')).toBe(true) | ||
| expect(mountedNodeIds.value.has('2')).toBe(false) | ||
| }) | ||
|
|
||
| it('mounts nodes without layout data', () => { | ||
| const nodes = [makeNode('1'), makeNode('2')] | ||
| useLayouts( | ||
| new Map([ | ||
| ['1', makeLayout(100, 100)] | ||
| // Node '2' has no layout | ||
| ]) | ||
| ) | ||
|
|
||
| mockIsNodeInViewport.mockReturnValue(true) | ||
|
|
||
| const { mountedNodeIds } = useViewportCulling({ | ||
| rawNodes: computed(() => nodes), | ||
| rootGraphId, | ||
| getViewportSize: () => viewport, | ||
| isTransforming | ||
| }) | ||
|
|
||
| expect(mountedNodeIds.value.has('1')).toBe(true) | ||
| expect(mountedNodeIds.value.has('2')).toBe(true) | ||
| }) | ||
|
|
||
| it('mounts all nodes when viewport size is zero', () => { | ||
| const nodes = [makeNode('1'), makeNode('2')] | ||
| useLayouts( | ||
| new Map([ | ||
| ['1', makeLayout(100, 100)], | ||
| ['2', makeLayout(5000, 5000)] | ||
| ]) | ||
| ) | ||
|
|
||
| mockIsNodeInViewport.mockReturnValue(false) | ||
|
|
||
| const { mountedNodeIds } = useViewportCulling({ | ||
| rawNodes: computed(() => nodes), | ||
| rootGraphId, | ||
| getViewportSize: () => ({ width: 0, height: 0 }), | ||
| isTransforming | ||
| }) | ||
|
|
||
| expect(mountedNodeIds.value.size).toBe(2) | ||
| }) | ||
|
|
||
| it('delays unmounting nodes that leave the viewport', async () => { | ||
| const nodes = [makeNode('1'), makeNode('2')] | ||
| const layouts = new Map([ | ||
| ['1', makeLayout(100, 100)], | ||
| ['2', makeLayout(300, 100)] | ||
| ]) | ||
| useLayouts(layouts) | ||
|
|
||
| mockIsNodeInViewport.mockReturnValue(true) | ||
|
|
||
| const rawNodes = ref(nodes) | ||
|
|
||
| const { mountedNodeIds } = useViewportCulling({ | ||
| rawNodes: computed(() => rawNodes.value), | ||
| rootGraphId, | ||
| getViewportSize: () => viewport, | ||
| isTransforming | ||
| }) | ||
|
|
||
| expect(mountedNodeIds.value.size).toBe(2) | ||
|
|
||
| // Node 2 leaves viewport | ||
| mockIsNodeInViewport.mockImplementation( | ||
| (pos: [number, number]) => pos[0] < 200 | ||
| ) | ||
|
|
||
| // Trigger a refresh by bumping the geometry version | ||
| mockGeometryVersion.value++ | ||
| await nextTick() | ||
|
|
||
| // Node 2 should still be mounted (debounce hasn't fired) | ||
| expect(mountedNodeIds.value.has('2')).toBe(true) | ||
|
|
||
| // After debounce delay, node 2 should be unmounted | ||
| await vi.advanceTimersByTimeAsync(300) | ||
|
|
||
| expect(mountedNodeIds.value.has('1')).toBe(true) | ||
| expect(mountedNodeIds.value.has('2')).toBe(false) | ||
| }) | ||
|
|
||
| it('immediately mounts nodes entering the viewport', async () => { | ||
| const nodes = [makeNode('1'), makeNode('2')] | ||
| const layouts = new Map([ | ||
| ['1', makeLayout(100, 100)], | ||
| ['2', makeLayout(5000, 5000)] | ||
| ]) | ||
| useLayouts(layouts) | ||
|
|
||
| mockIsNodeInViewport.mockImplementation( | ||
| (pos: [number, number]) => pos[0] < 2000 | ||
| ) | ||
|
|
||
| const rawNodes = ref(nodes) | ||
|
|
||
| const { mountedNodeIds } = useViewportCulling({ | ||
| rawNodes: computed(() => rawNodes.value), | ||
| rootGraphId, | ||
| getViewportSize: () => viewport, | ||
| isTransforming | ||
| }) | ||
|
|
||
| expect(mountedNodeIds.value.has('2')).toBe(false) | ||
|
|
||
| // Node 2 enters viewport | ||
| mockIsNodeInViewport.mockReturnValue(true) | ||
| mockGeometryVersion.value++ | ||
| await nextTick() | ||
|
|
||
| // Should be immediately mounted without waiting for debounce | ||
| expect(mountedNodeIds.value.has('2')).toBe(true) | ||
| }) | ||
|
|
||
| it('handles new nodes being added to the graph', async () => { | ||
| const nodes = ref([makeNode('1')]) | ||
| const layouts = new Map([['1', makeLayout(100, 100)]]) | ||
| useLayouts(layouts) | ||
|
|
||
| mockIsNodeInViewport.mockReturnValue(true) | ||
|
|
||
| const { mountedNodeIds } = useViewportCulling({ | ||
| rawNodes: computed(() => nodes.value), | ||
| rootGraphId, | ||
| getViewportSize: () => viewport, | ||
| isTransforming | ||
| }) | ||
|
|
||
| expect(mountedNodeIds.value.size).toBe(1) | ||
|
|
||
| // Add a new node | ||
| nodes.value = [...nodes.value, makeNode('2')] | ||
| layouts.set('2', makeLayout(200, 200)) | ||
| await nextTick() | ||
|
|
||
| expect(mountedNodeIds.value.has('2')).toBe(true) | ||
| }) | ||
| }) |
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.