lua-users home
lua-l archive

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


Hello,

The following idiom is sometimes proposed for feeding an array in a loop:
    local array = {}
    local i = 1
    for/while/repeat
        <compute item>
        array[i] = item
        i = i+1
    end
instead of "table.insert(array,item)" or "array[#array+1] = item". The reason is probably to avoid Lua recomputing the current array size (or last index) at each iteration. This probably means Lua does not cache the array part's size in the C structure representing a Lua table.

Correct? If yes, why? Would this be a big overhead? Tables already are rather complicated constructs anyway; and Lua must know this information when inserting (or removing) an item. I have the impression, maybe false, that caching it would not impact perfomance significantly, while making the above idiom unnecessary, since insertion at end (pushing) would be as fast as possible.

Side question: what sort of data structure is used for a table's array part?

Denis
________________________________

la vita e estrany

http://spir.wikidot.com/