How Lua handle with table in memory -


my doubt lua tables, how lua handles table's grow?

is equal arraylist in java? needs continuous memory space, , if grows, internal array copied memory space...

there clever way led that?

my doubt how stored in memory, "under hood". nothing related how implement array in lua.

(assuming you're referring recent versions of lua; describing behavior of 5.3 should (nearly?) same 5.0-5.2.)

under hood, table contains array , hash part. both (independently) grow , shrink in power-of-two steps, , both may absent if aren't needed.

most key-value pairs stored in hash part. however, positive integer keys (starting 1) candidates storing in array part. array part stores values , doesn't store keys (because equivalent element's position in array). half of allocated space allowed empty (i.e. contain nils – either gaps or trailing free slots). (array candidates leave many empty slots instead put hash part. if array part full there's leftover space in hash part, entries go hash part.)

for both array , hash part, insertions can trigger resize, either next larger power of 2 or down smaller power of 2 if sufficiently many entries have been removed previously. (actually triggering down-resize non-trivial: rehash place table resized (and both parts resized @ same time), , called luah_newkey if there wasn't enough space in either of 2 parts1.)

for more information, can @ chapter 4 of the implementation of lua 5.0, or inspect source: of relevance happens in ltable.c, interesting starting points reading rehash (in ltable.c) (the resizing function), , main interpreter loop luav_execute (in lvm.c) or more luav_settable (also there) (what happens when storing key-value pair in table).


1as example, in order shrink table contained large array part , no hash, you'd have clear array entries , add entry hash part (i.e. using non-integer key, value may including nil), end table contains no array part , 1-element hash part.
if both parts contained entries, you'd have first clear hash part, add enough entries array part fill both array , hash combined (to trigger resize leave table large array part , no hash), , subsequently clear array above.2 (first clearing array , hash won't work because after clearing both parts you'll have no array , huge hash part, , cannot trigger resize because entries go hash.)

2actually, it's much easier throw away table , make new one. ensure table shrunk, you'd need know actual allocated capacity (which is not current number of entries, , lua won't tell you, @ least not directly), steps , sizes right – mix order of steps or fail trigger resize , you'll end huge table may perform slower if you're using array… (array candidates stored in hash store keys, half amount of useful data in e.g. cache line.)


Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -