1 <!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
4 <title>FFI Library
</title>
5 <meta http-equiv=
"Content-Type" content=
"text/html; charset=iso-8859-1">
6 <meta name=
"Author" content=
"Mike Pall">
7 <meta name=
"Copyright" content=
"Copyright (C) 2005-2012, Mike Pall">
8 <meta name=
"Language" content=
"en">
9 <link rel=
"stylesheet" type=
"text/css" href=
"bluequad.css" media=
"screen">
10 <link rel=
"stylesheet" type=
"text/css" href=
"bluequad-print.css" media=
"print">
11 <style type=
"text/css">
12 span
.codemark
{ position:absolute
; left: 16em; color: #4040c0; }
13 span
.mark
{ color: #4040c0; font-family: Courier New
, Courier
, monospace
;
15 pre
.mark
{ padding-left: 2em; }
20 <a href=
"http://luajit.org"><span>Lua
<span id=
"logo">JIT
</span></span></a>
27 <a href=
"luajit.html">LuaJIT
</a>
29 <a href=
"install.html">Installation
</a>
31 <a href=
"running.html">Running
</a>
34 <a href=
"extensions.html">Extensions
</a>
36 <a class=
"current" href=
"ext_ffi.html">FFI Library
</a>
38 <a href=
"ext_ffi_tutorial.html">FFI Tutorial
</a>
40 <a href=
"ext_ffi_api.html">ffi.* API
</a>
42 <a href=
"ext_ffi_semantics.html">FFI Semantics
</a>
45 <a href=
"ext_jit.html">jit.* Library
</a>
47 <a href=
"ext_c_api.html">Lua/C API
</a>
50 <a href=
"status.html">Status
</a>
52 <a href=
"changes.html">Changes
</a>
55 <a href=
"faq.html">FAQ
</a>
57 <a href=
"http://luajit.org/performance.html">Performance
<span class=
"ext">»</span></a>
59 <a href=
"http://luajit.org/download.html">Download
<span class=
"ext">»</span></a>
65 The FFI library allows
<b>calling external C
functions
</b> and
66 <b>using C
data structures
</b> from pure Lua code.
71 The FFI library largely obviates the need to write tedious manual
72 Lua/C bindings in C. No need to learn a separate binding language
73 — <b>it parses plain C
declarations!
</b> These can be
74 cut-n-pasted from C
header files or reference manuals. It's up to
75 the task of binding large libraries without the need for dealing with
76 fragile binding generators.
80 The FFI library is tightly integrated into LuaJIT (it's not available
81 as a separate module). The code generated by the JIT-compiler for
82 accesses to C
data structures from Lua code is on par with the
83 code a C
compiler would generate. Calls to C
functions can
84 be inlined in JIT-compiled code, unlike calls to functions bound via
85 the classic Lua/C API.
88 This page gives a short introduction to the usage of the FFI library.
89 <em>Please use the FFI sub-topics in the navigation bar to learn more.
</em>
92 <h2 id=
"call">Motivating Example: Calling External C Functions
</h2>
94 It's really easy to call an external C
library function:
96 <pre class=
"code mark">
97 <span class=
"codemark">①
101 ③</span>local ffi = require(
"ffi")
103 <span style=
"color:#00a000;">int printf(const char *fmt, ...);
</span>
105 ffi.C.printf(
"Hello %s!",
"world")
108 So, let's pick that apart:
111 <span class=
"mark">①</span> Load the FFI library.
114 <span class=
"mark">②</span> Add a C
declaration
115 for the function. The part inside the double-brackets (in green) is
116 just standard C
syntax.
119 <span class=
"mark">③</span> Call the named
120 C
function
— Yes, it's that simple!
122 <p style=
"font-size: 8pt;">
123 Actually, what goes on behind the scenes is far from simple:
<span
124 style=
"color:#4040c0;">③</span> makes use of the standard
125 C
library namespace
<tt>ffi.C
</tt>. Indexing this namespace with
126 a symbol name (
<tt>"printf"</tt>) automatically binds it to the the
127 standard C
library. The result is a special kind of object which,
128 when called, runs the
<tt>printf
</tt> function. The arguments passed
129 to this function are automatically converted from Lua objects to the
130 corresponding C
types.
133 Ok, so maybe the use of
<tt>printf()
</tt> wasn't such a spectacular
134 example. You could have done that with
<tt>io.write()
</tt> and
135 <tt>string.format()
</tt>, too. But you get the idea ...
138 So here's something to pop up a message box on Windows:
141 local ffi = require(
"ffi")
143 <span style=
"color:#00a000;">int MessageBoxA(void *w, const char *txt, const char *cap, int type);
</span>
145 ffi.C.MessageBoxA(nil,
"Hello world!",
"Test",
0)
148 Bing! Again, that was far too easy, no?
150 <p style=
"font-size: 8pt;">
151 Compare this with the effort required to bind that function using the
152 classic Lua/C API: create an extra C
file, add a C
function
153 that retrieves and checks the argument types passed from Lua and calls
154 the actual C
function, add a list of module functions and their
155 names, add a
<tt>luaopen_*
</tt> function and register all module
156 functions, compile and link it into a shared library (DLL), move it to
157 the proper path, add Lua code that loads the module aaaand ... finally
158 call the binding function. Phew!
161 <h2 id=
"cdata">Motivating Example: Using C Data Structures
</h2>
163 The FFI library allows you to create and access C
data
164 structures. Of course the main use for this is for interfacing with
165 C
functions. But they can be used stand-alone, too.
168 Lua is built upon high-level data types. They are flexible, extensible
169 and dynamic. That's why we all love Lua so much. Alas, this can be
170 inefficient for certain tasks, where you'd really want a low-level
171 data type. E.g. a large array of a fixed structure needs to be
172 implemented with a big table holding lots of tiny tables. This imposes
173 both a substantial memory overhead as well as a performance overhead.
176 Here's a sketch of a library that operates on color images plus a
177 simple benchmark. First, the plain Lua version:
180 local floor = math.floor
182 local function image_ramp_green(n)
186 img[i] = { red =
0, green = floor((i-
1)*f), blue =
0, alpha =
255 }
191 local function image_to_grey(img, n)
193 local y = floor(
0.3*img[i].red +
0.59*img[i].green +
0.11*img[i].blue)
194 img[i].red = y; img[i].green = y; img[i].blue = y
199 local img = image_ramp_green(N)
201 image_to_grey(img, N)
205 This creates a table with
160.000 pixels, each of which is a table
206 holding four number values in the range of
0-
255. First an image with
207 a green ramp is created (
1D for simplicity), then the image is
208 converted to greyscale
1000 times. Yes, that's silly, but I was in
209 need of a simple example ...
212 And here's the FFI version. The modified parts have been marked in
215 <pre class=
"code mark">
216 <span class=
"codemark">①
233 ⑤</span><b>local ffi = require(
"ffi")
235 </b><span style=
"color:#00a000;">typedef struct { uint8_t red, green, blue, alpha; } rgba_pixel;
</span><b>
238 local function image_ramp_green(n)
239 <b>local img = ffi.new(
"rgba_pixel[?]", n)
</b>
241 for i=
<b>0,n-
1</b> do
242 <b>img[i].green = i*f
</b>
243 <b>img[i].alpha =
255</b>
248 local function image_to_grey(img, n)
249 for i=
<b>0,n-
1</b> do
250 local y =
<b>0.3*img[i].red +
0.59*img[i].green +
0.11*img[i].blue
</b>
251 img[i].red = y; img[i].green = y; img[i].blue = y
256 local img = image_ramp_green(N)
258 image_to_grey(img, N)
262 Ok, so that wasn't too difficult:
265 <span class=
"mark">①</span> First, load the FFI
266 library and declare the low-level data type. Here we choose a
267 <tt>struct
</tt> which holds four byte fields, one for each component
268 of a
4x8
bit RGBA pixel.
271 <span class=
"mark">②</span> Creating the data
272 structure with
<tt>ffi.new()
</tt> is straightforward
— the
273 <tt>'?'
</tt> is a placeholder for the number of elements of a
274 variable-length array.
277 <span class=
"mark">③</span> C
arrays are
278 zero-based, so the indexes have to run from
<tt>0</tt> to
279 <tt>n-
1</tt>. One might want to allocate one more element instead to
280 simplify converting legacy code.
283 <span class=
"mark">④</span> Since
<tt>ffi.new()
</tt>
284 zero-fills the array by default, we only need to set the green and the
288 <span class=
"mark">⑤</span> The calls to
289 <tt>math.floor()
</tt> can be omitted here, because floating-point
290 numbers are already truncated towards zero when converting them to an
291 integer. This happens implicitly when the number is stored in the
292 fields of each pixel.
295 Now let's have a look at the impact of the changes: first, memory
296 consumption for the image is down from
22 Megabytes to
297 640 Kilobytes (
400*
400*
4 bytes). That's a factor of
35x less! So,
298 yes, tables do have a noticeable overhead. BTW: The original program
299 would consume
40 Megabytes in plain Lua (on x64).
302 Next, performance: the pure Lua version runs in
9.57 seconds (
52.9
303 seconds with the Lua interpreter) and the FFI version runs in
0.48
304 seconds on my machine (YMMV). That's a factor of
20x faster (
110x
305 faster than the Lua interpreter).
307 <p style=
"font-size: 8pt;">
308 The avid reader may notice that converting the pure Lua version over
309 to use array indexes for the colors (
<tt>[
1]
</tt> instead of
310 <tt>.red
</tt>,
<tt>[
2]
</tt> instead of
<tt>.green
</tt> etc.) ought to
311 be more compact and faster. This is certainly true (by a factor of
312 ~
1.7x). Switching to a struct-of-arrays would help, too.
314 <p style=
"font-size: 8pt;">
315 However the resulting code would be less idiomatic and rather
316 error-prone. And it still doesn't get even close to the performance of
317 the FFI version of the code. Also, high-level data structures cannot
318 be easily passed to other C
functions, especially I/O functions,
319 without undue conversion penalties.
325 Copyright
© 2005-
2012 Mike Pall
326 <span class=
"noprint">
328 <a href=
"contact.html">Contact
</a>