Repository navigation
perf(usb_host): no heap work per input report in the dispatch path - #972
Merged
Merged
Conversation
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
Contributor
There was a problem hiding this comment.
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::dequetostd::vectorand 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
Copilot stopped reviewing on behalf of
finger563 due to an error
December 3, 2026 06:38
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
The
UsbHostdispatch path no longer does heap work per input report.Before this change, each wake of the dispatch task built a
std::dequefor its batch and freed it. libstdc++ allocates a deque's map and first node even when it is empty. The queue itself was astd::dequetoo, 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:max_queued_eventsplus room for the disconnect events the queue always keeps.Testing
usb_host/examplebuilds for the ESP32-S3.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