Optimizing Push Pipeline: Three Causes Of Stale Frames

by Jule 55 views
Optimizing Push Pipeline: Three Causes Of Stale Frames

Optimizing Push Pipeline: Three Causes of Stale Frames in Fast Typing

Introduction

Ever experienced that frustrating moment when you're typing rapidly in your terminal, only to find that the characters aren't rendering as expected? Even after the fixes in PR #225, some users might still encounter this issue. Let's dive into the remaining causes and find solutions.

1. push_frame drops the newest frame on a full channel

When the channel is full, push_frame drops the newest frame, causing the client to render an older state. This behavior contradicts the explicit description in the doc comment above the function. To fix this, we need to swap the transport or add oldest-drop semantics.

2. Writer thread's continue starves the frame drain

The writer thread's continue statement skips the frame drain, allowing frames to accumulate in the bounded channel faster than they are drained. This issue can be resolved with a simple one-line change: drop the continue statement and let the loop fall through to the frame drain every iteration.

3. DumpState double-pushes the same frame to the polling client

For the client that originated the dump-state request, the server pushes the same frame twice, once through push_frame and once through resp.send. This double-push causes two copies of a 50-100 KB frame to be written to the client's socket. To fix this, either skip push_frame when handling a DumpState from a persistent client or skip resp.send.

4. SendText / SendKey don't set state_dirty

None of these commands set state_dirty, which can cause a sub-frame race that defers the echo under load. Setting state_dirty on these commands can improve predictability.

Suggested ordering

To address these issues effectively, consider fixing the cheaper ones first:

  1. Remove the continue in the writer thread (#2).
  2. Stop the DumpState double-push to the requesting client (#3).
  3. Swap the transport or add oldest-drop semantics to push_frame (#1).
  4. Set state_dirty on SendText, SendKey, and SendPaste (#4).

Environment

  • Windows 11
  • psmux main at a138a9e (post #225 / #228)
  • Reproducible with rapid typing in pwsh; less severe than pre-#225 but still observable

Related

  • #224 - original report (closed by #225)
  • PR #225 - bounded sync_channel(16) fix