|
| 1 | +// Performance benchmarks for the bulk fold paths. |
| 2 | +// |
| 3 | +// Run with: npm run bench |
| 4 | +// |
| 5 | +// These guard the O(N) behavior of replay / import / cascade. The "fold |
| 6 | +// strategy contrast" group makes the win explicit: folding commands with the |
| 7 | +// immutable `applyCommand` clones the whole graph per command (the old, |
| 8 | +// quadratic shape) while the in-place fold used by replay stays linear. |
| 9 | + |
| 10 | +import { bench, describe } from "vitest"; |
| 11 | +import { applyCommand } from "../src/reducer.js"; |
| 12 | +import { createGraphState } from "../src/graph.js"; |
| 13 | +import { createMemoryItem, createEdge } from "../src/helpers.js"; |
| 14 | +import { replayCommands, replayFromEnvelopes } from "../src/replay.js"; |
| 15 | +import { importSlice } from "../src/transplant.js"; |
| 16 | +import type { MemexExport } from "../src/transplant.js"; |
| 17 | +import { createIntentState } from "../src/intent.js"; |
| 18 | +import { createTaskState } from "../src/task.js"; |
| 19 | +import { cascadeRetract, getDependents } from "../src/integrity.js"; |
| 20 | +import type { |
| 21 | + GraphState, |
| 22 | + MemoryItem, |
| 23 | + MemoryCommand, |
| 24 | + Edge, |
| 25 | + EventEnvelope, |
| 26 | +} from "../src/types.js"; |
| 27 | + |
| 28 | +const BASE = 1_700_000_000_000; |
| 29 | +const padId = (i: number): string => `m-${i.toString().padStart(8, "0")}`; |
| 30 | + |
| 31 | +function item(i: number, parents?: string[]): MemoryItem { |
| 32 | + return createMemoryItem({ |
| 33 | + id: padId(i), |
| 34 | + scope: "bench", |
| 35 | + kind: "observation", |
| 36 | + content: { text: `item ${i}` }, |
| 37 | + author: "agent:bench", |
| 38 | + source_kind: "observed", |
| 39 | + authority: 0.5, |
| 40 | + created_at: BASE + i, |
| 41 | + ...(parents ? { parents } : {}), |
| 42 | + }); |
| 43 | +} |
| 44 | + |
| 45 | +function createCommands(n: number): MemoryCommand[] { |
| 46 | + const cmds: MemoryCommand[] = []; |
| 47 | + for (let i = 0; i < n; i++) |
| 48 | + cmds.push({ type: "memory.create", item: item(i) }); |
| 49 | + return cmds; |
| 50 | +} |
| 51 | + |
| 52 | +function createEnvelopes(n: number): EventEnvelope<MemoryCommand>[] { |
| 53 | + const envs: EventEnvelope<MemoryCommand>[] = []; |
| 54 | + for (let i = 0; i < n; i++) { |
| 55 | + envs.push({ |
| 56 | + id: `e-${i}`, |
| 57 | + namespace: "memory", |
| 58 | + type: "memory.create", |
| 59 | + // Reverse the timestamps so replayFromEnvelopes pays for the sort. |
| 60 | + ts: new Date(BASE + (n - i)).toISOString(), |
| 61 | + payload: { type: "memory.create", item: item(i) }, |
| 62 | + }); |
| 63 | + } |
| 64 | + return envs; |
| 65 | +} |
| 66 | + |
| 67 | +function chainCommands(n: number): MemoryCommand[] { |
| 68 | + const cmds: MemoryCommand[] = []; |
| 69 | + for (let i = 0; i < n; i++) { |
| 70 | + cmds.push({ |
| 71 | + type: "memory.create", |
| 72 | + item: item(i, i > 0 ? [padId(i - 1)] : undefined), |
| 73 | + }); |
| 74 | + } |
| 75 | + return cmds; |
| 76 | +} |
| 77 | + |
| 78 | +function sliceOf(n: number): MemexExport { |
| 79 | + const memories: MemoryItem[] = []; |
| 80 | + const edges: Edge[] = []; |
| 81 | + for (let i = 0; i < n; i++) { |
| 82 | + memories.push(item(i)); |
| 83 | + if (i > 0) { |
| 84 | + edges.push( |
| 85 | + createEdge({ |
| 86 | + edge_id: `edge-${i}`, |
| 87 | + from: padId(i), |
| 88 | + to: padId(i - 1), |
| 89 | + kind: "DERIVED_FROM", |
| 90 | + author: "agent:bench", |
| 91 | + source_kind: "observed", |
| 92 | + authority: 0.5, |
| 93 | + }), |
| 94 | + ); |
| 95 | + } |
| 96 | + } |
| 97 | + return { memories, edges, intents: [], tasks: [] }; |
| 98 | +} |
| 99 | + |
| 100 | +describe("replayCommands", () => { |
| 101 | + const c5k = createCommands(5_000); |
| 102 | + const c20k = createCommands(20_000); |
| 103 | + bench("5k creates", () => { |
| 104 | + replayCommands(c5k); |
| 105 | + }); |
| 106 | + bench("20k creates", () => { |
| 107 | + replayCommands(c20k); |
| 108 | + }); |
| 109 | +}); |
| 110 | + |
| 111 | +describe("replayFromEnvelopes (sort + fold)", () => { |
| 112 | + const e5k = createEnvelopes(5_000); |
| 113 | + bench("5k reverse-sorted creates", () => { |
| 114 | + replayFromEnvelopes(e5k); |
| 115 | + }); |
| 116 | +}); |
| 117 | + |
| 118 | +describe("importSlice (memories + edges into empty graph)", () => { |
| 119 | + const s5k = sliceOf(5_000); |
| 120 | + const s10k = sliceOf(10_000); |
| 121 | + bench("5k memories", () => { |
| 122 | + importSlice( |
| 123 | + createGraphState(), |
| 124 | + createIntentState(), |
| 125 | + createTaskState(), |
| 126 | + s5k, |
| 127 | + ); |
| 128 | + }); |
| 129 | + bench("10k memories", () => { |
| 130 | + importSlice( |
| 131 | + createGraphState(), |
| 132 | + createIntentState(), |
| 133 | + createTaskState(), |
| 134 | + s10k, |
| 135 | + ); |
| 136 | + }); |
| 137 | +}); |
| 138 | + |
| 139 | +describe("cascadeRetract / getDependents (5k-deep chain)", () => { |
| 140 | + const { state } = replayCommands(chainCommands(5_000)); |
| 141 | + const root = padId(0); |
| 142 | + bench("cascadeRetract whole chain", () => { |
| 143 | + cascadeRetract(state, root, "agent:bench"); |
| 144 | + }); |
| 145 | + bench("getDependents transitive", () => { |
| 146 | + getDependents(state, root, true); |
| 147 | + }); |
| 148 | +}); |
| 149 | + |
| 150 | +describe("fold strategy contrast (2k creates)", () => { |
| 151 | + const cmds = createCommands(2_000); |
| 152 | + bench("immutable applyCommand fold — clone per command (~O(N^2))", () => { |
| 153 | + let state: GraphState = createGraphState(); |
| 154 | + for (const cmd of cmds) state = applyCommand(state, cmd).state; |
| 155 | + }); |
| 156 | + bench("in-place fold via replayCommands (O(N))", () => { |
| 157 | + replayCommands(cmds); |
| 158 | + }); |
| 159 | +}); |
0 commit comments