r/lua • • 9d ago

Discussion Has anyone ever used luajit before?

I'm a C++ dev interested in lua and found out about lua jit as I was researching a way to substitute for python and bash scripting while also being easily embeddable.

Mainly looking at ffi.cdef for interoperability with C code for low level stuff. Has anyone tried it out?

13 Upvotes

11 comments sorted by

8

u/unburdened_swallow 9d ago edited 8d ago

Yes it is one of the most common lua implementations but ngl I like to just use the C api with all of them instead of the ffi thing. Or mlua for rust. If you are embedding lua in your program you can expose c/rust functions to lua that way, or you could compile them to .so and create c or rust modules you can require that way.

But for hooking up to external C apis the ffi stuff is quite good and less code than making an actual compiled module to add the bindings instead often.

I just don’t mind the C api or mlua at all and often don’t mind making the module so that I can use it across lua versions easier (although you still do sometimes need to check the version when doing stuff still)

5

u/busyHighwayFred 9d ago

Yeah its really great, just keep in mind to avoid the ffi sandwich issue:

> The LuaJIT FFI sandwich issue occurs when LuaJIT code calls a C function via FFI, and that C function subsequently invokes a Lua callback (creating a Lua -> C -> Lua call stack)

3

u/cinnamonjune 9d ago

To add what others have said about the FFI, be advised that the FFI expects a C def. If you have C++ classes in your types, then you'll need to provide a C struct in the FFI's cdef that has the same memory layout as the class.

1

u/infrahazi 6d ago

Emphasizing this- more or less would be my comment as one who enjoys C and C++ calls from my Lua modules.

3

u/c0gster 8d ago

Even in general luaJIT is extremely fast. No other interperter comes close. It has the advantage of being able to mispredict. It detects sections of code that are ran often, and in a split second compiles them to machine code that is used instead. It's more advanced than just a basic JIT as it also analyzes how the code runs and performs optimizations that could potentially cause errors if the prediction is wrong. Why? Because these predictions about the code allow for far better optimizations. If its wrong, luajit immediately switches back to the standard interpreted code, so the program never messes up.

From what I know, it can sometimes be faster than C in math and logic heavy code. But ofc due to garbage collection and auto memory, c is faster in memory bound code. (I may be wrong on all this don't quote me)

As for FFI stuff, idk. I just wanted to comment on its speed. It is for sure so much faster than python. However, lua doesn't have built in multithreading, so you will need to get some sort of external library if you want it, such as LuaLanes. But idk if it supports luajit.

2

u/smtp_pro 9d ago

LuaJIT is pretty sweet but nowadays I just write regular bindings in C.

When you use ffi - you're usually copying parts of header files into Lua. Which can be ok, but sometimes you'll have a library that does crazy things with macros, deeply-nested structures, or a change to a structure definition that breaks things - whereas with the C API, those kinds of issues aren't so bad.

2

u/catbrane lua-vips developer 8d ago

I used it for a library binding, it works really well, in my experience.

Having the whole binding as pure lua makes distribution and updates really easy, and performance is stellar.

The thing I did, fwiw: https://github.com/libvips/lua-vips

1

u/hawhill 9d ago

KOReader makes heavy use of LuaJIT and the FFI, check out https://github.com/koreader/koreader-base