Re: Key lookup optimizations...

  • From: Coda Highland <chighland@xxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Tue, 1 Sep 2015 13:16:47 -0700

On Tue, Sep 1, 2015 at 12:19 PM, Javier Guerra Giraldez
<javier@xxxxxxxxxxx> wrote:

On Tue, Sep 1, 2015 at 1:47 PM, Coda Highland <chighland@xxxxxxxxx> wrote:
A question to be asked: Are you using the Lua C API for exposing
functions to scripts, or are you using FFI? The former is RIDICULOUSLY
slow; the latter is nearly as fast as if you had made the same call in
C.

contrary to common repeated advice, the C API is _not_ slow in LuaJIT.
It's just as fast as it is in plain Lua.

Yes, I know, I said as much in my larger explanation -- it's slow
relative to the alternative, and it's potentially a good place to
start optimizing if performance is proving to be a problem.

On Tue, Sep 1, 2015 at 12:42 PM, Vyacheslav Egorov
<dmarc-noreply@xxxxxxxxxxxxx> wrote:
On Tue, Sep 1, 2015 at 8:48 PM Coda Highland <chighland@xxxxxxxxx> wrote:
If you have:

a.b.c.d()
a.b.c.e()

then internally LuaJIT will do something like

local f = a.b.c
f.d()
f.e()


This is a somewhat inaccurate description of what's actually happening.

Fair enough, I should have specified that this is only true if d()
never calls a function using the Lua C API. I tend to take this for
granted because I never use the Lua C API in any place where I care
about performance, and if I don't care about performance I don't care
about CSE either.

On Tue, Sep 1, 2015 at 1:02 PM, Lance Thornblad
<lance.thornblad@xxxxxxxxx> wrote:
Yes, the __index metamethod is a lua_cfunction because it does a branching
search for the key based on certain criteria. Rewriting as a Lua function
is not really an option. I'm trying to keep as little as possible exposed
directly to Lua tables. Lua is merely the language used to create and
manipulate game objects (of all kinds).

Profiling the code would probably not be that useful at the moment because
the Lua part isn't actually responsible for much. Putting it through its
paces is definitely a good idea, though.

In my opinion, you should go ahead and write that logic in Lua, inject
the Lua code into the environment from the host environment (so you
can treat it as part of the binding API and not part of the script)
and then lock down the metatable so that user code can't mess with it.
Likewise, you can wrap FFI calls in Lua functions, keeping the FFI
namespace as an upvalue and removing the ffi object from the global
environment so that user code can't use that, either.

There are, of course, limits to sandboxing, but it's not generally
very hard to block the obvious routes.

/s/ Adam

Other related posts: