Question about LuaJIT and FFI

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Question about LuaJIT and FFI

Post by yetneverdone »

Hey, so after a while here in the love2d community, ive encountered alot of topics/mentions about the luajit, but i didnt put much thought in it. So after finally being super curious about luajit, ive stumbled upon the FFI library. Which, correct me if im wrong, is binding c/c++(?) to lua.

My questions are:
1. Explain briefly the involvement of luajit to LOVE in particular.
2. Can we use that FFI in love? Please provide a simple code about how so.
3. Will using FFI make gamedev in love a lot easier and faster?

Thanks.
User avatar
RaycatRakittra
Prole
Posts: 22
Joined: Fri Sep 30, 2016 12:40 am
Location: Chicago, IL
Contact:

Re: Question about LuaJIT and FFI

Post by RaycatRakittra »

I am not an expert on the subject but this is my takeaway from LuaJIT, FFI, and company:

1) LuaJIT is a just-in-time compiler that only works with Lua 5.1. It makes LOVE two to four times faster at the cost of never upgrading to Lua 5.2+. Does that matter to you? Probs not; Lua is weird that way. However, it gives you access to Foreign Function Interface or FFI.

2) Yes, uhh... Stolen from LuaJIT.org:

Code: Select all

local ffi = require('ffi')
ffi.cdef[[
int printf(const char *fmt, ...);
]]
ffi.C.printf("Hello %s!", "world")
Looks like they require it, expose the library as a variable, define a function, and FFI exposes that function on a generic C variable. (I think including Lua in C does something similar with the _L object.)

3) No, it just provides a nice interface for those rascally C/C++ libs you don't wanna port over. There are some performance bumps to be had, though (i.e. using a FFI C array vs a table for heavy computations).
Sometimes, I can code things.
User avatar
yetneverdone
Party member
Posts: 446
Joined: Sat Sep 24, 2016 11:20 am
Contact:

Re: Question about LuaJIT and FFI

Post by yetneverdone »

RaycatRakittra wrote: Wed May 31, 2017 3:55 pm I am not an expert on the subject but this is my takeaway from LuaJIT, FFI, and company:

1) LuaJIT is a just-in-time compiler that only works with Lua 5.1. It makes LOVE two to four times faster at the cost of never upgrading to Lua 5.2+. Does that matter to you? Probs not; Lua is weird that way. However, it gives you access to Foreign Function Interface or FFI.

2) Yes, uhh... Stolen from LuaJIT.org:

Code: Select all

local ffi = require('ffi')
ffi.cdef[[
int printf(const char *fmt, ...);
]]
ffi.C.printf("Hello %s!", "world")
Looks like they require it, expose the library as a variable, define a function, and FFI exposes that function on a generic C variable. (I think including Lua in C does something similar with the _L object.)

3) No, it just provides a nice interface for those rascally C/C++ libs you don't wanna port over. There are some performance bumps to be had, though (i.e. using a FFI C array vs a table for heavy computations).
Thanks! So basically, i can use ffi. Hooray haha
MasterLee
Party member
Posts: 141
Joined: Tue Mar 07, 2017 4:03 pm
Contact:

Re: Question about LuaJIT and FFI

Post by MasterLee »

I have the following code:

Code: Select all

      local ptr=ffi.cast('unsigned char*',data:getPointer())
      for y=0,height-1 do
        for x=0,width-1 do
          local c=cells[y][x]
          ptr[0]=(c.n%16)*16
          ptr=ptr+1
          ptr[0]=math.floor(c.n/16)*16
          ptr=ptr+1
          ptr[0]=c.pal*8
          ptr=ptr+1
          ptr[0]=(c.xflip and 128 or 0)+(c.yflip and 64 or 0)+(c.dflip and 32 or 0)+1
          ptr=ptr+1
        end
      end
I did the ffi way only, because this solutions came fastest to my mind and that part was not time critical so i did it.
But now i wonder about how much it would be optimized by JIT.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Question about LuaJIT and FFI

Post by kikito »

If you want to climb the performance mountains, you must wear measuring boots.

In other words, if you are thinking about performance, you have to write and run performance tests, and compare how different solutions really perform. Human intuition is notably fallible on this particular task.
When I write def I mean function.
User avatar
zorg
Party member
Posts: 3436
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Question about LuaJIT and FFI

Post by zorg »

And even if you heard about things like cache misses, and how to mitigate them (struct of arrays vs. array of structs), you still need to understand that programming languages are dissimilar to each other; maybe lua itself can not be used in such ways (because of tables being generic containers) but C structs/arrays via FFI can, probably; grain of salt, i don't really know either if this particular thing can be used or not here.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Question about LuaJIT and FFI

Post by raidho36 »

Also, LuaJIT alone, without any JIT compilation, is faster than vanilla Lua. Jit compilation improves performance to anywhere between 10 and 200 times, and brings it on par with C if you don't use Lua tables.
SPlice
Prole
Posts: 48
Joined: Thu Jul 28, 2011 8:53 am

Re: Question about LuaJIT and FFI

Post by SPlice »

you can do a lot of cool stuff with FFI if you are willing to create c libraries as .dll files
User avatar
Xii
Party member
Posts: 137
Joined: Thu Aug 13, 2020 9:09 pm
Contact:

Re: Question about LuaJIT and FFI

Post by Xii »

kikito wrote: Wed May 31, 2017 8:56 pm If you want to climb the performance mountains, you must wear measuring boots.

In other words, if you are thinking about performance, you have to write and run performance tests, and compare how different solutions really perform. Human intuition is notably fallible on this particular task.
I know this is an old thread, but I wanted to chip in my results.

A simple test of 3000 objects with linear integration of movement (direction and speed) runs at 60 fps on my machine.
The same test, with the objects placed in an FFI-allocated array/struct, is actually slower to iterate through, only managing 2000 at 60 fps.
The only upside is less memory use, since not all members necessitate a 64-bit variable.
User avatar
slime
Solid Snayke
Posts: 3131
Joined: Mon Aug 23, 2010 6:45 am
Location: Nova Scotia, Canada
Contact:

Re: Question about LuaJIT and FFI

Post by slime »

One potential cause is some bit of code in your loop might be preventing the loop from being JIT compiled. Another potential cause is if the values in the FFI array are not stored as doubles, LuaJIT will have to convert between the storage format and double, which can hurt performance in an inner loop. LuaJIT provides some tools to help identify those sort of things, although they aren't necessarily trivial to use with love.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 44 guests