feat(): Controller Performance Changes (WinHandler)#1455
Conversation
…n every frame. Should see a tiny performance improvement.
…Buttons. Note: these changes are only for the single-controller support. Later we should update this file to do proper multi-controller support.
…the same input listening. Checks for the running thread, and will run it if not already running.
📝 WalkthroughWalkthroughWinHandler refactors memory allocation for SDL button state and thread startup safety. A persistent ChangesGamepad memory and thread safety improvements
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@app/src/main/java/com/winlator/winhandler/WinHandler.java`:
- Around line 551-554: The two unsynchronized check-then-set guards around the
field running permit concurrent start() calls to invoke startSendThread() twice;
replace both guards with a single thread-safe startup guard: change running to
an AtomicBoolean (or make it volatile and protect with a synchronized block) and
use compareAndSet(false, true) in start() to atomically flip the flag and call
startSendThread() exactly once; remove the duplicate check at the other site so
startSendThread() is only invoked from the single guarded path (referencing the
running field, start() method, and startSendThread()).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: eb57b0ae-4ef6-490f-83f0-4324647c1556
📒 Files selected for processing (1)
app/src/main/java/com/winlator/winhandler/WinHandler.java
| if(!this.running) { | ||
| this.running = true; | ||
| startSendThread(); | ||
| } |
There was a problem hiding this comment.
Make send-thread startup guard atomic and single-sourced.
Line 551 and Line 575 both do unsynchronized check-then-set on running, so concurrent start() calls can still pass the guard and invoke startSendThread() twice. Consolidate to one guarded block and make visibility/thread-safety explicit.
Suggested fix
@@
- private boolean running;
+ private volatile boolean running;
+ private final Object lifecycleLock = new Object();
@@
- if(!this.running) {
- this.running = true;
- startSendThread();
- }
+ synchronized (lifecycleLock) {
+ if (this.running) return;
+ this.running = true;
+ startSendThread();
+ }
@@
- if(!running) {
- running = true;
- startSendThread();
- }
+ // send thread already started in the lifecycle guard aboveAlso applies to: 575-578
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@app/src/main/java/com/winlator/winhandler/WinHandler.java` around lines 551 -
554, The two unsynchronized check-then-set guards around the field running
permit concurrent start() calls to invoke startSendThread() twice; replace both
guards with a single thread-safe startup guard: change running to an
AtomicBoolean (or make it volatile and protect with a synchronized block) and
use compareAndSet(false, true) in start() to atomically flip the flag and call
startSendThread() exactly once; remove the duplicate check at the other site so
startSendThread() is only invoked from the single guarded path (referencing the
running field, start() method, and startSendThread()).
Description
Very small changes to improve performance without negatively impacting Controller behaviour.
Found 2 issues to adjust so increase performance:
Note: This still requires a bunch of testing etc.
Recording
Type of Change
Checklist
#code-changes, I have discussed this change there and it has been green-lighted. If I do not have access, I have still provided clear context in this PR. If I skip both, I accept that this change may face delays in review, may not be reviewed at all, or may be closed.CONTRIBUTING.md.Summary by cubic
Pre-allocated the controller
sdlButtonsarray and added a guard to prevent starting the send thread twice. This removes per-frame allocations and fixes a race that could create duplicate threads, improving controller performance and stability (single-controller path only).Written for commit 9f32503. Summary will update on new commits. Review in cubic
Summary by CodeRabbit