pixelmatix / SmartMatrix

SmartMatrix Library for Teensy 3, Teensy 4, and ESP32
http://docs.pixelmatix.com/SmartMatrix
611 stars 161 forks source link

Faster integers? #164

Open welshcoder opened 2 years ago

welshcoder commented 2 years ago

Hi,

I thought I'd bring to your attention a potential speed-up of the library that you might be interested in. The branch with the changes I've been testing is available here: https://github.com/welshcoder/SmartMatrix/tree/speed_improvement_check.

The nature of the change is to change certain variables from uintX_t to uint_fastX_t and intX_t to int_fastX_t. I've also changed some ints to be int_fastX_t too. The fast variants ensure that the integers are at least the given size, but the compiler will use the native integer size for the CPU, giving a potential for faster computation. For the Teensy 4, I believe this to be 32-bit integer, so all the fast variants of integers will be 32-bit. I've not done a wholesale change since it is evident that some variables need to be of a fixed size.

I've not done a PR for this because I don't consider it complete (I've only updated the Teensy 4 Hub75 related files, as that coincides with the hardware I have). According to my benchmarks, the speed increase is about 10%, and most of that comes from changing the Layer* classes. If others find that to be the case, then the modification might be more straightforward for a PR as the hardware dependant classes can be left untouched.

I would be interested to know if others get a similar speed increase using the branch cited above and whether it's worth updating the library to include the fast variants of integers. If there's a positive response to this, I'll arrange a PR with the required changes.

Cheers, welshcoder