richonguzman / LoRa_APRS_Tracker

LoRa APRS Tracker with Tx and Rx capabilities, Messages, Wx, Winlink and more...
MIT License
168 stars 45 forks source link

Small main menu refactor #98

Closed LiamMehle closed 1 month ago

LiamMehle commented 1 month ago

A small refactor of the main menu display. An else-if cascade turned into a switch-case. String move constructor no longer called at the end of showOnScreen().

richonguzman commented 1 month ago

Hi! I had this on pause as I wanted to do many updates before merging this, but this is great to learn as (I know now) switch is faster the if in this special cases

LiamMehle commented 1 month ago

No problem! In this case, switch-case probably isn't noticeably faster. I used it because I figured it's a more appropriate than many ifs checking the same variable, but it works much the same. If there's anything you'd like me to change, just let me know.

richonguzman commented 1 month ago

Hi, I added and tried all your proposal and compared byte by byte how it worked

strangely switch adds more size than if validation

so i tested them and decided to have less error validations and just one IF is enough

all other were added manually just to learn and test

PD: some PL ham op told me you were working on some special feature about search and rescue or emergencies and I want to participate!!!!

richonguzman commented 1 month ago

all of this was manually compared and added on certain parts, nice ideas!

LiamMehle commented 1 month ago

Greetings, apologies for the late response. Swapping out the if-elses with switch-case was mainly aimed at code readability, since switch case is for enumerating values a single variable or expression can have, where if takes an arbitrary expression. In other words: switch-case is a special case of many if-elses. I've also included a default branch, at least for printing the currently selected frequency which almost certainly adds more code size. Default is a special case that is taken if no other case is selected before it, meaning it's the equivalent of adding an extra if or else at the end and the code that goes in there.

When comparing against contiguous values, switch case can be optimized to a table lookup. GCC is even clever enough to optimize some switch-cases to a mathematical expression! https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1DIApACYAQuYukl9ZATwDKjdAGFUtAK4sGISQDspK4AMngMmAByPgBGmMQgZgDMpAAOqAqETgwe3r7%2BQemZjgJhEdEscQnJtpj2JQxCBEzEBLk%2BfoG19dlNLQRlUbHxiSkKza3t%2BV3j/YMVVaMAlLaoXsTI7BzmSeHI3lgA1CZJbsjjxOH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spect the assembly generated by compilers for many different languages. The link is for the code in the attachment compiled with Xtensa ESP32 GCC with -O3 flag, though -O2 also works.

Warm regards, S56IM

On Tue, May 14, 2024 at 12:30 AM Ricardo Guzman (Richonguzman) < @.***> wrote:

Hi, I added and tried all your proposal and compared byte by byte how it worked

strangely switch adds more size than if validation

so i tested them and decided to have less error validations and just one IF is enough

all other were added manually just to learn and test

PD: some PL ham op told me you were working on some special feature about search and rescue or emergencies and I want to participate!!!!

— Reply to this email directly, view it on GitHub https://github.com/richonguzman/LoRa_APRS_Tracker/pull/98#issuecomment-2108910536, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMHLRJLX3MQRIJYWF7IDC63ZCE5HBAVCNFSM6AAAAABHSE6UK2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMBYHEYTANJTGY . You are receiving this because you authored the thread.Message ID: @.***>