From 3c209f04b470a4d919db2a8fe5d1ab05f5e3288a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Trzci=C5=84ski?= Date: Mon, 28 Nov 2022 19:21:12 +0100 Subject: [PATCH] Reduce memory allocations in `KeyboardController` --- SteamController/Devices/KeyboardController.cs | 29 +++++++------------ 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/SteamController/Devices/KeyboardController.cs b/SteamController/Devices/KeyboardController.cs index e07962d..470087f 100644 --- a/SteamController/Devices/KeyboardController.cs +++ b/SteamController/Devices/KeyboardController.cs @@ -1,5 +1,4 @@ using WindowsInput; -using KeyRepeats = System.Tuple; namespace SteamController.Devices { @@ -10,8 +9,8 @@ namespace SteamController.Devices InputSimulator simulator = new InputSimulator(); - Dictionary keyCodes = new Dictionary(); - Dictionary lastKeyCodes = new Dictionary(); + Dictionary keyCodes = new Dictionary(); + Dictionary lastKeyCodes = new Dictionary(); public KeyboardController() { @@ -69,7 +68,8 @@ namespace SteamController.Devices { if (keyCodes.ContainsKey(key)) return; - var keyRepeat = lastKeyCodes.GetValueOrDefault(key) ?? new KeyRepeats(DateTime.Now.Add(FirstRepeat), 0); + if (!lastKeyCodes.TryGetValue(key, out var keyRepeat)) + keyRepeat = DateTime.Now.Add(FirstRepeat); keyCodes.Add(key, keyRepeat); } } @@ -83,7 +83,7 @@ namespace SteamController.Devices internal void BeforeUpdate() { lastKeyCodes = keyCodes; - keyCodes = new Dictionary(); + keyCodes = new Dictionary(); } internal void Update() @@ -91,20 +91,14 @@ namespace SteamController.Devices // Key Up: it is missing now foreach (var keyUp in lastKeyCodes.Except(keyCodes)) { - try - { - // if key was not yet pressed, send it - if (keyUp.Value.Item2 < 0) - simulator.Keyboard.KeyPress(keyUp.Key); - simulator.Keyboard.KeyUp(keyUp.Key); - } + try { simulator.Keyboard.KeyUp(keyUp.Key); } catch (InvalidOperationException) { } } // Key Down: new keys being down - foreach (var keyUp in keyCodes.Except(lastKeyCodes)) + foreach (var keyDown in keyCodes.Except(lastKeyCodes)) { - try { simulator.Keyboard.KeyDown(keyUp.Key); } + try { simulator.Keyboard.KeyDown(keyDown.Key); } catch (InvalidOperationException) { } } @@ -112,16 +106,13 @@ namespace SteamController.Devices var now = DateTime.Now; foreach (var keyPress in keyCodes) { - if (keyPress.Value.Item1 > now) + if (keyPress.Value > now) continue; try { simulator.Keyboard.KeyPress(keyPress.Key); } catch (InvalidOperationException) { } - keyCodes[keyPress.Key] = new KeyRepeats( - DateTime.Now.Add(NextRepeats), - keyPress.Value.Item2 + 1 - ); + keyCodes[keyPress.Key] = DateTime.Now.Add(NextRepeats); } }