Skip to content

Commit

Permalink
Only insert or remove input if needed (bevyengine#4273)
Browse files Browse the repository at this point in the history
# Objective

We are currently inserting an `input` into `pressed` even if it is already pressed. This also applies to releasing an input. This is not a big deal, but since we are already checking if the `input` is pressed or not we might as well remove the cost of the value update caused by the `pressed.insert` method.

Related to bevyengine#4209

## Solution

Only insert or remove input if needed.
  • Loading branch information
KDecay authored and ItsDoot committed Feb 1, 2023
1 parent 24c9f51 commit 490f0f9
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions crates/bevy_input/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,10 @@ where
{
/// Register a press for input `input`.
pub fn press(&mut self, input: T) {
if !self.pressed(input) {
// Returns `true` if the `input` wasn't pressed.
if self.pressed.insert(input) {
self.just_pressed.insert(input);
}

self.pressed.insert(input);
}

/// Check if `input` has been pressed.
Expand All @@ -70,11 +69,10 @@ where

/// Register a release for input `input`.
pub fn release(&mut self, input: T) {
if self.pressed(input) {
// Returns `true` if the `input` was pressed.
if self.pressed.remove(&input) {
self.just_released.insert(input);
}

self.pressed.remove(&input);
}

/// Check if `input` has been just pressed.
Expand Down

0 comments on commit 490f0f9

Please sign in to comment.