Skip to content

AI-native design editor. Open-source Figma alternative.

perf(kiwi): cache instance override resolution

Danila Poyarkov04b425f64c29
diff --git a/packages/core/src/kiwi/instance-overrides/dsd.ts b/packages/core/src/kiwi/instance-overrides/dsd.ts
index 0a9e22f5c..5eefcc37b 100644
--- a/packages/core/src/kiwi/instance-overrides/dsd.ts
+++ b/packages/core/src/kiwi/instance-overrides/dsd.ts
@@ -64,13 +64,33 @@ function resolveDsdGeometry(
return result
}
-function hasSingleVisibleSibling(ctx: OverrideContext, node: SceneNode): boolean {
+function getVisibleSiblingCount(
+ ctx: OverrideContext,
+ cache: Map<string, number>,
+ parentId: string
+): number {
+ const cached = cache.get(parentId)
+ if (cached !== undefined) return cached
+ const count = ctx.graph.getChildren(parentId).filter((child) => child.visible).length
+ cache.set(parentId, count)
+ return count
+}
+
+function hasSingleVisibleSibling(
+ ctx: OverrideContext,
+ visibleSiblingCount: Map<string, number>,
+ node: SceneNode
+): boolean {
if (!node.parentId) return false
- return ctx.graph.getChildren(node.parentId).filter((child) => child.visible).length === 1
+ return getVisibleSiblingCount(ctx, visibleSiblingCount, node.parentId) === 1
}
-function resolveSizeOnlyPosition(ctx: OverrideContext, node: SceneNode): Pick<SceneNode, 'x' | 'y'> | null {
- if (!hasSingleVisibleSibling(ctx, node) || !node.componentId) return null
+function resolveSizeOnlyPosition(
+ ctx: OverrideContext,
+ visibleSiblingCount: Map<string, number>,
+ node: SceneNode
+): Pick<SceneNode, 'x' | 'y'> | null {
+ if (!hasSingleVisibleSibling(ctx, visibleSiblingCount, node) || !node.componentId) return null
const source = ctx.graph.getNode(node.componentId)
if (!source) return null
@@ -87,6 +107,7 @@ function resolveSizeOnlyPosition(ctx: OverrideContext, node: SceneNode): Pick<Sc
function buildDsdLayoutUpdates(
ctx: OverrideContext,
+ visibleSiblingCount: Map<string, number>,
d: DerivedSymbolOverride,
target: SceneNode
): { updates: Partial<SceneNode>; hasSize: boolean } {
@@ -105,7 +126,7 @@ function buildDsdLayoutUpdates(
figmaDerivedLayout.x = d.transform.m02
figmaDerivedLayout.y = d.transform.m12
} else if (d.size) {
- const position = resolveSizeOnlyPosition(ctx, target)
+ const position = resolveSizeOnlyPosition(ctx, visibleSiblingCount, target)
if (position) {
updates.x = position.x
updates.y = position.y
@@ -124,6 +145,7 @@ function buildDsdLayoutUpdates(
function resolveDsdUpdates(ctx: OverrideContext): { modified: Set<string>; sizeSet: Set<string> } {
const modified = new Set<string>()
const sizeSet = new Set<string>()
+ const visibleSiblingCount = new Map<string, number>()
for (const [ncId, nc] of ctx.changeMap) {
if (nc.type !== 'INSTANCE') continue
@@ -143,7 +165,7 @@ function resolveDsdUpdates(ctx: OverrideContext): { modified: Set<string>; sizeS
const target = ctx.graph.getNode(targetId)
if (!target) continue
- const { updates, hasSize } = buildDsdLayoutUpdates(ctx, d, target)
+ const { updates, hasSize } = buildDsdLayoutUpdates(ctx, visibleSiblingCount, d, target)
if (d.fillGeometry?.length || d.strokeGeometry?.length) {
ctx.geometryOverrideNodes.add(targetId)
}
diff --git a/packages/core/src/kiwi/instance-overrides/populate.ts b/packages/core/src/kiwi/instance-overrides/populate.ts
index 2cdecc3c1..ff011dfe8 100644
--- a/packages/core/src/kiwi/instance-overrides/populate.ts
+++ b/packages/core/src/kiwi/instance-overrides/populate.ts
@@ -34,24 +34,9 @@ export function populateInstances(graph: SceneGraph): void {
}
}
- for (const node of graph.getAllNodes()) {
+ for (const node of graph.nodes.values()) {
if (node.type === 'INSTANCE' && node.componentId && node.childIds.length === 0) {
ensurePopulated(node.id)
}
}
-
- // Cloning may introduce new empty instances not seen in the first pass
- // (nested clones). Repeat until stable.
- let changed = true
- while (changed) {
- changed = false
- for (const node of graph.getAllNodes()) {
- if (node.type !== 'INSTANCE' || !node.componentId || node.childIds.length > 0) continue
- const comp = graph.getNode(node.componentId)
- if (comp && comp.childIds.length > 0) {
- graph.populateInstanceChildren(node.id, node.componentId)
- changed = true
- }
- }
- }
}
diff --git a/packages/core/src/kiwi/instance-overrides/resolve.ts b/packages/core/src/kiwi/instance-overrides/resolve.ts
index e5ffb99c6..d72e9b48c 100644
--- a/packages/core/src/kiwi/instance-overrides/resolve.ts
+++ b/packages/core/src/kiwi/instance-overrides/resolve.ts
@@ -6,7 +6,9 @@ import type { OverrideContext } from './types'
const MAX_CHAIN_DEPTH = 20
const siblingIndexCache = new WeakMap<OverrideContext, Map<string, number | null>>()
+const siblingGroupCache = new WeakMap<OverrideContext, Map<string, string[]>>()
const candidateCache = new WeakMap<OverrideContext, Map<string, string[]>>()
+const componentFindCache = new WeakMap<OverrideContext, Map<string, string | null>>()
/**
* Pre-compute componentId root for every node.
@@ -82,6 +84,40 @@ export function getComponentRoot(ctx: OverrideContext, nodeId: string, depth = 0
* ambiguity when multiple siblings share the same root).
* Pass 3: recurse into children.
*/
+function buildSiblingGroups(ctx: OverrideContext): Map<string, string[]> {
+ const groups = new Map<string, string[]>()
+ for (const [id, sibling] of ctx.changeMap) {
+ const siblingParent = sibling.parentIndex?.guid ? guidToString(sibling.parentIndex.guid) : null
+ const siblingSymbol = sibling.symbolData?.symbolID
+ ? guidToString(sibling.symbolData.symbolID)
+ : null
+ if (!siblingParent || !siblingSymbol) continue
+ const groupKey = `${siblingParent}\0${siblingSymbol}`
+ const group = groups.get(groupKey)
+ if (group) group.push(id)
+ else groups.set(groupKey, [id])
+ }
+ for (const group of groups.values()) {
+ group.sort((aId, bId) => {
+ const a = ctx.changeMap.get(aId)
+ const b = ctx.changeMap.get(bId)
+ return (
+ (a?.transform?.m12 ?? 0) - (b?.transform?.m12 ?? 0) ||
+ (a?.transform?.m02 ?? 0) - (b?.transform?.m02 ?? 0)
+ )
+ })
+ }
+ return groups
+}
+
+function getSiblingGroups(ctx: OverrideContext): Map<string, string[]> {
+ const cached = siblingGroupCache.get(ctx)
+ if (cached) return cached
+ const groups = buildSiblingGroups(ctx)
+ siblingGroupCache.set(ctx, groups)
+ return groups
+}
+
function sourceSiblingIndex(ctx: OverrideContext, figmaGuid: string): number | null {
let cache = siblingIndexCache.get(ctx)
if (!cache) {
@@ -98,18 +134,7 @@ function sourceSiblingIndex(ctx: OverrideContext, figmaGuid: string): number | n
return null
}
- const siblings = [...ctx.changeMap]
- .filter(([, sibling]) => {
- const siblingParent = sibling.parentIndex?.guid ? guidToString(sibling.parentIndex.guid) : null
- const siblingSymbol = sibling.symbolData?.symbolID ? guidToString(sibling.symbolData.symbolID) : null
- return siblingParent === parentId && siblingSymbol === symbolId
- })
- .sort(([, a], [, b]) =>
- (a.transform?.m12 ?? 0) - (b.transform?.m12 ?? 0) ||
- (a.transform?.m02 ?? 0) - (b.transform?.m02 ?? 0)
- )
- .map(([id]) => id)
-
+ const siblings = getSiblingGroups(ctx).get(`${parentId}\0${symbolId}`) ?? []
const index = siblings.indexOf(figmaGuid)
const result = index !== -1 ? index : null
cache.set(figmaGuid, result)
@@ -139,7 +164,8 @@ function findNodeBySourceSiblingIndex(
const node = ctx.graph.getNode(id)
if (!node) return
if (node.componentId) {
- const root = ctx.preComputedRoot.get(node.componentId) ?? getComponentRoot(ctx, node.componentId)
+ const root =
+ ctx.preComputedRoot.get(node.componentId) ?? getComponentRoot(ctx, node.componentId)
if (root === targetRoot) candidates?.push(id)
}
for (const childId of node.childIds) collect(childId)
@@ -160,12 +186,26 @@ export function findNodeByComponentId(
parentId: string,
componentId: string
): string | null {
+ let cache = componentFindCache.get(ctx)
+ if (!cache) {
+ cache = new Map()
+ componentFindCache.set(ctx, cache)
+ }
+ const cacheKey = `${parentId}\0${componentId}`
+ if (cache.has(cacheKey)) return cache.get(cacheKey) ?? null
+
const parent = ctx.graph.getNode(parentId)
- if (!parent) return null
+ if (!parent) {
+ cache.set(cacheKey, null)
+ return null
+ }
for (const childId of parent.childIds) {
const child = ctx.graph.getNode(childId)
- if (child?.componentId === componentId) return childId
+ if (child?.componentId === componentId) {
+ cache.set(cacheKey, childId)
+ return childId
+ }
}
const targetRoot = ctx.preComputedRoot.get(componentId) ?? getComponentRoot(ctx, componentId)
@@ -185,13 +225,20 @@ export function findNodeByComponentId(
rootMatch = childId
}
}
- if (rootMatch && !ambiguous) return rootMatch
+ if (rootMatch && !ambiguous) {
+ cache.set(cacheKey, rootMatch)
+ return rootMatch
+ }
}
for (const childId of parent.childIds) {
const deep = findNodeByComponentId(ctx, childId, componentId)
- if (deep) return deep
+ if (deep) {
+ cache.set(cacheKey, deep)
+ return deep
+ }
}
+ cache.set(cacheKey, null)
return null
}
@@ -265,4 +312,6 @@ export function repopulateInstance(ctx: OverrideContext, nodeId: string, compId:
ctx.graph.populateInstanceChildren(nodeId, compId)
}
ctx.componentIdRoot.clear()
+ candidateCache.delete(ctx)
+ componentFindCache.delete(ctx)
}