Skip to content

Commit 657259a

Browse files
committed
Optimization: stop to constantly search for the bus
Adding a caching mechanism to the Bus Manager - up to 30% faster especially when many led pins are used.
1 parent 59752ad commit 657259a

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

wled00/bus_manager.cpp

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ void IRAM_ATTR BusDigital::setPixelColor(uint16_t pix, uint32_t c) {
175175
PolyBus::setPixelColor(_busPtr, _iType, pix, c, co);
176176
}
177177

178-
uint32_t BusDigital::getPixelColor(uint16_t pix) {
178+
uint32_t IRAM_ATTR_YN BusDigital::getPixelColor(uint16_t pix) {
179179
if (reversed) pix = _len - pix -1;
180180
else pix += _skip;
181181
uint8_t co = _colorOrderMap.getPixelColorOrder(pix+_start, _colorOrder);
@@ -724,6 +724,10 @@ int BusManager::add(BusConfig &bc) {
724724
} else {
725725
busses[numBusses] = new BusPwm(bc);
726726
}
727+
// WLEDMM clear cached Bus info
728+
lastBus = nullptr;
729+
laststart = 0;
730+
lastend = 0;
727731
return numBusses++;
728732
}
729733

@@ -734,6 +738,10 @@ void BusManager::removeAll() {
734738
while (!canAllShow()) yield();
735739
for (uint8_t i = 0; i < numBusses; i++) delete busses[i];
736740
numBusses = 0;
741+
// WLEDMM clear cached Bus info
742+
lastBus = nullptr;
743+
laststart = 0;
744+
lastend = 0;
737745
}
738746

739747
void BusManager::show() {
@@ -749,11 +757,24 @@ void BusManager::setStatusPixel(uint32_t c) {
749757
}
750758

751759
void IRAM_ATTR BusManager::setPixelColor(uint16_t pix, uint32_t c, int16_t cct) {
760+
if ((pix >= laststart) && (pix < lastend ) && (lastBus != nullptr)) {
761+
// WLEDMM same bus as last time - no need to search again
762+
lastBus->setPixelColor(pix - laststart, c);
763+
return;
764+
}
765+
752766
for (uint_fast8_t i = 0; i < numBusses; i++) { // WLEDMM use fast native types
753767
Bus* b = busses[i];
754768
uint_fast16_t bstart = b->getStart();
755769
if (pix < bstart || pix >= bstart + b->getLength()) continue;
756-
busses[i]->setPixelColor(pix - bstart, c);
770+
else {
771+
// WLEDMM remember last Bus we took
772+
lastBus = b;
773+
laststart = bstart;
774+
lastend = bstart + b->getLength();
775+
b->setPixelColor(pix - bstart, c);
776+
break; // WLEDMM found the right Bus -> so we can stop searching
777+
}
757778
}
758779
}
759780

@@ -772,12 +793,23 @@ void BusManager::setSegmentCCT(int16_t cct, bool allowWBCorrection) {
772793
Bus::setCCT(cct);
773794
}
774795

775-
uint32_t BusManager::getPixelColor(uint_fast16_t pix) { // WLEDMM use fast native types
796+
uint32_t IRAM_ATTR BusManager::getPixelColor(uint_fast16_t pix) { // WLEDMM use fast native types, IRAM_ATTR
797+
if ((pix >= laststart) && (pix < lastend ) && (lastBus != nullptr)) {
798+
// WLEDMM same bus as last time - no need to search again
799+
return lastBus->getPixelColor(pix - laststart);
800+
}
801+
776802
for (uint_fast8_t i = 0; i < numBusses; i++) {
777803
Bus* b = busses[i];
778804
uint_fast16_t bstart = b->getStart();
779805
if (pix < bstart || pix >= bstart + b->getLength()) continue;
780-
return b->getPixelColor(pix - bstart);
806+
else {
807+
// WLEDMM remember last Bus we took
808+
lastBus = b;
809+
laststart = bstart;
810+
lastend = bstart + b->getLength();
811+
return b->getPixelColor(pix - bstart);
812+
}
781813
}
782814
return 0;
783815
}

wled00/bus_manager.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,12 @@ class BusManager {
427427

428428
private:
429429
uint8_t numBusses = 0;
430-
Bus* busses[WLED_MAX_BUSSES+WLED_MIN_VIRTUAL_BUSSES];
430+
Bus* busses[WLED_MAX_BUSSES+WLED_MIN_VIRTUAL_BUSSES] = {nullptr}; // WLEDMM init array
431431
ColorOrderMap colorOrderMap;
432+
// WLEDMM cache last used Bus -> 20% to 30% speedup when using many LED pins
433+
Bus *lastBus = nullptr;
434+
unsigned laststart = 0;
435+
unsigned lastend = 0;
432436

433437
inline uint8_t getNumVirtualBusses() {
434438
int j = 0;

0 commit comments

Comments
 (0)