ProTip - luasrcdiet to reduce compiled bytecode size

Discussion on the Lua Scripting capabilities for RaceCapture/Pro. Also see the <a href="http://autosportlabs.net/RaceCapturePro_Lua_Scripting">Lua Scripting Guide</a>

Moderators: JeffC, stieg

Post Reply
eacmen
Posts: 15
Joined: Fri Dec 30, 2016 7:39 am

ProTip - luasrcdiet to reduce compiled bytecode size

Post by eacmen »

I have been doing a fair amount of lua recently for the RCP. I am an experience programmer but have never had to program lua until a few weeks ago. I got very frustrated when my scripts would exceed the max size allowed by the RCP. I understand why the constraints are there but I still wasn't happy about it.

What I discovered is that the compiled lua code includes variable names and other debug information that can bloat the size of the resulting compiled lua bytecode. I like to use friendly global variable names when declaring things like constants and things of that nature. This practice really bloated the size of the resulting compiled LUA code.

Then I discovered luasrcdiet. This project will automatically shorten variable names and functions if they are declared local. The resulting code is unreadable but as far as getting code to fit on the RCP it has proven very effective.

Basically you run your code through luasrcdiet (making sure your code declares local variables as local) and it will allow you to fit much more code on the RCP than it normally would support.

You can find the code on github here:

https://github.com/LuaDist/luasrcdiet

I noticed as much as a 70% reduction in the size of the compiled bytecode. I hope this helps others.

notso2slo
Posts: 12
Joined: Wed Jan 06, 2016 8:55 pm
Contact:

Post by notso2slo »

That is awesome! Especially since I keep running out of memory.

But as someone who is not a programmer.... How the heck do I actually run my script through that?? I don't even know where to start.

eacmen
Posts: 15
Joined: Fri Dec 30, 2016 7:39 am

Post by eacmen »

Fair warning you will need to be comfortable using the command line to use this tool.

I will be using my mutlicam GoPro script as an example. You can find it here:

https://github.com/eacmen/hpde/blob/mas ... lticam.lua

Notice which variables I marked as local and which ones I didn't. Anything that is contained within your file can be marked local. However functions like onTick cannot since that is the function that the RCP calls and its function name cannot be changed.

Make sure you have lua installed on your machine (apt-get install lua51 on linux, or various download packages available for Windows or OSX).

Download and extract luasrcdiet from git: https://github.com/LuaDist/luasrcdiet/a ... master.zip

Copy your lua script into the luasrcdiet-master directory that was created when you extracted the above file.

Create the "diet" lua file:

Code: Select all

lua LuaSrcDiet.lua gopro_session_multicam.lua -o gopro_session_multicam.diet.lua
You should now be able to take this file and copy and paste it into the RCP interface.

However lets compare the compiled size between the two:

First compile the lua scripts.

Code: Select all

luac -o gopro_session_multicam.lua.out gopro_session_multicam.lua
luac -o gopro_session_multicam.diet.lua.out gopro_session_multicam.diet.lua
Now lets compare their size:

Code: Select all

-rw-r--r-- 1 peter peter 8995 Oct 11 11&#58;35 gopro_session_multicam.diet.lua.out
-rw-r--r-- 1 peter peter 9867 Oct 11 11&#58;21 gopro_session_multicam.lua.out

psfp
Posts: 49
Joined: Mon Aug 21, 2017 10:40 pm
Location: DF - Brazil

Post by psfp »

Sorry, but what are the advantages or disadvantages when comparing it to Lua minifier?

https://mothereff.in/lua-minifier
--Paulo

eacmen
Posts: 15
Joined: Fri Dec 30, 2016 7:39 am

Post by eacmen »

I had better success with luasrcdiet identifying and shortening variable names. Have you had success with luamin?

wcorredor
Posts: 50
Joined: Thu Oct 27, 2016 6:29 pm

Post by wcorredor »

I have been using luaminifier and it works great. Just cut and paste your lua source and cut and paste the result of the bottom window.
It is very simple and saves a lot of memory.

Post Reply