const const const

adding hints for the compiler for optimization.
In case your custom build complains about "const", just remove the keyword. based on e82f38e277, but going further :-)

* "const" class functions : function does not modify any class attributes ( --> "this" becomes const)

* __attribute__((pure)) :  function return value depends only on the parameters and/or global variables. The function does not modify any global or static variables.
* __attribute__((const)) : function only examines arguments (no globals), and has no effects except the return value. This slightly more strict than "pure"
* hot: tells the compiler "this functions is called very often"
* cold: the opposite of hot
This commit is contained in:
Frank
2024-08-07 14:58:38 +02:00
parent 273154db76
commit 7f9da309c9
14 changed files with 148 additions and 145 deletions

View File

@@ -882,7 +882,7 @@ uint16_t Segment::virtualLength() const {
}
//WLEDMM used for M12_sBlock
void xyFromBlock(uint16_t &x,uint16_t &y, uint16_t i, uint16_t vW, uint16_t vH, uint16_t vStrip) {
static void xyFromBlock(uint16_t &x,uint16_t &y, uint16_t i, uint16_t vW, uint16_t vH, uint16_t vStrip) {
float i2;
if (i<=SEGLEN*0.25) { //top, left to right
i2 = i/(SEGLEN*0.25);
@@ -907,7 +907,7 @@ void xyFromBlock(uint16_t &x,uint16_t &y, uint16_t i, uint16_t vW, uint16_t vH,
}
void IRAM_ATTR_YN Segment::setPixelColor(int i, uint32_t col) //WLEDMM: IRAM_ATTR conditionally
void IRAM_ATTR_YN __attribute__((hot)) Segment::setPixelColor(int i, uint32_t col) //WLEDMM: IRAM_ATTR conditionally
{
if (!isActive()) return; // not active
#ifndef WLED_DISABLE_2D
@@ -1171,7 +1171,7 @@ void Segment::setPixelColor(float i, uint32_t col, bool aa)
}
}
uint32_t Segment::getPixelColor(int i)
uint32_t __attribute__((hot)) Segment::getPixelColor(int i) const
{
if (!isActive()) return 0; // not active
#ifndef WLED_DISABLE_2D
@@ -1345,7 +1345,7 @@ void Segment::refreshLightCapabilities() {
/*
* Fills segment with color - WLEDMM using faster sPC if possible
*/
void Segment::fill(uint32_t c) {
void __attribute__((hot)) Segment::fill(uint32_t c) {
if (!isActive()) return; // not active
#if 0 && defined(WLED_ENABLE_HUB75MATRIX) && defined(WLEDMM_FASTPATH)
@@ -1457,7 +1457,7 @@ void Segment::fade_out(uint8_t rate) {
}
// fades all pixels to black using nscale8()
void Segment::fadeToBlackBy(uint8_t fadeBy) {
void __attribute__((hot)) Segment::fadeToBlackBy(uint8_t fadeBy) {
if (!isActive() || fadeBy == 0) return; // optimization - no scaling to apply
const uint_fast16_t cols = is2D() ? virtualWidth() : virtualLength(); // WLEDMM use fast int types
const uint_fast16_t rows = virtualHeight(); // will be 1 for 1D
@@ -1480,7 +1480,7 @@ void Segment::fadeToBlackBy(uint8_t fadeBy) {
/*
* blurs segment content, source: FastLED colorutils.cpp
*/
void Segment::blur(uint8_t blur_amount, bool smear) {
void __attribute__((hot)) Segment::blur(uint8_t blur_amount, bool smear) {
if (!isActive() || blur_amount == 0) return; // optimization: 0 means "don't blur"
#ifndef WLED_DISABLE_2D
if (is2D()) {
@@ -1541,7 +1541,7 @@ uint32_t Segment::color_wheel(uint8_t pos) {
/*
* Returns a new, random wheel index with a minimum distance of 42 from pos.
*/
uint8_t Segment::get_random_wheel_index(uint8_t pos) { // WLEDMM use fast int types, use native min/max
uint8_t Segment::get_random_wheel_index(uint8_t pos) const { // WLEDMM use fast int types, use native min/max
uint_fast8_t r = 0, x = 0, y = 0, d = 0;
while(d < 42) {
@@ -1562,7 +1562,7 @@ uint8_t Segment::get_random_wheel_index(uint8_t pos) { // WLEDMM use fast int ty
* @param pbri Value to scale the brightness of the returned color by. Default is 255. (no scaling)
* @returns Single color from palette
*/
uint32_t Segment::color_from_palette(uint_fast16_t i, bool mapping, bool wrap, uint8_t mcol, uint8_t pbri) // WLEDMM use fast int types
uint32_t __attribute__((hot)) Segment::color_from_palette(uint_fast16_t i, bool mapping, bool wrap, uint8_t mcol, uint8_t pbri) // WLEDMM use fast int types
{
// default palette or no RGB support on segment
if ((palette == 0 && mcol < NUM_COLORS) || !_isRGB) {
@@ -1582,7 +1582,7 @@ uint32_t Segment::color_from_palette(uint_fast16_t i, bool mapping, bool wrap, u
}
//WLEDMM netmindz ar palette
uint8_t * Segment::getAudioPalette(int pal) {
uint8_t * Segment::getAudioPalette(int pal) const {
// https://forum.makerforums.info/t/hi-is-it-possible-to-define-a-gradient-palette-at-runtime-the-define-gradient-palette-uses-the/63339
um_data_t *um_data;
@@ -1891,7 +1891,7 @@ void IRAM_ATTR WS2812FX::setPixelColor(int i, uint32_t col)
busses.setPixelColor(i, col);
}
uint32_t WS2812FX::getPixelColor(uint_fast16_t i) // WLEDMM fast int types
uint32_t WS2812FX::getPixelColor(uint_fast16_t i) const // WLEDMM fast int types
{
if (i < customMappingSize) i = customMappingTable[i];
if (i >= _length) return 0;
@@ -2029,7 +2029,7 @@ void WS2812FX::show(void) {
* Returns a true value if any of the strips are still being updated.
* On some hardware (ESP32), strip updates are done asynchronously.
*/
bool WS2812FX::isUpdating() {
bool WS2812FX::isUpdating() const {
return !busses.canAllShow();
}
@@ -2037,7 +2037,7 @@ bool WS2812FX::isUpdating() {
* Returns the refresh rate of the LED strip. Useful for finding out whether a given setup is fast enough.
* Only updates on show() or is set to 0 fps if last show is more than 2 secs ago, so accuracy varies
*/
uint16_t WS2812FX::getFps() {
uint16_t WS2812FX::getFps() const {
if (millis() - _lastShow > 2000) return 0;
#ifdef ARDUINO_ARCH_ESP32
return ((_cumulativeFps500 + 250) / 500); // +250 for proper rounding
@@ -2128,14 +2128,14 @@ void WS2812FX::setMainSegmentId(uint8_t n) {
return;
}
uint8_t WS2812FX::getLastActiveSegmentId(void) {
uint8_t WS2812FX::getLastActiveSegmentId(void) const {
for (size_t i = _segments.size() -1; i > 0; i--) {
if (_segments[i].isActive()) return i;
}
return 0;
}
uint8_t WS2812FX::getActiveSegmentsNum(void) {
uint8_t WS2812FX::getActiveSegmentsNum(void) const {
uint8_t c = 0;
for (size_t i = 0; i < _segments.size(); i++) {
if (_segments[i].isActive()) c++;
@@ -2143,13 +2143,13 @@ uint8_t WS2812FX::getActiveSegmentsNum(void) {
return c;
}
uint16_t WS2812FX::getLengthTotal(void) { // WLEDMM fast int types
uint16_t WS2812FX::getLengthTotal(void) const { // WLEDMM fast int types
uint_fast16_t len = Segment::maxWidth * Segment::maxHeight; // will be _length for 1D (see finalizeInit()) but should cover whole matrix for 2D
if (isMatrix && _length > len) len = _length; // for 2D with trailing strip
return len;
}
uint16_t WS2812FX::getLengthPhysical(void) { // WLEDMM fast int types
uint16_t WS2812FX::getLengthPhysical(void) const { // WLEDMM fast int types
uint_fast16_t len = 0;
for (unsigned b = 0; b < busses.getNumBusses(); b++) { // WLEDMM use native (fast) types
Bus *bus = busses.getBus(b);
@@ -2162,7 +2162,7 @@ uint16_t WS2812FX::getLengthPhysical(void) { // WLEDMM fast int types
//used for JSON API info.leds.rgbw. Little practical use, deprecate with info.leds.rgbw.
//returns if there is an RGBW bus (supports RGB and White, not only white)
//not influenced by auto-white mode, also true if white slider does not affect output white channel
bool WS2812FX::hasRGBWBus(void) {
bool WS2812FX::hasRGBWBus(void) const {
for (unsigned b = 0; b < busses.getNumBusses(); b++) { // WLEDMM use native (fast) types
Bus *bus = busses.getBus(b);
if (bus == nullptr || bus->getLength()==0) break;
@@ -2171,7 +2171,7 @@ bool WS2812FX::hasRGBWBus(void) {
return false;
}
bool WS2812FX::hasCCTBus(void) {
bool WS2812FX::hasCCTBus(void) const {
if (cctFromRgb && !correctWB) return false;
for (unsigned b = 0; b < busses.getNumBusses(); b++) { // WLEDMM use native (fast) types
Bus *bus = busses.getBus(b);