← Back to Pull requests

perf(gliff-server): take blocking work off the session thread #89

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 25, 2026 13:25 UTC
Author

Summary

Each session runs on one current-thread tokio runtime, and its select! loop did the slow work inline. A frame's encode takes 7 to 8.5 ms on the GPU tier at 1080p and 21 to 43 ms on the CPU tier (docs/architecture.md, Measurements), and client input, acks and pings waited behind it. The next capture was requested only after the encode returned. Hyprland IPC calls, which block on a socket read with a 5 s timeout, also ran on the loop.

The loop stays the only owner of session state. Three commits move the blocking work out of it:

 select! loop
   client message, capture event, timers
-  then pump_encoder
-    encode_and_send
-      encoder.encode()             inline, 7 to 43 ms
-    request the next capture
+  then pump_encoder
+    dispatch_encode                to the gliff-encode thread
+    request the next capture       captured during the encode
+  encoder.finished()
+    finish_encode                  frame id, pacing, send

1. Hyprland IPC on the blocking pool. The loop's monitors() and mode calls go through spawn_blocking, so the reader and writer tasks keep running while Hyprland answers.
2. Encode on a dedicated thread. The gliff-encode thread builds and owns the encoder, because the Vulkan Video encoder is not Send. At most one encode is in flight. Frame ids are assigned in order when an encode finishes, and pacing still runs from the dispatch time. A frame whose output changed during its encode is dropped and forces a keyframe. A ladder step or resize finishes the encode in flight before its StreamConfig, so no frame at an old size follows one.
3. Open the GPU during output setup. VideoTier::open runs on spawn_blocking while setup_output creates the output and waits for its mode. Output errors are still reported first, with the same messages.

Evidence

Claude Opus 5.5 assisted with the implementation. I reviewed the code myself, as a Rust developer since before AI coding assistants.

  • Before: on the CPU tier, a client message waited up to 93 ms for the loop, and 1.5 to 11 % of messages waited over 5 ms.

  • After: no message waited over 5 ms on the CPU tier. The GPU tier keeps 57 fps on lan and dsl.

    scripts/bench.sh in a nested Hyprland 0.56.2 mirroring a 1648x1362 output, 20 s per run, two runs per cell. RTX 3060 with Vulkan Video on the GPU tier, OpenH264 2.6.0 on the CPU tier. The wait is the time from a client message reaching the session's channel to its handling, measured with a temporary debug log that is not part of this PR.

    | Tier, preset | Wait p99 ms, main | Wait p99 ms, PR | Longest wait ms, main | Longest wait ms, PR | fps, main | fps, PR |
    |---|---|---|---|---|---|---|
    | CPU, lan | 45.3, 40.4 | 0.0, 0.0 | 92.8, 55.3 | 0.0, 0.0 | 22.6, 21.9 | 30.8, 21.7 |
    | CPU, dsl | 40.7, 41.8 | 0.0, 0.0 | 80.7, 80.2 | 0.0, 0.1 | 22.8, 19.0 | 24.2, 20.9 |
    | CPU, satellite | 27.4, 23.3 | 0.0, 0.0 | 27.4, 23.3 | 0.0, 0.0 | 0.9, 2.8 | 0.6, 0.4 |
    | GPU, lan | 0.0, 0.0 | 0.0, 0.0 | 0.0, 0.0 | 0.0, 0.0 | 57.0, 57.1 | 56.9, 57.1 |
    | GPU, dsl | 0.1, 0.1 | 0.0, 0.0 | 6.7, 0.4 | 0.0, 0.0 | 57.2, 57.1 | 57.0, 57.1 |
    | GPU, satellite | 0.0, 0.0 | 11.2, 0.0 | 0.0, 0.0 | 11.2, 0.0 | 2.5, 1.3 | 9.8, 1.0 |

    Frame ids were strictly increasing in all 24 runs. The CPU tier's frame rate moves with background load (median encode 25 to 38 ms across runs), not with this PR; see Merge Danger. The one wait in the PR's GPU satellite run is the encoder rebuild after a ladder step to 824x680, which blocks the loop on main too.

  • Startup (commit 3): time to hello_ack on a --headless GPU session, four interleaved rounds. main: 869, 897, 873 and 877 ms. This PR: 502 ms in every round. Nested headless outputs on this machine never get their mode, so setup waited its full 500 ms and the whole GPU open fit inside it. On a host where the mode applies at once, the saving is only as long as that wait.
  • By hand on two machines, both on Omarchy 4.0.6 with Hyprland 0.56.2. The desktop runs gliff-server, and the laptop connects to it over ssh. Every check passed:

  • - the GPU tier streams a moving picture
    - resizing the client window during motion keeps the picture live, on the GPU tier and on the CPU tier
    - a private screen follows the window, and disconnecting removes it and ends the server
    - a private screen starts sooner than on main

  • scripts/e2e.sh on the laptop passed steps 1 to 7: the headless GPU server, the CPU tier matrix, and the CPU fallback after a mirrored resize. It then failed at 7b with E2E FAIL: rotated GPU pipeline. That step only runs gliff-probe pipeline on a rotated output. gliff-probe does not use gliff-server, and this PR changes only a comment in the crates it does use, so main fails the same way. Steps 7c to 12 did not run.
  • scripts/check.sh: CHECK PASS on each of the three commits (rustfmt, clippy -D warnings, 277 tests on the last). New unit tests: the encoder thread applies encodes and rate changes in order, and a failed rebuild keeps the old encoder.
  • Merge Danger

    Door: two-way.

    No protocol, config or CLI change. Reverting restores the inline encode.

    Blast Radius: video pipeline.

  • A ladder step or resize waits for the encode in flight before it applies, for at most one encode (21 to 43 ms on the CPU tier). The encoder rebuild still blocks the loop, as on main. Both are rare next to the per-frame wait this removes.

  • The CPU tier's frame rate does not rise. update_fps_cmd still caps it at one frame per measured encode time plus 10 %, so the overlap shows up as lower input latency, not more frames. Relaxing that cap belongs in its own PR.

  • The libva Display is now opened on one thread and driven from another. Its Send and Sync safety comment now says "one thread at a time", which every hand-off (a channel or a join) keeps.

  • #87 ends main with a 100 ms shutdown_timeout. Nothing from this PR is still running then: the GPU open is joined even when output setup fails, and dropping the session joins the encoder thread. The two branches merge without conflicts, and the merged tree passes clippy and the gliff-server tests. #80 (audio) adds work to the same loop, so the two may conflict.
  • Follow-ups, out of scope here: relax the CPU tier's fps cap, nested headless outputs that stay 0x0 (they block the headless cases of bench.sh and e2e.sh on some machines), and the rotated GPU pipeline failure at e2e step 7b.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: greptile-apps[bot] Review duration: 4m
    All Vouch review confidence checks passed
    Residual risk 0.42 = Change Risk (0.52) × (1 − Review Confidence (0.20)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 27% · Reviewer familiarity: 35%
    19%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #89"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 19.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 19.8% · Lower = rushed or shallow review
    19.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 41.7% · Moderate risk
    41.7%
    Repository