From f0c1c35e8f5882aa38ba44ac0e01810319f14b4c Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 7 Aug 2017 12:08:11 +0200 Subject: [PATCH] Significant speed improvement in key lookup code Instead of storing the layer for each key, store the keycode, so that lookups are considerably faster (one array lookup instead of two). This saves us almost a full millisecond per scan cycle. Furthermore, inline `Layer_.lookup`, saving us even more time. Signed-off-by: Gergely Nagy --- src/layers.cpp | 10 ++-------- src/layers.h | 6 ++++-- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/layers.cpp b/src/layers.cpp index d0622118..18ad41e2 100644 --- a/src/layers.cpp +++ b/src/layers.cpp @@ -4,7 +4,7 @@ static uint8_t DefaultLayer; static uint32_t LayerState; uint8_t Layer_::highestLayer; -uint8_t Layer_::keyMap[ROWS][COLS]; +Key Layer_::keyMap[ROWS][COLS]; Key(*Layer_::getKey)(uint8_t layer, byte row, byte col) = Layer.getKeyFromPROGMEM; bool Layer_::repeat_first_press = false; @@ -89,19 +89,13 @@ Layer_::updateKeyCache(byte row, byte col) { Key mappedKey = (*getKey)(layer, row, col); if (mappedKey != Key_Transparent) { - keyMap[row][col] = layer; + keyMap[row][col] = mappedKey; break; } } } } -Key Layer_::lookup(byte row, byte col) { - uint8_t layer = keyMap[row][col]; - - return (*getKey)(layer, row, col); -} - uint8_t Layer_::top(void) { for (int8_t i = 31; i >= 0; i--) { if (bitRead(LayerState, i)) diff --git a/src/layers.h b/src/layers.h index 06eb7e75..69d84899 100644 --- a/src/layers.h +++ b/src/layers.h @@ -8,7 +8,9 @@ class Layer_ { public: Layer_(void); - static Key lookup(byte row, byte col); + static Key lookup(byte row, byte col) { + return keyMap[row][col]; + }; static void on(uint8_t layer); static void off(uint8_t layer); static void move(uint8_t layer); @@ -36,7 +38,7 @@ class Layer_ { private: static uint8_t highestLayer; - static uint8_t keyMap[ROWS][COLS]; + static Key keyMap[ROWS][COLS]; }; Key layer_getKey(uint8_t layer, uint8_t r, uint8_t c);