Skip to content

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

perf(collab): skip unchanged Yjs node fields (#529)

Danila Poyarkovb0ebe91ed676
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 6fb4e4e7f..48a5bcc8a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@
- Add tested pane-registry and recursive split-tree models for independently viewed same-document canvases, capped at four visible panes.
- Add explicit shared/view editor-state ownership and canvas render-state hooks as a foundation for independent same-document canvas panes.
+- Improve collaboration efficiency by avoiding redundant unchanged-field Yjs writes and covering repeated and concurrent two-peer edits.
- Show Figma-style temporary distance measurements between selected and Option/Alt-hovered layers. (#491)
- Add a single CodeMirror editor for live Design JSX and HTML/CSS canvas previews, with Tailwind JSX viewing, completion, diagnostics, line numbers, bounded execution, and session-level undo. (#130)
- Allow supported AI model profiles to set a provider-specific reasoning effort. (#454)
diff --git a/bun.lock b/bun.lock
index cbcf8bc7f..39a5ac287 100644
--- a/bun.lock
+++ b/bun.lock
@@ -50,6 +50,7 @@
"canvaskit-wasm": "^0.40.0",
"culori": "^4.0.2",
"dedent": "^1.7.1",
+ "es-toolkit": "^1.46.1",
"fflate": "^0.8.2",
"fzstd": "^0.1.1",
"hast-util-to-html": "^9.0.5",
diff --git a/package.json b/package.json
index 307347ca2..93294ff50 100644
--- a/package.json
+++ b/package.json
@@ -106,6 +106,7 @@
"canvaskit-wasm": "^0.40.0",
"culori": "^4.0.2",
"dedent": "^1.7.1",
+ "es-toolkit": "^1.46.1",
"fflate": "^0.8.2",
"fzstd": "^0.1.1",
"hast-util-to-html": "^9.0.5",
diff --git a/src/app/collab/node-codec.ts b/src/app/collab/node-codec.ts
index a1c8559b6..6b2f6fddf 100644
--- a/src/app/collab/node-codec.ts
+++ b/src/app/collab/node-codec.ts
@@ -1,3 +1,5 @@
+import { isEqual } from 'es-toolkit/predicate'
+
import type {
Fill,
FillType,
@@ -38,10 +40,19 @@ export function encodeNodeForYjs(node: SceneNode): Record<string, unknown> {
export function syncEncodedNodeToYMap(
node: SceneNode,
- ynode: { delete(key: string): void; set(key: string, value: unknown): void }
+ ynode: {
+ delete(key: string): void
+ get(key: string): unknown
+ has(key: string): boolean
+ set(key: string, value: unknown): void
+ }
): void {
- for (const key of DERIVED_NODE_FIELDS) ynode.delete(key)
- for (const [key, value] of Object.entries(encodeNodeForYjs(node))) ynode.set(key, value)
+ for (const key of DERIVED_NODE_FIELDS) {
+ if (ynode.has(key)) ynode.delete(key)
+ }
+ for (const [key, value] of Object.entries(encodeNodeForYjs(node))) {
+ if (!isEqual(ynode.get(key), value)) ynode.set(key, value)
+ }
}
export function decodeNodeFromYjs(ynode: YjsNodeLike): Partial<SceneNode> {
diff --git a/tests/engine/collab/yjs-sync.test.ts b/tests/engine/collab/yjs-sync.test.ts
index 4041eb08f..4e6e7d345 100644
--- a/tests/engine/collab/yjs-sync.test.ts
+++ b/tests/engine/collab/yjs-sync.test.ts
@@ -1,6 +1,8 @@
import { describe, test, expect } from 'bun:test'
+import { create as createPRNG } from 'lib0/prng'
import * as Y from 'yjs'
+import { TestConnector, type TestYInstance } from 'yjs/testHelper'
import type { Fill, GeometryPath, SceneNode } from '@open-pencil/scene-graph'
import { SceneGraph } from '@open-pencil/scene-graph'
@@ -47,11 +49,17 @@ function firstPage(graph: SceneGraph): SceneNode {
type SyncedStores = ReturnType<typeof createSyncedStores>
-function createSyncedStores() {
+type SyncedStoreOptions = {
+ hostDoc?: Y.Doc
+ peerDoc?: Y.Doc
+ connectImmediately?: boolean
+}
+
+function createSyncedStores(options: SyncedStoreOptions = {}) {
const hostStore = createEditorStore(new SceneGraph())
const peerStore = createEditorStore(new SceneGraph())
- const hostDoc = new Y.Doc()
- const peerDoc = new Y.Doc()
+ const hostDoc = options.hostDoc ?? new Y.Doc()
+ const peerDoc = options.peerDoc ?? new Y.Doc()
const hostNodes = hostDoc.getMap<Y.Map<unknown>>('nodes')
const peerNodes = peerDoc.getMap<Y.Map<unknown>>('nodes')
const hostImages = hostDoc.getMap<Uint8Array>('images')
@@ -101,13 +109,17 @@ function createSyncedStores() {
applyYjsToGraph: peerSync.applyYjsToGraph
})
- const disconnectYDocs = connectYDocs(hostDoc, peerDoc)
+ const disconnectYDocs =
+ options.connectImmediately === false ? undefined : connectYDocs(hostDoc, peerDoc)
return {
hostStore,
peerStore,
hostSync,
peerSync,
+ hostDoc,
+ peerDoc,
+ disconnectYDocs,
get hostSuppressGraphSync() {
return hostSuppressGraphSync
},
@@ -115,15 +127,15 @@ function createSyncedStores() {
return peerSuppressGraphSync
},
cleanup: () => {
- disconnectYDocs()
+ disconnectYDocs?.()
hostDoc.destroy()
peerDoc.destroy()
}
}
}
-function withSyncedStores(run: (stores: SyncedStores) => void) {
- const stores = createSyncedStores()
+function withSyncedStores(run: (stores: SyncedStores) => void, options: SyncedStoreOptions = {}) {
+ const stores = createSyncedStores(options)
try {
run(stores)
} finally {
@@ -345,6 +357,97 @@ describe('collab yjs-sync', () => {
})
})
+ test('unchanged node synchronization emits no Yjs update', () => {
+ withSyncedStores(({ hostStore, hostSync, hostDoc }) => {
+ const hostPage = firstPage(hostStore.graph)
+ const rect = hostStore.graph.createNode('RECTANGLE', hostPage.id, { width: 80, height: 60 })
+ hostSync.syncNodeToYjs(rect.id)
+
+ let updateCount = 0
+ let updateBytes = 0
+ const trackUpdate = (update: Uint8Array) => {
+ updateCount++
+ updateBytes += update.byteLength
+ }
+ hostDoc.on('update', trackUpdate)
+ for (let index = 0; index < 100; index++) hostSync.syncNodeToYjs(rect.id)
+ hostDoc.off('update', trackUpdate)
+
+ expect(updateCount).toBe(0)
+ expect(updateBytes).toBe(0)
+ })
+ })
+
+ test('repeated drag-like updates stay field-sized and do not echo', () => {
+ withSyncedStores(({ hostStore, peerStore, hostSync, hostDoc, peerDoc }) => {
+ const hostPage = firstPage(hostStore.graph)
+ const rect = hostStore.graph.createNode('RECTANGLE', hostPage.id, { width: 80, height: 60 })
+ hostSync.syncAllNodesToYjs()
+ hostSync.syncNodeToYjs(rect.id)
+
+ let hostUpdateCount = 0
+ let hostUpdateBytes = 0
+ let peerUpdateCount = 0
+ const trackHostUpdate = (update: Uint8Array) => {
+ hostUpdateCount++
+ hostUpdateBytes += update.byteLength
+ }
+ const trackPeerUpdate = () => {
+ peerUpdateCount++
+ }
+ hostDoc.on('update', trackHostUpdate)
+ peerDoc.on('update', trackPeerUpdate)
+
+ for (let index = 1; index <= 100; index++) {
+ hostStore.graph.updateNode(rect.id, { x: index })
+ hostSync.syncNodeToYjs(rect.id)
+ }
+
+ hostDoc.off('update', trackHostUpdate)
+ peerDoc.off('update', trackPeerUpdate)
+ expect(getNodeOrThrow(peerStore.graph, rect.id).x).toBe(100)
+ expect(hostUpdateCount).toBe(100)
+ expect(peerUpdateCount).toBe(100)
+ expect(hostUpdateBytes).toBeLessThan(8_000)
+ })
+ })
+
+ test('queued concurrent edits converge through the official Yjs test connector', () => {
+ const connector = new TestConnector(createPRNG(526))
+ const hostDoc: TestYInstance = connector.createY(1)
+ const peerDoc: TestYInstance = connector.createY(2)
+ connector.syncAll()
+
+ withSyncedStores(
+ ({ hostStore, peerStore, hostSync, peerSync }) => {
+ const hostPage = firstPage(hostStore.graph)
+ const rect = hostStore.graph.createNode('RECTANGLE', hostPage.id, {
+ width: 80,
+ height: 60
+ })
+ hostSync.syncAllNodesToYjs()
+ hostSync.syncNodeToYjs(rect.id)
+ connector.flushAllMessages()
+ expect(getNodeOrThrow(peerStore.graph, rect.id).type).toBe('RECTANGLE')
+
+ hostDoc.disconnect()
+ hostStore.graph.updateNode(rect.id, { x: 42 })
+ hostSync.syncNodeToYjs(rect.id)
+ peerStore.graph.updateNode(rect.id, { y: 24 })
+ peerSync.syncNodeToYjs(rect.id)
+
+ hostDoc.connect()
+ expect(connector.flushRandomMessage()).toBe(true)
+ connector.flushAllMessages()
+
+ expect(getNodeOrThrow(hostStore.graph, rect.id)).toMatchObject({ x: 42, y: 24 })
+ expect(getNodeOrThrow(peerStore.graph, rect.id)).toMatchObject({ x: 42, y: 24 })
+ expect(Y.encodeStateVector(hostDoc)).toEqual(Y.encodeStateVector(peerDoc))
+ },
+ { hostDoc, peerDoc, connectImmediately: false }
+ )
+ })
+
test('image fills sync image bytes', () => {
withSyncedStores(({ hostStore, peerStore, hostSync }) => {
const hostPage = firstPage(hostStore.graph)
diff --git a/tests/helpers/yjs-test-helper.d.ts b/tests/helpers/yjs-test-helper.d.ts
new file mode 100644
index 000000000..6f1e706f9
--- /dev/null
+++ b/tests/helpers/yjs-test-helper.d.ts
@@ -0,0 +1,25 @@
+declare module 'yjs/testHelper' {
+ import type { PRNG } from 'lib0/prng'
+ import type * as Y from 'yjs'
+
+ export class TestYInstance extends Y.Doc {
+ tc: TestConnector
+ updates: Uint8Array[]
+ connect(): void
+ disconnect(): void
+ }
+
+ export class TestConnector {
+ constructor(prng: PRNG)
+ allConns: Set<TestYInstance>
+ onlineConns: Set<TestYInstance>
+ createY(clientId: number): TestYInstance
+ flushRandomMessage(): boolean
+ flushAllMessages(): boolean
+ reconnectAll(): void
+ disconnectAll(): void
+ reconnectRandom(): boolean
+ disconnectRandom(): boolean
+ syncAll(): void
+ }
+}