From 8c50880009fcde3c8983d07e5a2d43dab7f4d054 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 2 Feb 2026 18:13:57 +0000 Subject: [PATCH] perf: optimize message cleanup using Map and explicit timestamps Co-authored-by: bmendonca3 <208517100+bmendonca3@users.noreply.github.com> --- package-lock.json | 4 +-- src/store.ts | 76 +++++++++++++++++++++++++++++++---------------- 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4ec2fcd..6789691 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2129,7 +2129,7 @@ "version": "19.2.8", "resolved": "https://registry.npmjs.org/@types/react/-/react-19.2.8.tgz", "integrity": "sha512-3MbSL37jEchWZz2p2mjntRZtPt837ij10ApxKfgmXCTuHWagYg7iA5bqPw6C8BMPfwidlvfPI/fxOc42HLhcyg==", - "dev": true, + "devOptional": true, "license": "MIT", "dependencies": { "csstype": "^3.2.2" @@ -3297,7 +3297,7 @@ "version": "3.2.3", "resolved": "https://registry.npmjs.org/csstype/-/csstype-3.2.3.tgz", "integrity": "sha512-z1HGKcYy2xA8AGQfwrn0PAy+PB7X/GSj3UVJW9qKyn43xWa+gl5nXmU4qqLMRzWVLFC8KusUX8T/0kCiOYpAIQ==", - "dev": true, + "devOptional": true, "license": "MIT" }, "node_modules/d3-color": { diff --git a/src/store.ts b/src/store.ts index b55e7e7..e4991fd 100644 --- a/src/store.ts +++ b/src/store.ts @@ -1,4 +1,3 @@ -/***FILE_CONTENT_START***/ /** * NexusFlow Gatekeeper - State Store * @@ -43,7 +42,7 @@ interface NexusStore { // State nodes: RoboticNode[]; pendingMessages: Map; - appliedMessageIds: Set; + appliedMessageIds: Map; // Maps ID to timestamp // Actions setNodeState: (nodeId: string, state: NodeState) => void; @@ -119,7 +118,7 @@ export const useNexusStore = create()( // Initial State nodes: initialNodes, pendingMessages: new Map(), - appliedMessageIds: new Set(), + appliedMessageIds: new Map(), metrics: { messagesSent: 0, messagesReceived: 0, @@ -208,22 +207,30 @@ export const useNexusStore = create()( switch (type) { case 'NODE_STATE_CHANGE': { const { nodeId, state } = payload as { nodeId: string; state: NodeState }; - set((store) => ({ - nodes: store.nodes.map((n) => - n.id === nodeId ? { ...n, state } : n - ), - appliedMessageIds: new Set([...store.appliedMessageIds, id]), - })); + set((store) => { + const newApplied = new Map(store.appliedMessageIds); + newApplied.set(id, Date.now()); + return { + nodes: store.nodes.map((n) => + n.id === nodeId ? { ...n, state } : n + ), + appliedMessageIds: newApplied, + }; + }); logger.debug({ nodeId, state }, 'Applied state change'); return true; } case 'STATE_SYNC': { const nodes = payload as RoboticNode[]; - set((store) => ({ - nodes, - appliedMessageIds: new Set([...store.appliedMessageIds, id]), - })); + set((store) => { + const newApplied = new Map(store.appliedMessageIds); + newApplied.set(id, Date.now()); + return { + nodes, + appliedMessageIds: newApplied, + }; + }); logger.debug({ nodeCount: nodes.length }, 'Applied state sync'); return true; } @@ -272,9 +279,13 @@ export const useNexusStore = create()( * Mark message as locally applied */ markMessageApplied: (messageId) => { - set((store) => ({ - appliedMessageIds: new Set([...store.appliedMessageIds, messageId]), - })); + set((store) => { + const newApplied = new Map(store.appliedMessageIds); + newApplied.set(messageId, Date.now()); + return { + appliedMessageIds: newApplied, + }; + }); }, /** @@ -342,7 +353,7 @@ export const useNexusStore = create()( const { pendingMessages, appliedMessageIds } = get(); const now = Date.now(); const newPendingMessages = new Map(); - const newAppliedIds = new Set(); + const newAppliedIds = new Map(); let cleanedCount = 0; pendingMessages.forEach((pending, messageId) => { @@ -363,12 +374,10 @@ export const useNexusStore = create()( // Clean up old applied IDs let cleanedAppliedCount = 0; - appliedMessageIds.forEach((id) => { - // UUID v4 format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx - const timestampPart = id.substring(0, 8); - const timestamp = parseInt(timestampPart, 16) * 4294966.296; // Approximate conversion - if (now - timestamp < CLEANUP_DELAY_MS * 1000) { - newAppliedIds.add(id); + appliedMessageIds.forEach((timestamp, id) => { + // Use actual stored timestamp instead of parsing UUID + if (now - timestamp < CLEANUP_DELAY_MS) { + newAppliedIds.set(id, timestamp); } else { cleanedAppliedCount++; } @@ -399,7 +408,8 @@ export const useNexusStore = create()( if (!persistedState) return currentState; const converted: NexusStore = { ...currentState } as NexusStore; - const psAny = persistedState as Partial; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const psAny = persistedState as any; if (psAny.pendingMessages) { const map = new Map(); @@ -410,7 +420,22 @@ export const useNexusStore = create()( } if (psAny.appliedMessageIds) { - converted.appliedMessageIds = new Set(psAny.appliedMessageIds as unknown as string[]); + const map = new Map(); + + if (Array.isArray(psAny.appliedMessageIds)) { + // Legacy format or array serialization: assume current time for lack of better data + psAny.appliedMessageIds.forEach((id: string) => map.set(id, Date.now())); + } else if (typeof psAny.appliedMessageIds === 'object') { + // Object format: key -> timestamp + Object.entries(psAny.appliedMessageIds).forEach(([key, value]) => { + if (typeof value === 'number') { + map.set(key, value); + } else { + map.set(key, Date.now()); + } + }); + } + converted.appliedMessageIds = map; } return converted; @@ -519,4 +544,3 @@ export const robotAlphaPath: { x: number; y: number }[] = [ { x: 200, y: 100 }, { x: 150, y: 100 }, ]; -/***FILE_CONTENT_END***/