Replies: 2 comments 12 replies
-
Some remarks: variable name lengths are relevant in LUA as everything is stored as key-value pair, and a variable name is a key. Item 2 is for me counter intuitive, as a 'sequence' in LUA is just a key-value pair with integers as keys. So creating two arrays with identical integers as references to keys and values, instead of one key-value array, would take more memory according to my way of viewing LUA. |
Beta Was this translation helpful? Give feedback.
-
About compilation: for some strange reason compilation on opentx is compatible with radio compilation, and will also simply work. At least in my case with several hundreds of files to be compiled.... |
Beta Was this translation helpful? Give feedback.
-
Hello guys,
Last month I've spent to research LUA scripts memory optimisation.
For simple print("Hello world") it doesn't matter but once you start to write something complex like my ToolBox or @yaapu Telemetry script you will soon hit memory cap which is roughly around 60kB on BW LCD radios.
There are always trade-offs but if memory usage is priority you have to consider them
So here are few tips and findings:
1. Amount of code matters if you are going to publish non-compiled version (*.lua).
Obvious but still worth mentioning
is more mem-efficient than
TRADE-OFFs: readability
LUA code maintenance is harder
2. Keep associative arrays' keys short
is more mem-efficient than
TRADE-OFFs: readability
3. Functions are mem-expensive
is more mem-efficient than
Whenever possible gather code in bigger one function instead of scattering it among several small ones
4. Clean unused allocated memory with collectgarbage()
Lua uses automatic memory management (garbage collector)
If you declare variable via local statement it will be auto-purged once exiting scope of this variable
But what if you hold variables,functions or other tables in tables?
It's good practice to force garbage collector
5. Garbage collection right way
Well it is well know LUA garbage collection is not top-class :) So
To be sure everything is purged use explicit clear function (recursive) before collectgarbage()
6. Keep code in several files and load it when needed
7. Compile to LUA byte-code *.luac
As compiler is now standard EdgeTX feature you may consider compiling *.lua to *.luac
LUA byte-code is
If you have LUA script in one file it will be auto-compiled once it is run
In case of multiply files compilation can be forced during runtime using loadScript mode argument
Compiler consumes some amount of memory so in case of big LUA app with several files it's good practice to write pre-compilation code that is run before first LUA app usage. (BF LUA script uses this technique)
To manipulate easily loading and compiling process use load-proxy function
So now you can modify globally what happens during load.
etc
main.lua (main script located wherever)
hud.lua (loaded script located in /SCRIPTS/TOOLS/MYSCRIPT/)
TRADE-OFFs
Hope that helps.
PS.
Thanks @yaapu for 5 and @lshems for 6.
If you have any other LUA memory optimisation tips & tricks share it here
Beta Was this translation helpful? Give feedback.
All reactions