RegisterSearchFAQMemberlistUsergroupsLog in
Reply to topic Page 1 of 1
ProTip - luasrcdiet to reduce compiled bytecode size
Author Message
Reply with quote
Post ProTip - luasrcdiet to reduce compiled bytecode size 
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.

View user's profile Send private message
Reply with quote
Post  
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.

View user's profile Send private message Visit poster's website
Reply with quote
Post  
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/master/rcp-lua/gopro_session_multicam.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/archive/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:
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:

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:

-rw-r--r-- 1 peter peter 8995 Oct 11 11:35 gopro_session_multicam.diet.lua.out
-rw-r--r-- 1 peter peter 9867 Oct 11 11:21 gopro_session_multicam.lua.out


View user's profile Send private message
Reply with quote
Post  
Sorry, but what are the advantages or disadvantages when comparing it to Lua minifier?

https://mothereff.in/lua-minifier


_________________
--Paulo
View user's profile Send private message
Reply with quote
Post  
I had better success with luasrcdiet identifying and shortening variable names. Have you had success with luamin?

View user's profile Send private message
Reply with quote
Post  
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.

View user's profile Send private message
Display posts from previous:
Reply to topic Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum