lua-users home
lua-l archive

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


> 
> There are exactly two times when the __key metamethod gets called:
> 
> t[ud] = v  --> t[ud.__key()] = v
> v = t[ud]  --> v = t[ud.__key()]
> 
> Of note, it does NOT get called in this situation:
> 
> k = ud.__key()
> assert(t[k] == v) -- no __key used here!
> 
> But this absolutely ought to work.
> 
> Yes, there's ONE extra check made when accessing a table ("does the
> key have a __key metamethod, yes or no?") so yes it does TECHNICALLY
> make things more expensive, but this isn't anywhere close to a "good
> luck with performance" kind of impact.
> 

Where is the synthetic key value stored then? It must be stored in the key+value slot of the table. Where is the userdata stored? In the same place. So now we are storing three items per table slot, not two. Seems to me a significant impact.

And yes, a given __key *should* return the same value each time it is called for a given userdata in a given state. But if it doesn’t strange things happen. Strange in that they vastly complicate the table contract. And to what end?

I’m not being obtuse here, I just don’t see the bang for the buck.

—Tim