Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slower UI when adding lua widgets #4572

Closed
neuhausj opened this issue Sep 26, 2024 · 20 comments
Closed

Slower UI when adding lua widgets #4572

neuhausj opened this issue Sep 26, 2024 · 20 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@neuhausj
Copy link

Hello,
As part of the creation of my F3k template, I made 2 simple widgets to display a status and a nicer battery widget. (available here > https://github.com/neuhausj/Ethos-resources )

However, the whole radio is becoming a lot less responsive with these 6 lua widgets, and for each widget removed, the whole radio is speeding up again.
How can the UI responsiveness be improved? Why the whole system is getting slower? (also the system/model menus)
When loading a new model without any widget, the radio is fast again, so it definetely comes from the lua widgets.

I am using these 2 pages, with each 3 lua widgets on it:
The idea is to keep track of the voltages and radio status easily.

  • Flight view: activated after launch
    image

  • Flight summary: activated after landing
    image

What I tried:

  • checking if the widget is visible or not in the wakeup call but it doesn't seem to help:
local function wakeup(widget)
    if lcd.isVisible() then
        doStuff ...
    end
  • compiling the lua but I didn't see much performance gain
  • trying to only call the refresh function once per second
  • loading completely empty widgets (all code in paint and wakeup commented), and the radio is also getting slower

This was tested on both a X18SE and a X20Pro with latest firmware:
image

Thanks in advance for the support.

@bsongis
Copy link
Contributor

bsongis commented Sep 26, 2024

Do you have any other script than those 2 ones?

@neuhausj
Copy link
Author

No, only the elrs one.
image

I also have a F3K template for 4servos wing where it is even worse (probably due to additional mixes and curves for calibration).
Here is a beta version > F3K 6s template.bin.zip

@bsongis-frsky
Copy link
Collaborator

Please provide a "minimal" ZIP with everything needed to reproduce the problem, I will have a look tomorrow.

@bsongis-frsky bsongis-frsky self-assigned this Sep 26, 2024
@bsongis-frsky bsongis-frsky added the enhancement New feature or request label Sep 26, 2024
@bsongis-frsky bsongis-frsky added this to the 1.5.16 milestone Sep 26, 2024
@neuhausj
Copy link
Author

neuhausj commented Sep 26, 2024

Thanks for your support.
Here a copy from my radio of models + scripts + audio to put in the correct folder.

  • F3K 2s template (2 servo wing) is a model with 6 lua widgets
  • f3k 4s without (2 servo wing) is a copy from F3K 2s template without 4 lua battery widgets
  • F3K 6s template is a more complex model (4 servo wing) with the same widget as F3K 2s template

You should notice the difference in UI speed between the 3 versions.
I tested this on 2 different radios (X18SE and X20Pro).
Let me know if you need further details.

Setup instructions are available on the repo if needed > https://github.com/neuhausj/Ethos-resources

image

Radio extract.zip

@bsongis-frsky
Copy link
Collaborator

Indeed the UI becomes slow. I wonder why there are debug traces such as "wakeup" or "paint".

@neuhausj
Copy link
Author

I tried to optimize my 2 widgets as much as possible yesterday and used some print to check how quickly those were called. Even calling them slower didn’t help.

You can replace the 2 widgets with the one from the repository (they don’t have the print active): https://github.com/neuhausj/Ethos-resources

@bsongis-frsky
Copy link
Collaborator

Are you sure that this compilation chain optimizes speed? I am not so sure. I have removed it here, removed the debug and removed the errors and it is already quicker. I work with the 6S template which you consider as the worst one, right?

@neuhausj
Copy link
Author

I think it helped, yes. The same approach was used for other scripts (elrs) and on EdgeTX as well iirc.

My initial approach was to just use a single main function.

I have it versioned here if you want to check > https://github.com/neuhausj/Ethos-resources/tree/107c7c96de047d1e6dd520ce87a1a36c6e0c3278

@neuhausj
Copy link
Author

I also tried using completely empty widgets (all code in paint and wakeup commented), and the radio was also getting slower.

@bsongis-frsky
Copy link
Collaborator

I think I have found the problem: to allow the Lua scripts to access his resources at any time, Ethos does a chdir into the script directory. It does this on each wakeup, on each paint, etc. Then at the end of all those functions, it chdir into the previous directory (you could be navigating inside System / File Manager ...)

And the problem I just discovered is ... it costs 6ms on each call. So 12ms per wakeup. Plus another 12ms in case you need to paint something. You have 6 widgets ... the UI responsiveness will be killed.

I will find another solution before the next nightlies.

@neuhausj
Copy link
Author

This is very nice hear ! Thank you for the debugging.
I can give a test on the nightlies as well if it can help.

@strgaltdel
Copy link

strgaltdel commented Sep 27, 2024

And the problem I just discovered is ... it costs 6ms on each call. So 12ms per wakeup. Plus another 12ms in case you need to paint something

Wow, seems a solution could really improve overall performance in case you 've several widgets active !

... would have thought that the "global declaration" of the class which is used in every handler could have caused a slighty performance penalty, but not so much as mentioned..

compileStatus = assert(loadfile(config.moduleDir .. "compile.lua"))(config)
myWidget = assert(compile.loadScript(config.moduleDir .. "loadable.lua"))(config, compileStatus)

why not declare
local myWidget = .... ?

@bsongis-frsky
Copy link
Collaborator

The performance gain after my fix seems huge on my X20-PRO. I hope there is no regression with relative paths now!

@bsongis-frsky
Copy link
Collaborator

Please give it a test when the nightlies are out, thanks a lot!

@bsongis-frsky
Copy link
Collaborator

I wonder if it could be the same problem than #4374
After USB is disconnected, the file system is mounted again, and the file cache is different ...

@bsongis-frsky
Copy link
Collaborator

@strgaltdel Don't hesite to give another test with this fix, I would like to close #4374 in the same shot ;)

@neuhausj
Copy link
Author

Hello,
I just tried the latest nightlies and noticed that the performance seems indeed better.
However, when enabling the ELRS script with 5.25Mbs baudrate, the performance degrades significantly again.
When set to 400k (which is fine up to 333Hz full), it is better but still slower.

I also noticed the widget does not behave the same in the nightly vs stable version: in the stable version, the value of an inactive telemetry value (without connection) is 0 vs nil in the nightlies (hence I added a check in the latest version).

And the speed doesn't seem to change when both widgets have an empty paint and wakeup functions.

Also thanks strgaltdel for the tip, I updated both widgets to use a local variable, but it doesn't seem to improve much.

@bsongis-frsky
Copy link
Collaborator

@neuhausj I have just started the nightly. It cannot be fixed in all firmware compiled before this comment has been written :)

@neuhausj
Copy link
Author

My bad !!
I am using the X18SE to test and the firmware was indeed not compiled yet.
I'll wait a bit more and redo my testing. 😉

@neuhausj
Copy link
Author

Just quick update: I tried the latest nightly and the speed is great! You can hardly distinguish this from a model without widgets.
Also no impact from ELRS speed change.
Thanks a lot for this update, 1.5.16 looks promising !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants