Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delay registering the Gamepad Hook #340

Merged
merged 1 commit into from
Jun 14, 2020

Conversation

CryZe
Copy link
Collaborator

@CryZe CryZe commented Jun 13, 2020

In the web we support gamepads. Unfortunately there's no callbacks or so that we could use, so instead we have to use an interval where we poll for button presses. This isn't super expensive, but still unnecessary if you don't use a gamepad. Additionally Chrome has the problem that it gets exclusive access to the gamepads and doesn't really yield control back until the whole browser is closed. So you really don't want that unless you really intend to use the gamepad for splitting. This is why the gamepad interval is now only being set when the first gamepad button is registered.

In the web we support gamepads. Unfortunately there's no callbacks or so
that we could use, so instead we have to use an interval where we poll
for button presses. This isn't super expensive, but still unnecessary if
you don't use a gamepad. Additionally Chrome has the problem that it
gets exclusive access to the gamepads and doesn't really yield control
back until the whole browser is closed. So you really don't want that
unless you really intend to use the gamepad for splitting. This is why
the gamepad interval is now only being set when the first gamepad button
is registered.
@CryZe CryZe added enhancement An improvement for livesplit-core. hotkey This is about the hotkey implementation. performance Affects the performance of the code. labels Jun 13, 2020
@CryZe CryZe requested a review from wooferzfg June 13, 2020 20:41
Copy link
Member

@wooferzfg wooferzfg left a comment

Choose a reason for hiding this comment

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

Works well when testing with LiveSplit One.

@@ -31,11 +34,37 @@ impl Drop for Hook {
"keypress",
self.keyboard_callback.as_ref().unchecked_ref(),
);
window.clear_interval_with_handle(self.interval_id);
if let Some(interval_id) = self.interval_id.get() {
window.clear_interval_with_handle(interval_id);
Copy link
Member

Choose a reason for hiding this comment

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

It's definitely an edge case, but should we also be calling this in unregister if there's no more gamepad buttons registered anymore?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think that case matters much, as I think it's a super rare situation that someone was using gamepad, then stops using gamepad. And even then, the cost for the gamepad hook is still very minimal, and it stops being a cost the next time they reload. So overall, I'd say yes, we could possibly remove the hook when unregistering all gamepad buttons, but it has a much lesser impact.

@CryZe CryZe merged commit 6f1108b into LiveSplit:master Jun 14, 2020
@CryZe CryZe added this to the v0.12 milestone Jun 14, 2020
CryZe added a commit to CryZe/LiveSplitOne that referenced this pull request Jun 14, 2020
This updates livesplit-core which brings a variety of performance
improvements:

- The Layout State is now being reused and thus most frames don't
  require any heap allocations anymore. However we still serialize
  everything over into a JSON string for now, which puts a lot of
  garbage on the JS heap.
  LiveSplit/livesplit-core#334

- The frequent performance.now() calls we do, first lookup up the window
  and performance object every time. This tripled the amount of calls we
  do over into JavaScript, with each call into JavaScript being quite
  expensive in Chrome.
  LiveSplit/livesplit-core#335

- By introducing a timer snapshot mechanism we further reduce the calls
  to performance.now() to a single time.
  LiveSplit/livesplit-core#339

- Rust 1.44 regressed the performance of 128-bit integer multiplications
  by accident. Those are used for hashing the comparisons when looking
  up the times for a comparison, which is something we do very
  frequently. We however don't have many comparisons, so a simple Vec
  that we loop through is a bit faster, even in native code, and quite a
  bit faster in the web, because of the Rust 1.44 regression.
  LiveSplit/livesplit-core#338

- We delay registering the Gamepad Hook Interval until the first gamepad
  button is registered. Most people won't use a gamepad, so the interval
  just waits cpu time for no reason.
  LiveSplit/livesplit-core#340
@CryZe CryZe deleted the delayed-gamepad-hook branch November 16, 2021 20:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement An improvement for livesplit-core. hotkey This is about the hotkey implementation. performance Affects the performance of the code.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants