lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Hi all,

inspired by the other thread with LuaJIT/ffi for numerical
calculations, I've experimented with it on sockets, with rather
positive results - the microbenchmarks on a degenerate web server are
better than those on lighttpd (obviously there is quite a different
amount of processing in lighttpd on the received data, so it is
probably slower in the end, but still quite fast).

Also a small trick of "setmetatable(_G, { __index = ffi.C } )" in the
beginning of the program allows for a nice to read code - avoiding the
need to use ffi.C. for native C function calls - so the resulting code
is less bumpy (and this appears to give also errors on undeclared
variables reads as a bonus)

Some longer ramblings here:
if you want to toy with the test code, it's here on github:

The "socket_set" abstraction that I came up with allows to express the
"useful" part of the application as this:

local HTTP_REPLY = [[HTTP/1.0 200 OK
Content-Type: text/plain

This is test

local ss = socket_set(MAX_FD)

local my_accept_cb = function(fds, i)
  local cb = {} = function(fds, i, data, len)
    fds.send(i, HTTP_REPLY, #HTTP_REPLY)
  cb.close = function(fds, i)
    -- print("Closed socket")
  return cb

while not ss.add_listener(12345, my_accept_cb) do

print("Added listener, please run the test")

while true do
  local n = ss.poll(1000)


I thought of naming it "node.lua" but figured it's a bit too early ;-)