From f5e53ba6f5bc1e0b9ab9f3e1de591fdcd522cee9 Mon Sep 17 00:00:00 2001 From: KDecay Date: Tue, 29 Mar 2022 22:39:16 +0000 Subject: [PATCH] Only insert or remove input if needed (#4273) # 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 #4209 ## Solution Only insert or remove input if needed. --- crates/bevy_input/src/input.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/crates/bevy_input/src/input.rs b/crates/bevy_input/src/input.rs index a27d738ae6..2ed7b7a634 100644 --- a/crates/bevy_input/src/input.rs +++ b/crates/bevy_input/src/input.rs @@ -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. @@ -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.