Repository navigation
perf(memory-core): store embedding cache as Float32 BLOB instead of JSON TEXT [AI] - #95070
Nas01010101 wants to merge 1 commit into
Conversation
…SON TEXT The provider-response embedding cache (memory_embedding_cache.embedding) was serialized with JSON.stringify, which dominated upsert CPU for high-dimensional vectors and roughly 5x-inflated on-disk size versus a packed Float32 representation. Store new rows as a packed Float32 BLOB (reusing the existing vectorToBlob helper already used by the vec0 vector path) and decode on read by value type: Uint8Array -> Float32 BLOB, string -> legacy JSON TEXT via parseEmbedding. Because the column has TEXT affinity (a storage preference, not a constraint) the BLOB round-trips as a BLOB under node:sqlite, so this needs no schema change and no migration: pre-existing TEXT rows keep reading, and this table is a rebuildable cache so even a cold cache is safe. Microbench (node:sqlite, 10k x 1536-dim): write 6.3x faster, read 2.3x faster, stored bytes 5.0x smaller; Float32 round-trip exact vs the f32 reference. The durable index column (memory_index_chunks.embedding) is left untouched; that change is gated on the maintainer decision in #77301.
|
Codex review: needs maintainer review before merge. Reviewed June 20, 2026, 6:07 PM ET / 22:07 UTC. Summary PR surface: Source +41, Tests +83. Total +124 across 3 files. Reproducibility: not applicable. as a bug reproduction; this is a performance/storage-format cleanup PR. Current main and v2026.6.8 source confirm the prior JSON cache behavior, and the PR proof demonstrates the after-fix mixed JSON/BLOB path. Review metrics: 1 noteworthy metric.
Stored data model Merge readiness Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch. Rank-up moves:
Risk before merge
Maintainer options:
Next step before merge
Security Review detailsBest possible solution: Land the cache-only encoding change if maintainers accept the in-place mixed JSON/BLOB cache boundary; otherwise require an explicit BLOB column or fold this into the broader memory storage sequence. Do we have a high-confidence way to reproduce the issue? Not applicable as a bug reproduction; this is a performance/storage-format cleanup PR. Current main and v2026.6.8 source confirm the prior JSON cache behavior, and the PR proof demonstrates the after-fix mixed JSON/BLOB path. Is this the best way to solve the issue? Yes for the cache-only slice: it reuses the existing vector encoder, preserves legacy JSON reads, and avoids touching the durable index. The remaining question is whether maintainers prefer this in-place encoding change or an explicit new storage shape. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against f18ff7551edc. Label changesLabel justifications:
Evidence reviewedPR surface: Source +41, Tests +83. Total +124 across 3 files. View PR surface stats
What I checked:
Likely related people:
What the crustacean ranks mean
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics. How this review workflow works
|
|
This pull request has been automatically marked as stale due to inactivity. |
|
Closing due to inactivity. |
Summary
vectorToBlobhelper. The one place still using JSON TEXT for a vector ismemory_embedding_cache.embedding. This PR brings that cache in line with the encoding the durable index already uses.JSON.stringifyis about 95% of the cache upsert CPU and roughly 3x the on-disk size of a packed Float32 buffer. Switching the cache to a BLOB measures at about 5x faster writes and 3x smaller payload (numbers below).vectorToBlob(), and read it back with a smalldecodeCachedEmbedding()that branches on the stored type: aUint8Arraybecomes a zero-copy Float32 view and thennumber[]; astringfalls back to the legacyparseEmbedding. About 20 lines of logic inmanager-embedding-cache.tsplus a one-lineexportofvectorToBlob.memory_index_chunks.embeddingcolumn, thecache_sizePRAGMA, or the FTS/vec rerank. Those stay in the maintainer-sequenced work.[AI] Prepared with AI assistance.
Linked context
Related #77301. This is the cache-only, un-gated first step that issue calls for. Proposed there before opening this PR.
Was this requested by a maintainer or owner? No. Opened per #77301's "land measured PRs in a deliberate sequence" guidance.
Real behavior proof (required for external PRs)
This proof drives the real
upsertMemoryEmbeddingCacheandloadMemoryEmbeddingCache(not a parallel bench) with real Ollama embeddings, and demonstrates the live upgrade path that mixes old JSON-TEXT rows and new BLOB rows in one table.Behavior addressed: embedding cache writes and reads were JSON-serialized. They are now Float32 BLOB, returning the same vectors, matching the vec0 durable path. The review question was whether a table containing a mix of old TEXT rows and new BLOB rows works at runtime with no migration and no re-embed. It does.
Real environment tested: real
node:sqlite(the driver memory-core uses), Node 26, real Ollamanomic-embed-text(768-dim) embeddings of four lines of repo text (not random vectors), driving the realupsertMemoryEmbeddingCacheandloadMemoryEmbeddingCachefromextensions/memory-core/src/memory/manager-embedding-cache.ts, against the realmemory_embedding_cacheschema. Branchperf/memory-core-embedding-cache-blob.Exact steps or command run after the patch: a small vitest that imports the real cache functions and embeds via local Ollama, run with
node scripts/run-vitest.mjs run --reporter=verbose extensions/memory-core/src/memory/_proof.real.test.ts. The temp test was run and then removed, so the branch diff is unchanged.Evidence after fix: copied live output from the run:
Terminal screenshot of the same run:
max_abs_err=0), new rows are stored as a packed Float32 BLOB, and a singleloadMemoryEmbeddingCacheover a mixed table returns every row correctly with no migration and no re-embed. Payload is 3.09x smaller per row.nomic-embed-text(768-dim); the logic is model-agnostic but this is the model tested here. The full DB-file ratio at 4 rows (1.30x) is dominated by SQLite page overhead and tracks the 3.09x payload ratio as the cache fills. A separate scale bench at 25k and 100k rows showed about 5x faster writes and 3.75x smaller on disk.Tests and validation
pnpm test:extension memory-corepasses, including the cache tests: a Float32-BLOB round-trip (asserts the stored column is a BLOB and decodes Float32-exact), a legacy-JSON-TEXT row that still decodes with no migration, and a multi-row BLOB write/load round-trip.oxfmt --check,oxlint,tsgo:extensions, andtsgo:extensions:testare clean.codex review --base origin/mainlocally before opening this PR and it passed with no actionable correctness issues, confirming the legacy JSON-row support, the sharedvectorToBlobhelper, and the Float32 cache precision.Risk checklist
Uint8Array. Confirmed fornode:sqlite(current) andbetter-sqlite3(fix(memory): replace node:sqlite with better-sqlite3 to enable sqlite-vec on macOS #90323). A future driver that coerced a BLOB to a string would break reads. The read detector falls back toparseEmbeddingfor strings, and this can move to an additiveembedding_blobcolumn with no behavior change if that ever happens.vectorToBlob, and tests covering both legacy and new rows.Current review state
codex reviewpassed clean, and the real-behavior proof above demonstrates the live upgrade path on the real cache functions.