I know how to load a Lua file via luaL_loadbuffer
. Now I have many Lua files, more than 100. I am thinking about how to speed up the loading process. One way I figured out is: put all files into one, and then load this file using luaL_loadbuffer
(I did some tests, but just got syntax error return by luaL_loadbuffer
). Does anyone ever use this method? Or is there any other way to speed up the loading?
Asked
Active
Viewed 742 times
4

hjpotter92
- 78,589
- 36
- 144
- 183

gzyuan888
- 93
- 1
- 6
-
4Precompiling with `luac` should speed loading. You can also combine many files into one precompiled file with `luac`, provided you don't use `require` to load your files. – lhf Jul 01 '14 at 01:15
-
I have looked into luac and done some practice. Maybe it can solve the speed problem but it brings another one:portablity. According to luac Document, The binary files created by luac are portable only among architectures with the same word size and byte order. I am developing a mobile game. Portability is important to such app. – gzyuan888 Jul 01 '14 at 06:07
-
You could merge multiple Lua files into one using something like [this](https://github.com/siffiejoe/lua-amalg), and provide precompiled versions for the most common architectures via preprocessor defines with the amalgamated source code as a fallback for the rest. I doubt that combining Lua files alone would make much difference in loading speed. – siffiejoe Jul 01 '14 at 07:09
-
Precompiled versions did reduce the loading time a lot. I test to load the precompiled files generated by a 32bit win7 PC on my android phone(HTC G7). The time reduced to 9sec.Before this,it took more than 30sec. But my phone failed to load precompiled files generated by a 64bit win7 PC(the os is 64bit). Now i am interesting in how to generate portable precompiled files. – gzyuan888 Jul 01 '14 at 07:58
-
@lhf: Why that restriction? I thought `package.preload` would remove it... – Deduplicator Jul 01 '14 at 10:54
-
@Deduplicator, yes, see my [luac.lua](http://www.tecgraf.puc-rio.br/~lhf/ftp/lua/#luac.lua) (which needs updating to 5.2 and 64-bit platforms). See also http://lua-users.org/lists/lua-l/2013-10/msg00746.html for other solutions. – lhf Jul 01 '14 at 11:20
-
@lhf Neither the direct nor the indirect link to the announcement work. The code is still there though... – Deduplicator Jul 01 '14 at 11:29
-
@Deduplicator, the mailing list archive is being rebuilt. The links should work in an hour or so. – lhf Jul 01 '14 at 11:30
-
Thanks,everyone. Regarding portability issue, it seems LuaJIT is good choice for precompiled script(I learned this after searching stackoverflow). I will try to find out if i need to turn to LuaJIT.any comment about portability of LuaJIT? – gzyuan888 Jul 01 '14 at 13:09
-
LuaJIT code is portable across all supported platforms, but only for the same version of LuaJIT. It's likely that versions of LuaJIT will refuse load bytecode from other versions. Note that you won't be able to mix Lua and LuaJIT bytecode as they are incompatible. – Paul Kulchenko Jul 01 '14 at 14:48
2 Answers
0
Expanding on @siffiejoe's comment and this answer to a related SO question, I use Squish to collapse multiple modules into a single .lua
file. You can then use luac
to compile it into bytecode, if desired.
0
I replaced Lua with LuaJIT and the loading time reduced to ~6sec. I'm satisfied with this result now.Thanks everybody.

gzyuan888
- 93
- 1
- 6