Skip to content

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
mainfrom
perf/window-client-diff
Open

SahilKhan101 wants to merge 2 commits into
mainfrom
perf/window-client-diff

Conversation

@SahilKhan101

@SahilKhan101 SahilKhan101 commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Two changes to ORDER BY … LIMIT windows. 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 with a = 2 while a row with a = 1 was 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:

  • The window remembers which keys changed since the last diff, up to 64. Past that, or after a rebuild, it falls back to a full comparison.
  • For windows with a unique order (client queries always end in the primary key), only the changed rows, the rows the step names, and the rows within m positions of L can cross the edge. Only those are compared.
  • The operations come out in exactly the old order, because the join layer counts join values in that order.
  • Pages and orders with ties keep the full comparison.

Results (new bench scenario 8, STREAMGRES_BENCH_ONLY=limits)

Engine µs per write, inserts at the top of the feed:

Subscriptions Before After
1 at L=50 3.7 2.1
1 at L=800 39.0 2.6
4 distinct limits 26.3 7.0
16 distinct limits 321 38.8
16 distinct limits, LEFT JOIN 359 74

Reads, refills and operations sent are unchanged.

Testing

  • New 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 on main (that's how the bug was found) and passes here.
  • Debug self-check: debug builds compute every diff both ways and assert they match, so the whole suite checks the new path on every step.
  • Full suite passes in debug and release, including the live Postgres tests.

SahilKhan101 and others added 2 commits December 3, 2026 03:12
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
SahilKhan101 force-pushed the perf/window-client-diff branch from 905ef72 to 8aca35e Compare December 2, 2026 21:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make the top-L client diff cost depend on what changed, not on the limit

1 participant