Makuna / NeoPixelBus

An Arduino NeoPixel support library supporting a large variety of individually addressable LEDs. Please refer to the Wiki for more details. Please use the GitHub Discussions to ask questions as the GitHub Issues feature is used for bug tracking.
GNU Lesser General Public License v3.0
1.18k stars 264 forks source link

Use Native type for loops that have large interations to save code size #813

Open Makuna opened 4 months ago

Makuna commented 4 months ago

Is your feature request related to a problem? Please describe. WLED developers have found that using a native size type to the uC (ESP32 would be 32 bit) on loops can reduce code size.

Describe the solution you'd like Define a NeoPixelBus wide type like uintLoop_t and intLoop_t (alternate of uintNative_t or others?) that then get used throughout NeoPixelBus. They get defined based on the microcontrollers platforms size. (using int and unsigned int?)

Additional context This seems to be a bigger issue (using smaller size than the native uC size) for larger chips as they then need to inject code on every increment or test to convert a native register to the size the code defined.

blazoncek commented 4 months ago

Perhaps just size_t would do when you only need positive numbers.

EDIT: the issue (but that is generic) is that it is only guaranteed to have 16 bits at least. IMO if you do not need maximum range (i.e. INTxx_MAX) in your loops then it may be best to use native size which is (more often than not) int or unsigned.

blazoncek commented 4 months ago

https://en.cppreference.com/w/cpp/types/integer

softhack007 commented 1 month ago

You could use the "fast" types like uint_fast16_t or int_fast32_t (see list in the link posted by @blazoncek).

Some people consider these make code look "nerdy" however it's the most portable solution. Like uint_fast8_t is "the fastest (native) unsigned that can hold at least 8 bits".