Page 1 of 1

Understanding luajit

Posted: Fri Oct 30, 2015 8:38 am
by rok
I'm taking a look at luajit. I'd like to learn how to properly use it to speed up my code. I know how to use its FFI extension but I'd like to know how to work with its bytecode exporter.

After I get the object file with

Code: Select all

luajit -b some_library.lua some_library.o
for example, it says I should link it (http://luajit.org/running.html). How do I do that?

Are there any good guides on the workflow with luajit that anyone can recommend? Stuff seems to be scattered all around.

Re: Understanding luajit

Posted: Fri Oct 30, 2015 11:25 am
by padicao2010
Two directories, A and B.

Code: Select all

luajit -b A/some.lua B/some.lua
Following is the same:

Code: Select all

require("some")
Run with different directory. Before

Code: Select all

love A
Now

Code: Select all

love B

Re: Understanding luajit

Posted: Fri Oct 30, 2015 12:21 pm
by rok
Why would you export a lua file? Is that even possible?
The output file type is auto-detected from the extension of the output file name:

c — C source file, exported bytecode data.
h — C header file, static bytecode data.
obj or o — Object file, exported bytecode data (OS- and architecture-specific).
raw or any other extension — Raw bytecode file (portable).
from http://luajit.org/running.html

Re: Understanding luajit

Posted: Fri Oct 30, 2015 6:44 pm
by slime
LuaJIT is a JIT compiler (just-in time compiler). That means it doesn't do any compilation/optimizations to your code until it's already running.
Exporting code to a LuaJIT bytecode file just makes it marginally easier for LuaJIT to initially load the code, it won't speed it up any more than it already does for plain Lua code - all of the performance-related aspects of LuaJIT happen automatically at runtime even with plain Lua source code.

Re: Understanding luajit

Posted: Fri Oct 30, 2015 7:42 pm
by rok
I knew what a JIT compiler is. Was just wondering about the bytecode. Thank you :awesome:

So the only only simple speedup I can do apart from running my code with LuaJIT is to use its FFI interface when possible and replace some of the stuff with C types right?

Re: Understanding luajit

Posted: Sun Nov 01, 2015 3:37 am
by padicao2010
Not compile to a lua file, but compile to Raw bytecode file and can be used as a lua file.
raw or any other extension — Raw bytecode file (portable).
Named without .lua, OK.

.c : A c source file, can be open with text editor. The bytecode is shown as a char *. Maybe used in C.
.h : Almost the same as .c, but used as a c header file.
.o, .obj : No clue. I can't run it with luajit.

The advantages of using bytecode compare to source file:
1. no string parse
2. protect source code to a certain extent

Re: Understanding luajit

Posted: Sun Nov 01, 2015 5:10 pm
by s-ol
padicao2010 wrote:Not compile to a lua file, but compile to Raw bytecode file and can be used as a lua file.
raw or any other extension — Raw bytecode file (portable).
Named without .lua, OK.

.c : A c source file, can be open with text editor. The bytecode is shown as a char *. Maybe used in C.
.h : Almost the same as .c, but used as a c header file.
.o, .obj : No clue. I can't run it with luajit.

The advantages of using bytecode compare to source file:
1. no string parse
2. protect source code to a certain extent
.o is basically a "partial executable" (OS dependent, native) that you can link to when compiling another file to include the lua module so it can be require'd if I understand the docs correctly.