« C++: Reseating references and getting address of themOne step forward and two years back »

A way of loading Lua scripts

I've often noticed one thing about games which uses Lua, the fact that they load scripts from text files only and don't support scripts in binary format.

 

Why wouldn't they support the binary scripts? Shouldn't the binary format provide a few benefits over the text files? What about speed? Of course that it should be faster to load the scripts from binary file, since they are already (pre)compiled, so there's no need to parse them again. Isn't that obvious? so why would someone abandon the binary format?

 

By "support", I mean native support for loading binary files instead of text files, so the game should check if binary version exists, check if it's synchronized with the text version, if not, recompile the script, and load it.

 

It's not hard to do it, I've added such feature to OpenArcanum in half an hour. I had to add a custom file header to the binary file, which contains info about the text and binary version (e.g. file times -> last write, access), so I can fastly compare them and recompile the binary version when loading if the text version has changed since last time.

 

I haven't made any benchmarks yet, but I think that there will be some speedup when loading a lof of scripts, especially when I put all the binary files in one script bank.

 

Categories: ArcanumAlive

5 comments

Comment from: radzh [Visitor]
radzhThe thing is Lua compiler's so fast, the difference won't even be noticeable, at least for most typical scenarios.
06/04/11 @ 14:39
Comment from: Crypton [Member]
Are you sure about it's fastness?:) Surely, it's written in ANSI C, but it don't change the fact that it's interpreter needs to parse whole text file, and convert it to executable binary data.

Anyway, I'll do some benchmarks, with a lot of scripts, if I get some noticable speedup.

Will report later :) Bye bye

06/06/11 @ 14:35
Comment from: radzh [Visitor]
radzhIt's single pass translator, straight into p-code. Pre-compilation is faster, sure, it's just - nobody cares.
06/06/11 @ 15:21
Comment from: Crypton [Member]
Still, it's foolish to recompile all scripts everytime game launches :)

Also it's recommended to avoid useless recompilation, and use precompiled scripts instead:

"Although the Lua compiler is quite efficient when compared with compilers for other languages, compilation is a heavy task. So, you should avoid compiling code in your program (e.g., function loadstring) whenever possible. Unless you must run code that is really dynamic, such as code entered by an end user, you seldom need to compile dynamic code."

Source: http://www.lua.org/gems/sample.pdf (Page 4.)

So I think that the games that doesn't use precompiled lua scripts were coded by lazy-lame programmers :)



06/09/11 @ 06:08
Comment from: radzh [Visitor]
radzh> compilation is a heavy task

Yes. And still - nobody cares :)
06/09/11 @ 20:50