refactor(engine): per-component fn-memo cache with prefetch + flush#1991
Merged
Conversation
Replace the previous `FnMemoAccessor` handle (#1990) with a `FnMemoCache` that owns the function-memoization lifecycle at the engine layer. - Eager prefix-scan prefetch at the start of build mode loads every fn-memo entry for the component into memory as `Stored(bytes)`. - `reserve_memoization` no longer reads from the database. Cache slot is looked up (or inserted as `Pending` on cache miss); `Stored` entries are lazy-decoded on first access via `decode_stored_entry`. - `finalize_fn_call_memoization` now walks the cache in memory and decodes `Stored` entries it reaches via dep chains — zero DB reads. - Commit-time flush is one consolidated pass: per-entry write/delete when the cache is fully loaded; prefix-delete + write-new when it isn't (covers `full_reprocess` and delete mode). - Net I/O per build: one prefix scan up front instead of O(N) point lookups during processing + one prefix scan at commit. Finalize's transitive dep walk drops from one point read per dep to zero. Storage API changes: - New: `list_fn_memos` (prefix read; layer-neutral name matching `list_child_existence` / `list_tombstones`), `delete_fn_memo`, `delete_all_fn_memos`. - Drop: `read_fn_memo`, `retain_fn_memos`. - `write_fn_memo` unchanged. Engine API changes: - New `FnCallMemoEntry::Stored(Vec<u8>)` variant; lazy-decoded. - `FnMemoCache<Prof>` on `ComponentBuildingState` replaces the `fn_call_memos: HashMap<…>` field; carries an `is_fully_loaded` flag driving the flush strategy. - `ComponentProcessorContext::prefetch_fn_memos()` runs in `execute_once` right after the build semaphore acquisition; idempotent and skipped under `full_reprocess` / delete mode. - `Committer::commit_in_txn` / `commit` lose the `all_memo_fps` + `memos_without_mounts_to_store` parameters in favor of a single `fn_memos: FnMemoCache<Prof>` param. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
Replace the previous
FnMemoAccessorhandle (#1990) with aFnMemoCachethat owns the function-memoization lifecycle at the engine layer.Stored(bytes). Subsequentreserve_memoizationcalls serve from the cache; entries lazy-decode on first access viadecode_stored_entry.finalize_fn_call_memoizationwalks the cache directly and decodesStoredentries it reaches via dep chains — no DB reads at finalize, down from one read per transitive dep.FnMemoCache::flush_to_dbconsolidates the write loop and the retain GC. When the cache is fully loaded: per-entry write/delete based on entry state. When it isn't (full_reprocess, delete mode): prefix-delete + write the new entries.Storage API: new
list_fn_memos(prefix read; layer-neutral name matchinglist_child_existence/list_tombstones),delete_fn_memo,delete_all_fn_memos. Dropread_fn_memoandretain_fn_memos.write_fn_memounchanged.Engine API: new
FnCallMemoEntry::Stored(Vec<u8>)variant; newFnMemoCache<Prof>onComponentBuildingStatewith anis_fully_loadedflag driving the flush strategy;ComponentProcessorContext::prefetch_fn_memos()invoked inexecute_onceafter the build semaphore is acquired.Committer::commit_in_txn/committake a singlefn_memos: FnMemoCache<Prof>parameter instead of the previousall_memo_fps+memos_without_mounts_to_storepair.No behavior change for the LMDB backend.
Test plan
CI. Locally:
cargo test -p cocoindex_core --lib(30/30),pytest python/tests/core/(358/358). The full engine lifecycle (component memoization, fn memoization with cache hits / state validation / re-execution, dep transitive expansion, full_reprocess, live components, GC) exercises the new cache paths.