Re: m = mat4(1, 2, 3, …) 75x slower than m = mat4(); m.m11 = 1; … (was: Allocation sinking in git HEAD)

  • From: Mike Pall <mike-1207@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Wed, 4 Jul 2012 22:15:35 +0200

Adam Strzelecki wrote:
> Also this example shows another problem, using cdata
> initializers is much much slower (75x slower) than initializing
> cdata with empty value then assigning structure fields. I
> thought it is should be opposite. Anyone knows why?

The allocations weren't sunk, anyway, because the PHI limit was
hit. The observed runtime difference was a side-effect of some
other issue: the compiler gave up on the loop and tried to compile
the functions instead (which is less efficient, though). One of
those two variants hit a NYI, the other didn't.

I've bumped some limits and removed some other restrictions. Both
variants should now run much faster and at the same speed. Please
try again with git HEAD.

Note that in general the cdata initializer variant mat4(a, b, ...)
is preferred, because it's slightly easier to optimize for the
compiler than an allocation + explicit stores.

This difference matters more for Lua tables, where this:

  local t = { foo = x, bar = y }          -- Good.

is much better than this:

  local t = {}; = x; = y      -- Bad.

At least right now. It matters most if you do this within loops,
of course.

Also, please note that using 'float' does not improve anything
over 'double', except storage requirements -- which is pretty
irrelevant for temporary matrix objects.

All FP computations are done with 'double', so using 'float' for
storage causes lots of float<->double conversions. These can
become costly in some contexts. E.g. your example runs around 20%
faster, if one uses doubles instead of floats for the matrix.


Other related posts: