lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]




On 19/03/2013 8:36 PM, steve donovan wrote:
Hm, well experimentation shows that functions are still unique objects ;(

$ lua52
Lua 5.2.1  Copyright (C) 1994-2012 Lua.org, PUC-Rio
f = function() end
g = function() end
= f == g
false
= f
function: 003E9538
= g
function: 003E97B0

Sorry I just ralised I don't have 5.2 installed for some reason, that shifts things around a bit.


But then, read this: http://lua-users.org/lists/lua-l/2011-06/msg01358.html

That references 3.4.3 here:
http://www.lua.org/manual/5.2/manual.html

"""Closures with the same reference are always equal. Closures with any detectable difference (different behavior, different definition) are always different."""

I'm not exactly sure what "the same reference" means. This appears to be a tautology.

None the less this does seem to fall into the category of something that might be optimised, rather than something that will be optimised.


Treat anonymous functions as if they were like {}, and yes, the
instinct is then always to precreate!

Indeed although now with 5.2 it looks not so important.

I just need to decide whether to use a metatable or copy the methods directly into fields in each instance table.

Using my benchmarks on 5.2.1:

It looks as though using the metatable is substantially more memory efficient than copying method references into each object instance, which makes sense. On the other hand, invoking methods via the metatable looks to be about 5% slower than if they were stored in the object's table (I guess the cost of an indirection will never be 0).

In other news: method invocation using an upvalue for 1 instance state appears to be about 18% more efficient than looking the state up in the self table. However allocating the instance-unique closure is 25% slower than using a shared closure in the object instance constructor.


Strangely, construction with a named local variable (D) now appears to be slightly faster than declaring the function inline in the table ctor (B) for both construction time (5%) and method invocation time (2.5%):

function ClassD( x )
  local function _D_foo(self) return self.x_ + 1 end
  return { x_ = x, foo = _D_foo }
end
d = ClassD(1)
print( d:foo() )
assert( ClassD().foo == ClassD().foo ) --ok

function ClassB( x )
  return { x_ = x,
    foo = function(self) return self.x_ + 1 end }
end
b = ClassB(1)
print( b:foo() )
assert( ClassB().foo == ClassB().foo ) -- ok

Ross.