Skip to content

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

Replace structuredClone with typed copy helpers in hot paths

Danila Poyarkovcf6747389a54
diff --git a/packages/core/src/copy.ts b/packages/core/src/copy.ts
new file mode 100644
index 000000000..b17a99e88
--- /dev/null
+++ b/packages/core/src/copy.ts
@@ -0,0 +1,75 @@
+/**
+ * Typed shallow-copy helpers for Fill, Stroke, Effect, and StyleRun.
+ *
+ * These replace `structuredClone` for known scene-graph array types,
+ * avoiding the ~24× overhead of the generic deep-clone algorithm.
+ * Each helper spreads the top-level object and any nested objects
+ * (color, offset, gradientStops, dashPattern, style) to ensure
+ * no shared references between source and copy.
+ */
+
+import type { Effect, Fill, GeometryPath, GradientStop, Stroke, StyleRun } from './scene-graph'
+
+// --- Individual copy functions ---
+
+export function copyFill(f: Fill): Fill {
+ const copy: Fill = { ...f, color: { ...f.color } }
+ if (f.gradientStops) {
+ copy.gradientStops = f.gradientStops.map(copyGradientStop)
+ }
+ return copy
+}
+
+export function copyStroke(s: Stroke): Stroke {
+ const copy: Stroke = { ...s, color: { ...s.color } }
+ if (s.dashPattern) {
+ copy.dashPattern = [...s.dashPattern]
+ }
+ return copy
+}
+
+export function copyEffect(e: Effect): Effect {
+ return {
+ ...e,
+ color: { ...e.color },
+ offset: { ...e.offset },
+ }
+}
+
+export function copyStyleRun(r: StyleRun): StyleRun {
+ return {
+ ...r,
+ style: { ...r.style },
+ }
+}
+
+// --- Array copy functions ---
+
+export function copyFills(fills: Fill[]): Fill[] {
+ return fills.map(copyFill)
+}
+
+export function copyStrokes(strokes: Stroke[]): Stroke[] {
+ return strokes.map(copyStroke)
+}
+
+export function copyEffects(effects: Effect[]): Effect[] {
+ return effects.map(copyEffect)
+}
+
+export function copyStyleRuns(runs: StyleRun[]): StyleRun[] {
+ return runs.map(copyStyleRun)
+}
+
+export function copyGeometryPaths(paths: GeometryPath[]): GeometryPath[] {
+ return paths.map((p) => ({
+ windingRule: p.windingRule,
+ commandsBlob: p.commandsBlob.slice(),
+ }))
+}
+
+// --- Internal helpers ---
+
+function copyGradientStop(gs: GradientStop): GradientStop {
+ return { color: { ...gs.color }, position: gs.position }
+}
diff --git a/packages/core/src/kiwi/instance-overrides.ts b/packages/core/src/kiwi/instance-overrides.ts
index 9c57616d3..f2507c3f6 100644
--- a/packages/core/src/kiwi/instance-overrides.ts
+++ b/packages/core/src/kiwi/instance-overrides.ts
@@ -1,5 +1,6 @@
import type { SceneGraph, SceneNode, GeometryPath } from '../scene-graph'
import { guidToString, convertOverrideToProps, resolveGeometryPaths } from './kiwi-convert'
+import { copyFills, copyStrokes, copyEffects, copyStyleRuns, copyGeometryPaths } from '../copy'
import type { GUID } from './codec'
interface SymbolOverride {
@@ -60,20 +61,40 @@ export function populateAndApplyOverrides(
guidToNodeId: Map<string, string>,
blobs: Uint8Array[] = []
): void {
- // Iterative population: cloning creates new instances that themselves need children
- let populated = 1
- while (populated > 0) {
- populated = 0
- 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)
- populated++
+ // Work-queue population: seed with empty instances, then discover newly
+ // cloned nested instances by walking the subtree (avoids full graph scan per round).
+ const populateQueue: string[] = []
+ for (const node of graph.getAllNodes()) {
+ if (node.type === 'INSTANCE' && node.componentId && node.childIds.length === 0) {
+ populateQueue.push(node.id)
+ }
+ }
+
+ function collectEmptyInstances(parentId: string, out: string[]) {
+ const parent = graph.getNode(parentId)
+ if (!parent) return
+ for (const childId of parent.childIds) {
+ const child = graph.getNode(childId)
+ if (!child) continue
+ if (child.type === 'INSTANCE' && child.componentId && child.childIds.length === 0) {
+ out.push(child.id)
+ } else if (child.childIds.length > 0) {
+ collectEmptyInstances(childId, out)
}
}
}
+ while (populateQueue.length > 0) {
+ const nodeId = populateQueue.pop()!
+ const node = graph.getNode(nodeId)
+ if (!node || node.type !== 'INSTANCE' || !node.componentId || node.childIds.length > 0) continue
+ const comp = graph.getNode(node.componentId)
+ if (comp && comp.childIds.length > 0) {
+ graph.populateInstanceChildren(nodeId, node.componentId)
+ collectEmptyInstances(nodeId, populateQueue)
+ }
+ }
+
// Build overrideKey → figmaGuid map
const overrideKeyToGuid = new Map<string, string>()
for (const [id, nc] of changeMap) {
@@ -477,8 +498,8 @@ export function populateAndApplyOverrides(
if (source.height !== clone.height) cu.height = source.height
if (source.x !== clone.x) cu.x = source.x
if (source.y !== clone.y) cu.y = source.y
- if (source.fillGeometry !== clone.fillGeometry) cu.fillGeometry = structuredClone(source.fillGeometry)
- if (source.strokeGeometry !== clone.strokeGeometry) cu.strokeGeometry = structuredClone(source.strokeGeometry)
+ if (source.fillGeometry !== clone.fillGeometry) cu.fillGeometry = copyGeometryPaths(source.fillGeometry)
+ if (source.strokeGeometry !== clone.strokeGeometry) cu.strokeGeometry = copyGeometryPaths(source.strokeGeometry)
if (Object.keys(cu).length > 0) graph.updateNode(cloneId, cu)
}
queue.push(cloneId)
@@ -530,10 +551,10 @@ export function populateAndApplyOverrides(
if (source.text !== undefined && source.text !== target.text) updates.text = source.text
if (source.visible !== undefined && source.visible !== target.visible) updates.visible = source.visible
if (source.opacity !== undefined && source.opacity !== target.opacity) updates.opacity = source.opacity
- if (source.fills !== undefined && source.fills !== target.fills) updates.fills = structuredClone(source.fills)
- if (source.strokes !== undefined && source.strokes !== target.strokes) updates.strokes = structuredClone(source.strokes)
- if (source.effects !== undefined && source.effects !== target.effects) updates.effects = structuredClone(source.effects)
- if (source.styleRuns !== undefined && source.styleRuns !== target.styleRuns) updates.styleRuns = structuredClone(source.styleRuns)
+ if (source.fills !== undefined && source.fills !== target.fills) updates.fills = copyFills(source.fills)
+ if (source.strokes !== undefined && source.strokes !== target.strokes) updates.strokes = copyStrokes(source.strokes)
+ if (source.effects !== undefined && source.effects !== target.effects) updates.effects = copyEffects(source.effects)
+ if (source.styleRuns !== undefined && source.styleRuns !== target.styleRuns) updates.styleRuns = copyStyleRuns(source.styleRuns)
if (source.layoutGrow !== undefined && source.layoutGrow !== target.layoutGrow) updates.layoutGrow = source.layoutGrow
if (source.textAutoResize !== undefined && source.textAutoResize !== target.textAutoResize) updates.textAutoResize = source.textAutoResize
if (source.locked !== undefined && source.locked !== target.locked) updates.locked = source.locked
diff --git a/packages/core/src/scene-graph.ts b/packages/core/src/scene-graph.ts
index 814592631..a2aa6430d 100644
--- a/packages/core/src/scene-graph.ts
+++ b/packages/core/src/scene-graph.ts
@@ -1,4 +1,5 @@
import { BLACK, DEFAULT_FONT_FAMILY, DEFAULT_STROKE_MITER_LIMIT } from './constants'
+import { copyEffects, copyFills, copyStrokes, copyStyleRuns } from './copy'
export type { GUID, Color } from './types'
@@ -966,7 +967,17 @@ export class SceneGraph {
key: K
): void {
const val = source[key]
- target[key] = (Array.isArray(val) ? structuredClone(val) : val) as SceneNode[K]
+ if (key === 'fills') {
+ ;(target as Record<string, unknown>)[key] = copyFills(val as Fill[])
+ } else if (key === 'strokes') {
+ ;(target as Record<string, unknown>)[key] = copyStrokes(val as Stroke[])
+ } else if (key === 'effects') {
+ ;(target as Record<string, unknown>)[key] = copyEffects(val as Effect[])
+ } else if (key === 'styleRuns') {
+ ;(target as Record<string, unknown>)[key] = copyStyleRuns(val as StyleRun[])
+ } else {
+ target[key] = (Array.isArray(val) ? structuredClone(val) : val) as SceneNode[K]
+ }
}
createInstance(