Repository navigation
Diff window prefixes by what changed, and refill when an update moves a held row down - #67
Open
SahilKhan101 wants to merge 2 commits into
Open
SahilKhan101 wants to merge 2 commits into
SahilKhan101 wants to merge 2 commits into
Conversation
A window asked for a refill only when a row left it. An update that moves a held row to a worse position also shrinks the part of the window known to be complete, but nothing checked: the window then showed the next row it held even when that row lay beyond the frontier, with better rows still unread in storage. For `ORDER BY a, id LIMIT 3` a client was shown a row with `a = 2` while one with `a = 1` was never read. Check for a refill after any change to a row the window held. tests/window_diff.rs runs ten windowed subscriptions (ascending, descending, several columns, ties, filters, limits from 1 to past the table's size) under random inserts, updates and deletes, and checks after every write that each client's view is the best L rows in storage. It fails on the previous code. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every step that touched a windowed subscription copied the first L keys of its window, hashed them and the L keys delivered last time, and compared the two to find what the client should be told. That cost O(L) per write even when one row crossed the edge of the prefix, and it was nine tenths of the engine's time on a windowed write. The window now remembers which keys it placed, moved or removed since the last diff (up to 64; past that, or after a rebuild, the next diff compares the whole prefix). For a window whose order is unique, which the client's queries always are, a row can enter or leave the prefix only if it changed, or if the m changed rows pushed it across position L, leaving it within m places of L. The diff compares only those rows and the rows the step's operations name, against the set of keys delivered last. Its operations come out in the order a whole comparison gives (the join layer counts join values in that order): the rows leaving in the order they were delivered in, then the rows entering or staying with operations in the prefix's order now. Pages and windows with ties keep the whole comparison. Debug builds compute every diff both ways and assert that they agree, so the whole test suite checks the new path on every step. The bench gains scenario 8, LIMIT variants of one feed. Engine time per write, inserts at the top of the feed, before and after: 1 subscription, L = 50 3.7 us -> 2.1 us 1 subscription, L = 800 39.0 us -> 2.6 us 4 distinct limits, 50..200 26.3 us -> 7.0 us 16 distinct limits, 50..800 321 us -> 38.8 us 16 distinct limits, LEFT JOIN 359 us -> 74 us Reads, refills and operations sent are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
SahilKhan101
force-pushed
the
perf/window-client-diff
branch
from
December 2, 2026 21:50
905ef72 to
8aca35e
Compare
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
Two changes to
ORDER BY … LIMITwindows. Closes #64.1. Bug fix: refill after an update moves a held row down (
f883a84, one line)A window asked for a refill only when a row left it. An update that moves a held row to a worse position also shrinks the part of the window known to be complete, but no refill was asked for. The window then showed the next row it held, even when that row was beyond the frontier and better rows were still unread in storage.
Example: for
ORDER BY a, id LIMIT 3, a client was shown a row witha = 2while a row witha = 1was never read.Fix: check for a refill after any change to a row the window held.
2. #64: the prefix diff costs what changed, not L (
905ef72)The client diff copied and compared the whole top-L on every write: O(L), and about 90% of engine time on windowed writes. Now:
Results (new bench scenario 8,
STREAMGRES_BENCH_ONLY=limits)Engine µs per write, inserts at the top of the feed:
LEFT JOINReads, refills and operations sent are unchanged.
Testing
tests/window_diff.rs: 10 windowed subscriptions (ASC/DESC, multi-column orders, ties, filters, limits from 1 to past the table size) under 17,500 random writes. After each write, every client's view is checked against the true top-L in storage. It fails onmain(that's how the bug was found) and passes here.