Cost of wrapping ffi functions

  • From: Tim Caswell <tim@xxxxxxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 6 Jun 2012 09:34:18 -0500

I have a general question about wrapping libraries using ffi.

I want to expose a nice lua-friendly API to my users, but use ffi under the
hood instead of C bindings.

I know that Mike said that it's bad to alias the ffi functions directly
because that will actually slow things down.  For example:

local gles = ffi.load("/usr/lib/x86_64-linux-gnu/libGLESv2.so")
gles.glViewport(640, 480, screen.w, screen.h)

is better than:

local gles = ffi.load("/usr/lib/x86_64-linux-gnu/libGLESv2.so")

local glViewport = gles.glViewport
glViewport(640, 480, screen.w, screen.h)

But I want to wrap some functions that are hairy to deal with
(outargs, strings, structs -> tables, etc..)  Is the best way to
accomplish this to wrap in a lua function and use the closure to the
gles object?

  local function glViewport(x, y, width, height)
    gles.glViewport(x, y, width, height)
  end

- Tim Caswell

Other related posts: