Skip to content

Commit 16a2968

Browse files
committed
perf: cache node z-indexes before legacy sorting
Amp-Thread-ID: https://ampcode.com/threads/T-01a02705-02f3-70c9-ae5b-8ea6c2a3cac2
1 parent 3bee137 commit 16a2968

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

src/renderer/core/canvas/litegraph/arrangeForLegacyRender.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { createTestingPinia } from '@pinia/testing'
22
import { setActivePinia } from 'pinia'
3-
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
3+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
44

55
import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph'
66
import { arrangeForLegacyRender } from '@/renderer/core/canvas/litegraph/arrangeForLegacyRender'
77
import { useLayoutMutations } from '@/renderer/core/layout/operations/layoutMutations'
8+
import { layoutStore } from '@/renderer/core/layout/store/layoutStore'
89
import { LayoutSource } from '@/renderer/core/layout/types'
910

1011
function addedNode(graph: LGraph) {
@@ -60,4 +61,15 @@ describe('arrangeForLegacyRender', () => {
6061

6162
expect(graph._nodes).toEqual([second, first])
6263
})
64+
65+
it('reads each node layout once while sorting', () => {
66+
const graph = new LGraph()
67+
const nodes = [addedNode(graph), addedNode(graph), addedNode(graph)]
68+
for (const node of nodes) node.flags.collapsed = true
69+
const getNodeLayout = vi.spyOn(layoutStore, 'getNodeLayout')
70+
71+
arrangeForLegacyRender(graph)
72+
73+
expect(getNodeLayout).toHaveBeenCalledTimes(nodes.length)
74+
})
6375
})

src/renderer/core/canvas/litegraph/arrangeForLegacyRender.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ const logger = log.getLogger('arrangeForLegacyRender')
1414
*/
1515
export function arrangeForLegacyRender(graph: LGraph): void {
1616
const rootGraphId = graph.rootGraph.id
17+
const zIndexByNode = new Map(
18+
graph._nodes.map((node) => [
19+
node,
20+
layoutStore.getNodeLayout(rootGraphId, node.id)?.zIndex ?? 0
21+
])
22+
)
1723
graph._nodes.sort(
18-
(a, b) =>
19-
(layoutStore.getNodeLayout(rootGraphId, a.id)?.zIndex ?? 0) -
20-
(layoutStore.getNodeLayout(rootGraphId, b.id)?.zIndex ?? 0)
24+
(a, b) => (zIndexByNode.get(a) ?? 0) - (zIndexByNode.get(b) ?? 0)
2125
)
2226

2327
for (const node of graph._nodes) {

0 commit comments

Comments
 (0)