Understanding luajit

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
rok
Prole
Posts: 36
Joined: Wed Dec 24, 2014 9:12 am
Location: Slovenia

Understanding luajit

Post 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.
padicao2010
Prole
Posts: 15
Joined: Wed Jan 15, 2014 8:38 am

Re: Understanding luajit

Post 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
User avatar
rok
Prole
Posts: 36
Joined: Wed Dec 24, 2014 9:12 am
Location: Slovenia

Re: Understanding luajit

Post 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
User avatar
slime
Solid Snayke
Posts: 3166
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Understanding luajit

Post 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.
User avatar
rok
Prole
Posts: 36
Joined: Wed Dec 24, 2014 9:12 am
Location: Slovenia

Re: Understanding luajit

Post 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?
padicao2010
Prole
Posts: 15
Joined: Wed Jan 15, 2014 8:38 am

Re: Understanding luajit

Post 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
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Understanding luajit

Post 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.

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 9 guests