lua-users home
lua-l archive

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


> > You can use inheritance (delegation) for that, as we have
> > always done in Lua:
> > 
> > Events = { print = function (self) print(self.x) end }
> > Events.index = Events
> > 
> > MyTable = {x = 0}
> > eventtable(MyTable, Events)
> > 
> > MyTable:print()
> 
> Sure... But the other part of the equation is speed.  For 
> those of us who operate in real-time environments where 
> memory is limited, the handler tables take care of the size, 
> but nothing takes care of the speed.  It is ridiculous to 
> perform a table lookup, have it fail, call a tag method to do 
> yet another table lookup, when you know exactly where the 
> function you needed to call is already (in the handler 
> table), thereby only resulting in one table lookup.
> 
> Is there a solution to the speed issue?

I'll correct myself a touch here... I missed an optimization in
luaV_gettable().  Using the method above:

1) A check for the gettable method is made in the handler table (table
lookup).
2) A check for the index method is made in the handler table (table
lookup).
3) The index "method" is found (it's actually a table in Roberto's
sample).
4) A check for the index "method" being a function is made.  It's not,
so it does sends the index "table" to step 1 and performs the same
checks.

I guess what I am proposing is a fast, non-recursive approach to the
problem:

Lua code:

MyTable->print()

Calls a special luaV_gethandlertable() that does an immediate table
lookup and returns.  No tag methods taken into account.  No recursion.
Nothing.

The Lua script author can make a choice... Ultimate flexibility at the
expense of speed or fastest speed at the expense of ultimate
flexibility.  For my work, I'd always choose fastest speed.

Josh