lua-users home
lua-l archive

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


Le 1 août 2014 à 14:00, Jean-Luc Jumpertz <jean-luc@celedev.eu> a écrit :

>>> 
>>> And btw testing the  '__len' and '__index'  metamethods in function 'luaB_ipairs' has an almost null performance cost and gives you additional flexibility, so what would it bring to restrict this test to the '__len' metamethod, as Philipp Janda proposed? :-)
>> 
>> It's useless code that causes one extra case of undefined behavior, so why keep it? And what's the additional flexibility?
> 
> The additional flexibility is to allow you to freely define the values you want to return for table[i] and to keep it consistent between direct accesses like << a = my_table[i] >> and for loops like << for i, v in ipairs(my_table) >>, even if you don't redefine __len

Although I agree that in this case, with '__index' defined and not '__len', there is a performance penalty associated with the repeated call of lua_len(), especially if the table has not been rehashed.
Theoretical question: would this justify the caching of the len in a table when calculated, until the next set element in this table?