Conversation
… optimization, and code clarity Co-authored-by: Hexagon <419737+Hexagon@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Review performance and architectural soundness
Fix cache eviction order, redundant matchesQuery recursion, and sync/async misuse
Feb 20, 2026
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.
Several correctness and performance issues found during architectural review of the core KV primitives.
Cache eviction was backwards (+ falsy-zero bug)
KVLedgerCache.evictOldestEntries()usedpop()— removing the newest entry — instead ofshift()to remove the oldest. The documented intent is FIFO eviction; the implementation was LIFO, so freshly-written entries were discarded while stale ones accumulated.Separately,
if (oldestOffset)silently skipped eviction for any entry at offset0(falsy), leaking memory.matchesQuery()did redundant O(n²) recursive workInside the
forloop, at every stepi, the code created a newKVKeyInstance, sliced both key and query arrays, and re-ranmatchesQueryon the tail — work the outer loop was already doing in subsequent iterations. Removed entirely; behavior is unchanged.KVIndex.get()calledkey.get()on every recursionkey.get()[keyIndex]inside the innerrecursefunction re-invoked the getter on every call. Cached toconst keyParts = key.get()once before entering recursion.Unnecessary
awaiton sync methods inkv.tstransaction.create()andtransaction.toUint8Array()are synchronous but were called withawait, misleading readers about the async nature of the surrounding code.🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.