Skip to content

Adopt per-thread high-water-mark CAS in URB receive loop — skip cross-thread CAS on ~99% of packets #33

Description

@stanvx

Parent

Architecture review (2026-06-21), "Latency tricks worth lifting wholesale". Lifted from Satellite's net/receiver.cpp:99-103. Independent of the deepening series.

What to build

In usbip-server's URB receive loop, the per-URB latency sample is currently posted to a shared atomic. The cross-thread CAS contention on a 1 kHz+ URB stream is measurable. Lift Satellite's per-thread high-water-mark pattern:

  • Each worker thread keeps a thread_local HighWaterMark { local_peak_ns: u64, last_flushed_at: Instant }
  • On each URB, the worker compares its local latency_ns to local_peak_ns using a relaxed load (no cross-thread CAS)
  • Only when latency_ns > local_peak_ns does the worker perform the cross-thread atomic CAS on the global high-water-mark
  • The CAS frequency drops from "every URB" to "only when a new local peak is set" — empirically ~1% of packets in steady state

This is a small, self-contained optimization with measurable latency-tail improvement under load. It does not change wire protocol, error handling, or test surface beyond one new benchmark.

Acceptance criteria

  • HighWaterMark is a new private struct in usbip-server/src/urb_loop.rs (or wherever the receive loop lives)
  • Each URB receive updates the thread-local high-water-mark with a relaxed load, then conditionally CASes the global atomic
  • A new benches/urb_high_water_mark.rs benchmark compares the cross-thread CAS rate before and after (criterion-based, default sample size)
  • cargo test --workspace --release passes; cargo clippy --workspace -- -D warnings clean
  • No public API change; the LatencySample shape emitted on /api/events is unchanged
  • Brief KDoc on the new struct explains the relaxed-load + conditional-CAS pattern with a one-line citation of Satellite's receiver

Blocked by

None — can start immediately.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestready-for-agentTriage complete; ready for an agent to implement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions