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

Offen
#64 0 Kommentare 0 Reaktionen 1 zugewiesene Person Auf GitHub ansehen

Maintainer antworten meist innerhalb von 1 Tag

@SahilKhan101 arbeitet bereits daran.

Seit 01.12.2026.

  • #67 von @SahilKhan101 — offen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Beschreibung

enhancement

Problem

For a query with ORDER BY … LIMIT L, every write that touches the window rebuilds the client's whole top-L list and compares it with the previous one to find what changed. That costs O(L) per write, even though a single-row write usually moves only one or two rows in or out of the top L.

For example, on a newest-first feed with LIMIT 200, each new row means copying and comparing about 400 keys just to send the client one add and one delete. Writes that land in the buffer below the top L pay the same cost and end up sending nothing.

On busy feeds this adds up, and since the engine is single-threaded, it slows every other table too.

Proposal

The window already keeps its rows in sorted order, so it can work out the change from the position where the write landed:

  • Inserted inside the top L: add it, and remove the row it pushed out.
  • Removed from inside the top L: remove it, and add the row that moves up.
  • Updated in place: pass the update through.
  • Inserted or removed below the top L: nothing to send.

This works the same for ASC and DESC. Rarer cases keep using today's full comparison, so correctness never depends on the new path: several changes in one step, a storage read landing many rows, and pages filtered through a join condition.

The messages sent to the client must stay exactly as they are today.

Testing

  • A randomized test that runs the old and new diff on the same writes and checks they produce identical output.
  • A benchmark of a feed with steady inserts at different limits. Today's cost should grow with L; after the change it should stay flat.
Vorherrschende Sprache
Rust
Sterne
129
Forks
7
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
5

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus juspay/streamgres

Alle Issues in juspay/streamgres

Ähnliche Issues

Weitere Issues zu Rust

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.