3
votes

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?

2
Precompiling 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
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
You could merge multiple Lua files into one using something like this, 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
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
@lhf: Why that restriction? I thought package.preload would remove it...Deduplicator

2 Answers

0
votes

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
votes

I replaced Lua with LuaJIT and the loading time reduced to ~6sec. I'm satisfied with this result now.Thanks everybody.