It is necessary to transfer tables (tables) in Lua code over the network (computer multiplayer game). In this connection, the issue of the size of the transmitted data (about keeping it as small as possible) is very relevant.

To preserve the structure of the table and "hold it" in an array-like style, we recommend using table.insert, table.remove ...

There is a code (litter for formatting, does not work in the browser):

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] = {} 

Questions:

  1. Will the <b> var </ b> table remain an array (only the c array part table), or is it converted to hash (a two-part table array and hash). (I myself think the answer "will remain only with array-part", since the structure of the table is not affected - only the values ​​of the elements are affected, but I highly doubt this answer, but you need to know for sure).
  2. Why? :) How to check it?
  3. Extremely precise: if the table initially has only array-part, will it result in changing the value of a table element directly through the syntax var[1] = {} var[2] = false var[3] = 1 , etc. to change the structure of the table and the appearance of hash-part? (Provided that it is given in advance :)

    local var = {} for i = 1, 10000, 1 do table.insert (var, i) end

I suspect that the answer for the code above is "no", but:

 var[10001] = 1 

Already affect the structure of the table and create "overhead" from memory. In this connection, I think the answer is (for the code above): up to n <= 10000, the code var [n] = {} does not affect the structure of the table, and therefore does not lead to the creation of a hash-part (the table remains array-like). However, the code var [n + 1] = {} will lead to memory overhead (for more, see http://www.lua.org/gems/sample.pdf )

Is the answer correct or not?

Thank!

    1 answer 1

    The point is that in Lua there are only associative "key-value" tables. She can only pretend to be a list. Therefore, arr ['1'] = 1; arr ['1000000'] = 1 does not mean that the table will swell as much as 999999 records.