Answer the question
In order to leave comments, you need to log in
When exactly in Lua does an array (a table with only array-part ) acquire a hash-part (become a hash )?
It is necessary to transfer tables (tables) in Lua code over the network (computer multiplayer game).
In this connection, the question of the size of the transmitted data (about keeping them as small as possible) is very relevant.
To save the structure of the table and "hold" it in an array-like style, it is recommended to use table.insert, table.remove...
There is a code:
local var = {}
for i = 1, 10000, 1 do
table.insert ( var, i )
end
var[10000] = { var = 1, some = "string", another = {} }
var[555] = false
var[1] = 1
var[2] = {}
var[1] = {}
local var = {}
for i = 1, 10000, 1 do
table.insert ( var, i )
end
var[10001] = 1
Answer the question
In order to leave comments, you need to log in
The ltable.c file says the following:
Thus, the array spans keys from 0 to such a number n that at least half is used. Let's take another look at the code for the computesizes function , which decides what the size of the array will be when the table is resized. This function iterates over powers of two (1, 2, 4, 8...) and sees how much of the array would be occupied if it were that size. Stops when this proportion becomes less than half. It is easy to prove that with such an array size selection algorithm and with continuous filling of all keys from 1 to n, the array size greater than or equal to n will be chosen.
By the way, it also follows from this that when adding elements to the end of the array, there will be overhead, but not for the hash part, but for empty array elements allocated "in reserve". But it should be so, so as not to rebuild the array with each new element. (In C++, vector.push_back behaves the same way.) If you already know the final size of the array and want to profit from it, write a C extension that calls lua_createtable(L, array-size, 0).
Regarding whether the hash part will appear when replacing values with different types. Will not appear. The fact is that in a key-value pair, the key and values are stored in different fields. I tracked how the i_val field is used, it turned out only in the gval macro, the type of which does not appear anywhere (only checks for nil).
Also, I would recommend using rawset and lua_rawseti as they don't check metamethods and should therefore be faster. About lua_rawseti, I'm sure that it works faster, but I suspect about rawset.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question