memorix icon indicating copy to clipboard operation
memorix copied to clipboard

[perf] observations.json performance

Open FurryWolfX opened this issue 7 months ago • 2 comments

Description

observations.json uses a "read-all → append → write-all" pattern for writes. Every storeObservation call triggers O(n) file I/O, where n is the total number of observations.

Core code path (src/memory/observations.ts):

// Every write executes:
const observations = JSON.parse(await fs.readFile('observations.json')); // O(n)
observations.push(newObs);                                             // O(1)
await fs.writeFile('observations.json', JSON.stringify(observations)); // O(n)

Impact

  • MCP response latency: every memorix_store call blocks the MCP server
  • Startup time: larger observations.json → slower HTTP control plane startup
  • Concurrent write starvation: file lock serializes all write requests
  • Memory pressure: each write loads the entire JSON into heap memory

Profiling Data

storeObservation()
  ├── acquireLock()          // file lock contention
  ├── fs.readFile()          // O(n) read entire JSON
  ├── JSON.parse()           // O(n) parse
  ├── Array.push()           // O(1)
  ├── JSON.stringify()       // O(n) serialize
  └── fs.writeFile()         // O(n) write back to disk

File lock waiting (high concurrency):

Agent A: [===== lock held (write entire file) =====]
Agent B: [wait...][===== lock held (write entire file) =====]
Agent C: [wait...wait...][===== lock held (write entire file) =====]

FurryWolfX avatar Apr 04 '26 16:04 FurryWolfX

Thanks for the careful write-up — this is a valid performance concern.

One nuance from the current implementation: embedding generation is already fire-and-forget, so memorix_store is not synchronously blocked on the embedding call itself. But the main point in this issue is still correct: the observation persistence path is currently file-backed and uses a read-all / update / write-all pattern under a file lock, which means write latency and lock hold time both grow with observations.json size.

So I’d frame this as a real storage / write-path scaling issue rather than a false alarm. It is also consistent with a broader pressure point we’ve already been tracking internally around file-backed persistence and future storage-model evolution.

Keeping this open makes sense as a performance / architecture issue. The likely next step is not a quick hotfix, but a scoped design for how far the current JSON-backed model can be pushed and what the smallest safe improvement would be before any larger storage migration discussion.

AVIDS2 avatar Apr 04 '26 17:04 AVIDS2

Fixed in v1.0.7 observations are now stored in SQLite (canonical store). The read-all/append/write-all pattern on observations.json has been replaced with direct SQLite CRUD. See Phase 2 storage migration.

AVIDS2 avatar Apr 14 '26 14:04 AVIDS2