Re: Allocation sinking in git HEAD

  • From: Mike Pall <mike-1207@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 4 Jul 2012 00:49:17 +0200

Francesco Abbate wrote:
> I guess that it will help a lot for numeric code with complex numbers
> since these are cdata objects in luajit.

Yes, the following (simplified) complex number implementation
performs the same as the other examples I gave:

  local ffi = require("ffi")
  local type = type
  local cx
  cx = ffi.metatype("complex", {
    __add = function(a, b)
      if type(a) == "number" then return cx(a+b.re, b.im)
      elseif type(b) == "number" then return cx(a.re+b, a.im)
      else return cx(a.re+b.re, a.im+b.im) end
    end,
  })
  local a, b = 1.5+2.5i, 3.25+4.75i
  for i=1,1e8 do a = (a + b) + b end
  print(a)

 10.6  LuaJIT -O-sink
  0.20 LuaJIT -O+sink
  ====

I'm planning to add a standard "ffi.complex" module, which
implements all of the common operators and functions. But I'll
probably have to add a couple of things to the JIT compiler:
JIT-compiling calls to C functions with complex args/returns
and marking C functions as 'pure'.

One thing to remember is to always use a "complex[?]" cdata array
or cdata structs if you want to store lots of complex numbers.
Avoid storing them in plain Lua tables, because then all of them
must be individually boxed. No problem storing an occasional
complex value in a table with other data, of course.

[
Actually, that's one example where the LuaJIT FFI can do much
better than Java: in Java, a 1000 element array of complex numbers
has 1000 pointers to 1000 individually boxed complex objects. You
can imagine this isn't exactly efficient ...

OTOH a "complex[1000]" array needs only 2*8*1000 bytes with the
LuaJIT FFI, the same as in C. And it's not scanned by the GC.
]

> I'm eager to test that with some numeric code, I will probably
> post some results!

Please do!

--Mike

Other related posts: