From a8fdd4b9c492a3083de89f3408d7d7045f2271eb Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Thu, 17 Aug 2017 11:01:58 +0200 Subject: [PATCH] Major performance optimization Now that we only care about the highest active layer, make sure we only do work if that changed between two cycles. This way, `onActivate()` will take care of the initial setup, `update()` will be an almost no-op for most of the time, and `refreshAt()` will take care of refreshing keys other plugins may have changed. This makes us about twice as fast as we were, on average. Signed-off-by: Gergely Nagy --- src/Kaleidoscope/Colormap.cpp | 21 ++++++++++----------- src/Kaleidoscope/Colormap.h | 2 ++ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Kaleidoscope/Colormap.cpp b/src/Kaleidoscope/Colormap.cpp index 56da9c9e..25a8264e 100644 --- a/src/Kaleidoscope/Colormap.cpp +++ b/src/Kaleidoscope/Colormap.cpp @@ -28,6 +28,7 @@ namespace kaleidoscope { uint16_t ColormapEffect::map_base_; uint8_t ColormapEffect::max_layers_; +uint8_t ColormapEffect::last_highest_layer_; void ColormapEffect::setup(void) { Kaleidoscope.use(&::EEPROMSettings, &::LEDPaletteTheme); @@ -41,22 +42,20 @@ void ColormapEffect::max_layers(uint8_t max_) { map_base_ = ::LEDPaletteTheme.reserveThemes(max_layers_); } +void ColormapEffect::onActivate(void) { + last_highest_layer_ = Layer.top(); + ::LEDPaletteTheme.updateHandler(map_base_, last_highest_layer_); +} + void ColormapEffect::update(void) { - for (uint8_t l = 0; l < max_layers_; l++) { - if (!Layer.isOn(l)) - continue; + if (Layer.top() == last_highest_layer_) + return; - ::LEDPaletteTheme.updateHandler(map_base_, l); - } + onActivate(); } void ColormapEffect::refreshAt(byte row, byte col) { - for (uint8_t l = 0; l < max_layers_; l++) { - if (!Layer.isOn(l)) - continue; - - ::LEDPaletteTheme.refreshAt(map_base_, l, row, col); - } + ::LEDPaletteTheme.refreshAt(map_base_, last_highest_layer_, row, col); } bool ColormapEffect::focusHook(const char *command) { diff --git a/src/Kaleidoscope/Colormap.h b/src/Kaleidoscope/Colormap.h index 9709dd47..208cfab4 100644 --- a/src/Kaleidoscope/Colormap.h +++ b/src/Kaleidoscope/Colormap.h @@ -32,10 +32,12 @@ class ColormapEffect : public LEDMode { protected: void setup(void) final; + void onActivate(void) final; void update(void) final; void refreshAt(byte row, byte col) final; private: + static uint8_t last_highest_layer_; static uint8_t max_layers_; static uint16_t map_base_; };