TFT_eSPI icon indicating copy to clipboard operation
TFT_eSPI copied to clipboard

A couple of optimizations/cleanups in TFT_eSPI.cpp

Open B3rn475 opened this issue 2 years ago • 1 comments

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.

B3rn475 avatar Feb 18 '24 12:02 B3rn475

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.

B3rn475 avatar Feb 18 '24 12:02 B3rn475

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?

Bodmer avatar Feb 22 '24 01:02 Bodmer

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.

B3rn475 avatar Feb 22 '24 22:02 B3rn475

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?

B3rn475 avatar Mar 01 '24 21:03 B3rn475

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.

Bodmer avatar Mar 19 '24 12:03 Bodmer

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.

B3rn475 avatar Mar 19 '24 12:03 B3rn475