Poll gamepads every frame without allocating - #9470
Merged
Merged
Conversation
GamePads#update() called poll() with no argument, so the default `pads = []` allocated a fresh array every frame. update() now walks the devices directly through a shared private _poll() that only collects into an array when the caller supplies one; poll() keeps its public signature and return value. Also remove the remaining per-frame garbage on the polling path: GamePad#update copies the axes by index instead of spreading into push(), and findByIndex() uses a loop instead of allocating a closure for Array#find. The Gamepad API is now resolved on each call rather than bound at module load, so the polling path can be exercised under Node by installing navigator.getGamepads before constructing GamePads. Fix GamePadButton treating a numeric previous value of 0 as "no previous state", which meant synthesized (axis-driven) buttons never reported wasPressed or wasTouched from the resting position. Fixes #842 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Build size reportThis PR changes the size of the minified bundles.
|
3 tasks done
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The test suite has a Node 18 setup issue, and the support flag can become inconsistent with the per-call API lookup.
Get a fresh assessment by requesting another Copilot review.
Review effort: Lite
Findings: 1
What changed in this PR
Optimizes gamepad polling to avoid per-frame allocations and fixes synthesized-button edge tracking.
Changes:
- Reuses polling state and preserves the public
poll()API. - Copies axes without iterator or temporary-array allocations.
- Fixes numeric previous-state handling and adds regression tests.
| File | Summary |
|---|---|
test/platform/input/game-pads.test.mjs |
Adds coverage for polling reuse, axis changes, and button edges; test setup must install the JSDOM navigator on globalThis. |
src/platform/input/game-pads.js |
Optimizes polling and state updates; gamepadsSupported may remain stale when the API becomes available after construction. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This branch was successfully deployed
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.

Fixes #842. Supersedes #8203.
GamePads#update()calledpoll()with no argument, so the defaultpads = []allocated a new array every frame, which is what #842 reported (the originalpoll()allocation was fixed in #1154, but the per-frame default parameter was left behind).Changes
GamePads#update()no longer allocates. The device walk lives in a private_poll(pads)that only collects results when the caller passes an array.poll(pads = [])keeps its public signature and return value.GamePad#update()copies axes by index instead ofpreviousAxes.push(...axes)/axes.push(...gamepad.axes), so no iterator or temporary array is created per frame. The previous axes are snapshotted at the old length before the new length is adopted (the case @mvaligursky flagged in Fixed unecessary axes array reallocation in GamePad update - Issue (#… #8203).findByIndex()uses a loop instead of allocating a closure forArray#findon every polled device.navigator.getGamepadsbefore constructingGamePads.GamePadButtonguarded the previous state withif (previous), so a numeric previous value of0(a stick at rest) was treated as "no previous state". Synthesized axis-driven buttons (PAD_UP/PAD_DOWN/PAD_LEFT/PAD_RIGHTon the dual-stick map, or anyupdateMapwithsynthesizedButtons) therefore never reportedwasPressedorwasTouched. The guard is nowprevious !== undefined.Tests
New
test/platform/input/game-pads.test.mjs(11 tests) coverspoll()with and without a target array, instance reuse across polls,update()picking up new devices and tracking button edges over frames, synthesized-button edges through the previous-axes bookkeeping (including axis-count changes in both directions), and theGamePadButtonprevious-value cases.Before the
GamePadButtonfix, the two synthesized-buttonwasPressedtests fail.🤖 Generated with Claude Code