Embedding LuaJIT with FFI

Vadim Graboys
@deapthoughts
github.com/vadimg/luajit-talk

Who am I?

What's Lua and why would I want to put it in my binary?

But also weird:
http://notebook.kulchenko.com/programming/lua-good-different-bad-and-ugly-parts

Lua Crash Course

Arrays are tables:

local arr = {1, 2, 3}

Dictionaries are tables:

local dict = {a = 1, b = 2}

Array indexes start at 1:

> arr[1]
1

Objects are also tables

Account = {
    balance = 0,
    new = function(self)
        local o = {}
        setmetatable(o, self)
        self.__index = self
        return o
    end,
    deposit = function(self, amt)
        self.balance = self.balance + amt
    end
}

local o = Account.new(Account)
o.deposit(o, 10)
print(o.balance)
Output: 10

Object syntatic sugar

Account = { balance = 0 }
function Account:new()
    local o = {}
    setmetatable(o, self)
    self.__index = self
    return o
end

function Account:deposit(amt)
    self.balance = self.balance + amt
end

local o = Account:new()
o:deposit(10)
print(o.balance)
Output: 10

Lua/C interface

The interface is an abstract stack

To send data or register functions to lua:

To get data from lua:

What's Chartbeat's problem?

Realtime analytics is hard.

Memoryfly

Common query

What are the top k pages (filtered by some criteria)?

Similar to:
Given a list of strings, what are the top k most frequently occurring strings?


Algorithm

Implementation

Located at: http://github.com/vadimg/luajit-talk

Implemented in:

Data is a list of 1 million strings, 1 per line, in a file.

Common interface to data for all implementations (data.h)

Benchmark tests: C boundary, hashtable, array, function call, string operation speed

LuaJIT Limitations

Takeaway

We're hiring

chartbeat.com/jobs