[Performance] Refactor HSG waypoint creation to use vector store ANN search.
Problem The current implementation of Hierarchical Sectored Graph (HSG) maintenance tasks, specifically the waypoint creation, relies on loading large datasets into memory and performing O(N) cosine similarity calculations in JavaScript/python. This bypasses the optimized (ANN/KNN/etc) search capabilities in the underlying vector stores (pgvector or Valkey).
Code Citations
-
Single Waypoint Creation https://github.com/CaviraOSS/OpenMemory/blob/main/packages/openmemory-js/src/memory/hsg.ts#L502-L533 https://github.com/CaviraOSS/OpenMemory/blob/main/packages/openmemory-py/src/openmemory/memory/hsg.py#L266-L286 This function fetches 1,000 rows into memory and iterates through them on the CPU to find a single nearest neighbour.
-
Inter-Memory Waypoint Creation https://github.com/CaviraOSS/OpenMemory/blob/main/packages/openmemory-js/src/memory/hsg.ts#L534-L567 https://github.com/CaviraOSS/OpenMemory/blob/main/packages/openmemory-py/src/openmemory/memory/hsg.py#L299-L317 This function fetches all vectors for a given sector (which could be thousands or millions) and performs a linear scan.
Performance Impact:
- Network overhead: Transfers all vectors
- CPU overhead: JavaScript or python cosine similarity (single-threaded)
- Memory pressure: Loading thousands (or millions) of vectors into the heap for every memory insert
- Latency: Waypoint generation time grows linearly with database size due to scanning linearly
Proposed Solution: Refactor both packages to use vector store ANN search capabilities.
JS Refactor Refactor hsg.ts to utilize the existing VectorStore.searchSimilar() interface. This delegates the search to the vector store, using native indexing.
Python Refactor (HSG Logic) Update the hsg.py to use store.search() instead of manual NumPy loops, mirroring the JS Refactor. You would also need to rewrite ValkeyVectorStore.search in valkey.py to use RediSearch commands (FT.SEARCH) instead of SCAN. Will need to ensure that the index exists in __ init __ or _ get_client.
@nullure , please, also change one line in hsg.py and hsg.ts -- add user_id to the simhash lookup:
# Before (global lookup)
existing = db.fetchone(
"SELECT * FROM memories WHERE simhash=? ORDER BY salience DESC LIMIT 1",
(simhash,)
)
# After (tenant-scoped lookup)
existing = db.fetchone(
"SELECT * FROM memories WHERE simhash=? AND user_id=? ORDER BY salience DESC LIMIT 1",
(simhash, user_id)
)
This will fix cross-tenant dedup and resolve this discussion issue automatically.
Thanks for the suggestion, will be implemented soon!
Thanks for the detailed performance analysis and code citations.
This targets packages/openmemory-js/packages/openmemory-py's HSG waypoint creation, which was removed in LongMemory's rewrite — recall no longer does O(N) in-process cosine scans; the current engine uses bounded spreading activation over typed edges (src/core/recall/activation_spread.ts, src/core/recall/matrix_fusion.ts) and a SQLite-backed index.
Closing as superseded by the rewrite. If you run into a similar full-scan/ANN-search performance gap on current main, happy to take a fresh issue — vector search strategy there is still a reasonable thing to revisit.