perf(retrieve): cache category embeddings + lazy item pool (-88% latency)#399
Open
2233admin wants to merge 1 commit intoNevaMind-AI:mainfrom
Open
perf(retrieve): cache category embeddings + lazy item pool (-88% latency)#3992233admin wants to merge 1 commit intoNevaMind-AI:mainfrom
2233admin wants to merge 1 commit intoNevaMind-AI:mainfrom
Conversation
…ncy) - Cache category summary embeddings across calls (230ms → 0ms on cache hit) - Replace full list_items scan (335ms/1420 items) with targeted get_item for hit IDs only - Fix _rag_build_context to use 'in state' check instead of falsy-or fallback - Retrieve hot path: 633ms → 70-80ms (1415 items, 5 top-k hits) - All 121 tests passing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Retrieve hot path latency reduced from 633ms → 70-80ms (-88%) on a 1415-item corpus with 5 top-k hits.
Two independent optimizations targeting the two largest segments identified via profiling:
1. Category summary embedding cache (230ms → 0ms)
_rank_categories_by_embeddingre-embeds category summaries on every call, even though summaries rarely change. Added an instance-level dict cache keyed by(category_id, summary)tuples — automatic invalidation when summaries update, zero config.2. Lazy item pool (335ms → ~10ms)
_rag_rank_itemscalledlist_items()scanning all 1420 rows from Postgres on every retrieve. Replaced with targetedget_item()calls for only the hit IDs returned by vector search (typically 5). Also fixed_rag_build_contextto use"item_pool" in stateinstead of falsy-or fallback, preventing accidental full scans when the lazy pool is an empty dict.Profiling breakdown (before)
rank_categorieslist_items(item pool)vector_searchgraph_recallbuild_contextProfiling breakdown (after)
rank_categoriesitem poolvector_searchgraph_recallbuild_contextChanges
src/memu/app/retrieve.py— 1 file, +19/-3 linesTesting