Fix: mq.poll() materialized the entire queue to return one batch#2019
Open
josh-j wants to merge 1 commit into
Open
Fix: mq.poll() materialized the entire queue to return one batch#2019josh-j wants to merge 1 commit into
josh-j wants to merge 1 commit into
Conversation
Co-Authored-By: Claude Fable 5 <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.
Problem
DataStoreMQ.poll()retrieves a batch of messages viads.luaQuery(prefix, { limit }). However,queryLuamaterializes the entire prefix range from IndexedDB into memory first, and only then applies the limit viaapplyQuery:So polling 3 messages off a queue with N messages costs a full O(N) IndexedDB cursor walk. During a full space reindex the index queue holds every file in the space, and the queue is re-polled after each small batch — making total queue overhead O(N²/batchSize). On a ~4,000-file space that's millions of wasted cursor steps, a substantial share of total reindex wall-clock time, growing quadratically with space size.
Fix
Replace the
luaQuerycall with a direct cursor scan overds.query({ prefix })that breaks aftermaxItems. The IndexedDB-backedquery()is a true cursor generator, so breaking early stops cursor advancement. Each poll is now O(batchSize) regardless of queue depth.No behavior change otherwise: the same messages are returned in the same (key) order, and the move-to-processing logic is untouched.
Testing
npx vitest run— 873 passed, no regressions (the handful of suite files that fail to load do so identically onmainin my environment).🤖 Generated with Claude Code