Re: [ANN] LuaJIT Roadmap 2012/2013

  • From: Mike Pall <mike-1206@xxxxxxxxxx>
  • To: luajit@xxxxxxxxxxxxx
  • Date: Thu, 14 Jun 2012 03:20:00 +0200

Geoff Leyland wrote:
> I'm hoping it'll transform this:
> 
> point = {}
> function point:new(x, y)
>   self.__index = self
>   return setmetatable({x=x, y=y}, self)
> end
> function point.__add(a, b)
>   return point:new(a.x + b.x, a.y + b.y)
> end
> 
> do
>   local a, b = point:new(1, 1), point:new(2, 2)
>   for i = 1, 1000 do
>     a = (a + b) + b
>   end
>   io.stderr:write(a.x, ", ", a.y)
> end
> 
> into this:
> 
> do
>   local ax, ay, bx, by = 1, 1, 2, 2
>   for i = 1, 1000 do
>     ax = ax + bx + bx
>     ay = ay + by + by
>   end
>   io.stderr:write(ax, ", ", ay)
> end

Yes, more or less. Inside the loop everything will be computed in
registers. There's still a 'point' allocation at the start of the
loop. And another 'point' object will be materialized from the
registers when the loop exits (it'll be eliminated if/when that
side trace is compiled eventually).

BTW: That 'self.__index = self' line should be outside of the
allocation function. You're performing a write to the metatable
every time -- never do that! That's a performance hit even for
plain Lua, since it invalidates the negative metamethod cache.

Oh, and 'local point = {}' ...

--Mike

Other related posts: