lua-users home
lua-l archive

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

----- Ursprüngliche Message -----

> Von: Steve Litt <>
> An:
> CC: Leo Romanoff <>
> Gesendet: 21:18 Freitag, 26.Juli 2013
> Betreff: Re: Question about accessing Lua tables and arrays faster
> On Fri, 26 Jul 2013 11:42:35 -0700 (PDT)
> Leo Romanoff <> wrote:
>>  ----- Ursprüngliche Message -----
>>  > Von: Steve Litt <>
> [clip]
>>  > Two more things, and the first one is this: I have no doubt that the
>>  > others are faster for going over and incrementing each element. But
>>  > I'm wondering if, for more ambitious usages of elements, you could
>>  > put the element processing in a metatable, and if that would speed
>>  > things up.
>>  Can you elaborate a bit? What do you mean by putting element
>>  processing in a metatable? I'm not sure I understand what you mean.
>>  Do you mean that __index method should do the actual processing, e.g.
>>  increment a value?
> Yes. __index() and __newindex()


> [clip]
>>  > If you try the "process the element using the metatable" 
> approach,
>>  > please let us know how it works.
>>  I'd be happy to try out and provide feedback once you explain me
>>  what "process the element using the metatable" exactly means ;-)
> Hi Leo,
> Just what you mentioned earlier: have the metatable's __index() and
> __newindex() do some of the actual computation. So if you're
> incrementing each position, have the __index increment it every time
> you read. Or every time you read if some flag is set.

May be I'm getting tired: I still do not quite follow. OK, I overload __index() and/or __newindex() with my own lua functions.
But what should they increment? They need a value to increment, right? How do they access it? Via the rawget() and rawset()? Or do you mean something else?

May be put some example code here to avoid any further misunderstandings?

> By the way, there's a rawget(t, i) and a rawset(t, k, v) that skip
> __index() and __newindex() respectively, but PIL says those don't gain
> you any performance.