Skip to content

perf(usb_host): no heap work per input report in the dispatch path - #972

Merged
finger563 merged 2 commits into
mainfrom
perf/usb-host-dispatch-no-alloc
Dec 3, 2026
Merged

finger563 merged 2 commits into
mainfrom
perf/usb-host-dispatch-no-alloc

Conversation

@finger563

Copy link
Copy Markdown
Contributor

Summary

The UsbHost dispatch path no longer does heap work per input report.

Before this change, each wake of the dispatch task built a std::deque for its batch and freed it. libstdc++ allocates a deque's map and first node even when it is empty. The queue itself was a std::deque too, which allocates and frees a node as reports stream through it. With a controller that reports at a fixed rate, such as a Switch Pro at 125 Hz, that was heap traffic on every report, on a task that runs above the games.

Now the queue and the dispatch task's batch are two std::vectors that swap buffers on each wake:

  • Reserved up front: both are reserved before the dispatch task starts, sized at max_queued_events plus room for the disconnect events the queue always keeps.
  • No heap work in steady state: a steady stream of reports does no heap work on either task.
  • Behaviour unchanged: same bound, same drop and evict rules, same order.

Testing

  • Build: usb_host/example builds for the ESP32-S3.
  • On a Tab5: tab5-emu, using The Phantom Menace's deterministic benchmark (a scripted run, vblanks 1800–3000 timed unpaced), with a Switch Pro attached and streaming:
Dispatch task's share of a core Benchmark
Before 4% 17.40 s
After 3% 17.32 s

Most of what remains per report is the mutex and condition-variable handoff between the HID driver's task and the dispatch task, plus cache misses while a busy game thrashes the caches. That could be a follow-up, for example a task notification instead of std::condition_variable.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY

Every wake of the dispatch task built a std::deque for its batch (libstdc++
allocates a deque's map and first node even when empty) and freed it, and
the queue itself was a std::deque, which allocates and frees a node as
reports stream through it. With a controller that reports at a fixed rate
(a Switch Pro, 125 Hz) that was heap traffic on every report, on a task
that runs above the games.

The queue and the dispatch task's batch are now two std::vectors that swap
buffers on each wake, reserved before the dispatch task starts
(max_queued_events plus room for the disconnects the queue always keeps),
so a steady stream of reports does no heap work on either task. Behaviour
is unchanged: same bound, same drop / evict rules, same order.

Measured on a Tab5 (tab5-emu, The Phantom Menace's benchmark with a Switch
Pro streaming): the dispatch task 4% -> 3% of a core, the benchmark
17.40 s -> 17.32 s.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
Copilot AI balanced review requested due to automatic review settings December 3, 2026 06:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Copilot review overview

3 open issues
What changed in this PR

This PR removes per-input-report heap allocations from the UsbHost dispatch path by replacing std::deque usage with two pre-reserved std::vector buffers that swap each wake.

Changes:

  • Pre-reserve queue/batch capacity before starting the dispatch task.
  • Replace the event queue and dispatch batch from std::deque to std::vector and swap buffers per wake.
  • Clear the persistent batch buffer after each dispatch loop (and during shutdown).
File Description
components/​usb_host/​src/​usb_host.cpp Reserves queue/batch upfront and swaps std::vector buffers in the dispatch task to avoid steady-state allocations.
components/​usb_host/​include/​usb_host.hpp Switches queue/batch types to std::vector, adds lifecycle headroom constant, and documents the new buffering approach.

Review effort: Lite

Comment thread components/usb_host/src/usb_host.cpp Resolved
…room wording

stop_dispatch_task() no longer clears batch_ (the dispatch task empties it at
the end of every pass, its last one included, and Task::stop() joins), so
nothing but the dispatch task touches it. The kLifecycleHeadroom comment now
says what is guaranteed: Input reports never take the queue past
max_queued_events, so the report path never allocates; only a burst of more
disconnects than the headroom on a full queue can grow a buffer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
@finger563
finger563 merged commit 8382449 into main Dec 3, 2026
203 of 205 checks passed
@finger563
finger563 deleted the perf/usb-host-dispatch-no-alloc branch December 3, 2026 20:45
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.

2 participants