Skip to content

Commit 346914b

Browse files
committed
setPixelColorXY small optimizations
Caching a few segment vars gives a 5% speedup on big setups. Its 2024, and compilers are still stupid :-P
1 parent 982a4bb commit 346914b

1 file changed

Lines changed: 23 additions & 15 deletions

File tree

wled00/FX_2Dfcn.cpp

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,10 @@ uint32_t WS2812FX::getPixelColorXY(uint16_t x, uint16_t y) {
214214
void IRAM_ATTR_YN Segment::setPixelColorXY(int x, int y, uint32_t col) //WLEDMM: IRAM_ATTR conditionally
215215
{
216216
if (Segment::maxHeight==1) return; // not a matrix set-up
217-
if (x<0 || y<0 || x >= virtualWidth() || y >= virtualHeight()) return; // if pixel would fall out of virtual segment just exit
217+
const int_fast16_t cols = virtualWidth(); // WLEDMM optimization
218+
const int_fast16_t rows = virtualHeight();
219+
220+
if (x<0 || y<0 || x >= cols || y >= rows) return; // if pixel would fall out of virtual segment just exit
218221

219222
unsigned i = UINT_MAX;
220223
bool sameColor = false;
@@ -231,11 +234,11 @@ void IRAM_ATTR_YN Segment::setPixelColorXY(int x, int y, uint32_t col) //WLEDMM:
231234
}
232235

233236
#if 0 // this is a dangerous optimization
234-
if ((i < UINT_MAX) && sameColor && (call > 0) && (ledsrgb[i] == CRGB(col)) && (_globalLeds == nullptr)) return; // WLEDMM looks like nothing to do (but we don't trust globalleds)
237+
if ((i < UINT_MAX) && sameColor && (call > 0) && (!transitional) && (ledsrgb[i] == CRGB(col)) && (_globalLeds == nullptr)) return; // WLEDMM looks like nothing to do (but we don't trust globalleds)
235238
#endif
236239

237-
if (reverse ) x = virtualWidth() - x - 1;
238-
if (reverse_y) y = virtualHeight() - y - 1;
240+
if (reverse ) x = cols - x - 1;
241+
if (reverse_y) y = rows - y - 1;
239242
if (transpose) { uint16_t t = x; x = y; y = t; } // swap X & Y if segment transposed
240243

241244
// WLEDMM shortcut when no grouping/spacing used
@@ -245,27 +248,32 @@ void IRAM_ATTR_YN Segment::setPixelColorXY(int x, int y, uint32_t col) //WLEDMM:
245248
return;
246249
}
247250

248-
x *= groupLength(); // expand to physical pixels
249-
y *= groupLength(); // expand to physical pixels
250-
if (x >= width() || y >= height()) return; // if pixel would fall out of segment just exit
251+
const int_fast16_t glen_ = groupLength(); // WLEDMM optimization
252+
const int_fast16_t wid_ = width();
253+
const int_fast16_t hei_ = height();
254+
255+
x *= glen_; // expand to physical pixels
256+
y *= glen_; // expand to physical pixels
257+
if (x >= wid_ || y >= hei_) return; // if pixel would fall out of segment just exit
251258

252-
for (int j = 0; j < grouping; j++) { // groupping vertically
253-
for (int g = 0; g < grouping; g++) { // groupping horizontally
259+
const int grp_ = grouping; // WLEDMM optimization
260+
for (int j = 0; j < grp_; j++) { // groupping vertically
261+
for (int g = 0; g < grp_; g++) { // groupping horizontally
254262
uint_fast16_t xX = (x+g), yY = (y+j); //WLEDMM: use fast types
255-
if (xX >= width() || yY >= height()) continue; // we have reached one dimension's end
263+
if (xX >= wid_ || yY >= hei_) continue; // we have reached one dimension's end
256264

257265
strip.setPixelColorXY(start + xX, startY + yY, col);
258266

259267
if (mirror) { //set the corresponding horizontally mirrored pixel
260-
if (transpose) strip.setPixelColorXY(start + xX, startY + height() - yY - 1, col);
261-
else strip.setPixelColorXY(start + width() - xX - 1, startY + yY, col);
268+
if (transpose) strip.setPixelColorXY(start + xX, startY + hei_ - yY - 1, col);
269+
else strip.setPixelColorXY(start + wid_ - xX - 1, startY + yY, col);
262270
}
263271
if (mirror_y) { //set the corresponding vertically mirrored pixel
264-
if (transpose) strip.setPixelColorXY(start + width() - xX - 1, startY + yY, col);
265-
else strip.setPixelColorXY(start + xX, startY + height() - yY - 1, col);
272+
if (transpose) strip.setPixelColorXY(start + wid_ - xX - 1, startY + yY, col);
273+
else strip.setPixelColorXY(start + xX, startY + hei_ - yY - 1, col);
266274
}
267275
if (mirror_y && mirror) { //set the corresponding vertically AND horizontally mirrored pixel
268-
strip.setPixelColorXY(width() - xX - 1, height() - yY - 1, col);
276+
strip.setPixelColorXY(wid_ - xX - 1, hei_ - yY - 1, col);
269277
}
270278
}
271279
}

0 commit comments

Comments
 (0)