0

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
Alexander Gladysh
  • 39,865
  • 32
  • 103
  • 160
  • A side note. Using the long string format `[[ ]]` in the last `ffi.new` lines instead of the simpler `" "` or `' '` forms contribute to obfuscate the code. – prapin Dec 23 '12 at 15:26
  • That is a matter of taste, personal preference and coding guidelines that are in effect for a given piece of code. But I see your point. Fixed. – Alexander Gladysh Dec 23 '12 at 18:28

1 Answers1

2

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
prapin
  • 6,395
  • 5
  • 26
  • 44