Speeding up FFI in the LuaJIT interpreter

  • From: agentzh <agentzh@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Sun, 25 Nov 2012 15:34:46 -0800

Hello!

I've found that using FFI in the interpreter mode of LuaJIT is much
slower than APIs based on traditional lua_CFunctions.

For example, in the following Lua code snippet for the ngx_lua module,
when the ngx.md5_bin() API is implemented via FFI, it is 38% slower
than the old CFunction-based implementation when the JIT compiler is
disabled:

    local md5_bin = ngx.md5_bin
    local last = ngx.now()

    for i = 1, 2000 do
        for j = 1, 2000 do
            md5_bin(i)
        end
    end

    ngx.update_time()
    local elapsed = ngx.now() - last
    ngx.say("ok: ", elapsed, " sec")

(Of course, when the JIT compiler is enabled and in effect, the
FFI-based version is 2.73 times faster than the CFunction-based
implementation.)

So I'm wondering if there's any plan for LuaJIT 2.1 (or even beyond)
to optimize the FFI calls in LuaJIT's fast interpreter.

If FFI calls are at least as fast as CFunction calls, we can safely
port all of our Lua APIs in the ngx_lua module to FFI without loosing
any performance even in the interpreter mode.

(For those who are wondering why we should worry about the interpreter
mode of LuaJIT at all: the trace compilation process in LuaJIT can
easily be aborted in the wild due to calls to other traditional Lua C
libraries like cjson or lpeg or use of NYI primitives like the CAT
bytecode and the table.concat builtin function).

Thanks!
-agentzh

Other related posts: