Is there a more effecient way to handle argv functions than this?
ffi.cdef [[
void fooArgv(int argc, const char ** argv, const size_t * argvlen);
]]
local foo = function(...)
local nargs = select("#", ...)
local argv = { }
local argvlen = { }
for i = 1, nargs do
local v = tostring( (select(i, ...)) )
argv[i] = v
argvlen[i] = #v
end
return ffi.C.fooArgv(
nargs,
ffi.new("const char * [" .. nargs .. "]", argv),
ffi.new("const size_t [" .. nargs .. "]", argvlen)
)
end
end
If foo
is called a lot of times, you can easily optimize the bottom part of the function. Calling ffi.new
with a string argument forces LuaJIT to run its C parser every time, which is sub-optimal. The function ffi.typeof
can create a constructor for a given type that is used then instead of ffi.new
.
Also, I think that using the select
function in a loop is slower that creating an array and indexing from it. I am not sure of this.
So here is my proposed version:
ffi.cdef [[
void fooArgv(int argc, const char ** argv, const size_t * argvlen);
]]
local argv_type = ffi.typeof("const char* [?]")
local argvlen_type = ffi.typeof("const size_t [?]")
local foo = function(...)
local nargs = select("#", ...)
local argv = { ... }
local argvlen = { }
for i = 1, nargs do
local v = tostring( argv[i] )
argv[i] = v
argvlen[i] = #v
end
return ffi.C.fooArgv(
nargs,
argv_type(nargs, argv),
argvlen_type(nargs, argvlen)
)
end