A couple of optimizations/cleanups in TFT_eSPI.cpp
A couple of optimizations.
- Avoid some checks if previous ones guarantee that they will be false.
- Merge consecutive checks which have the same effect.
- Store some temporary results, instead of computing them over and over.
Note that a lot of this code could be simplified by using std::min and std::max.
std::clamp could also be used in various places, but that would be limited to C++17 and above. On the other hand a local clamp helper could be introduced too.
I suspect these changes have no effect because the compiler will pre-calculate varaibles for inside a loop and minimise comparisons. This means that trivial re-jigging of code is of no benefit.
What code/varaible size and performance difference did you get?
I've reverted a couple of them, but for the ones that remain:
- I see a reduction of the size of the binary (a few bytes),
- I see differences in the generated asm (jumps are rearrenged to avoid running the checks in the
else ifs), - I think that some actually improve readability.
I suspect these changes have no effect because the compiler will pre-calculate varaibles for inside a loop and minimise comparisons. This means that trivial re-jigging of code is of no benefit.
What code/varaible size and performance difference did you get?
Is the above comment enough? or should I provide additional data?
I tried the changes on an RP2040 and it made no difference to binary so I would rather leave the code as is. I expect there are areas where optimisation will give benefits.
Which example have you tested it against?
The improvement is visible only in examples that use setTextFont, in the others the whole function is optimized away.