lua-users home
lua-l archive

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


On Sun, Oct 23, 2022 at 7:25 AM bil til <biltil52@gmail.com> wrote:

> I assume, this depends strongly on the C compiler used?

This will obviously depend on the toolset, toolset build-time flags,
whatever additional config #defines,the host code (if Lua is
embedded), the OS and even the OS configuration at the run-time. And
this will also depend on what exactly is being measured, because an
allocation of a single byte from the program's perspective may mean an
allocation of several megabytes of user memory, and bytes or kilobytes
of kernel memory.

Because all the tests in the book's original code may be assumed done
with all the above things exactly the same, we can then derive:

X  + 1000000Y = 95K
X  + 1000000y = 65K
2X +              Y = 24K

where X is the memory taken by the 1M-element "array" table, Y is for
a two-element "hash" table, and y for a two-element "array" table.
Note we have nothing for the memory taken by the numbers because the
numbers are stored directly in the tables, unlike strings, and we
ignore the memory consumed by two strings for "x" and "y". Assuming K
is actually one megabyte, we obtain X = ~12M, Y = ~84, y = ~52.

Comparing that with the source code of Lua 5.1.4, we can immediately
rule out that the system was a 64-bit system, because a properly
aligned struct Table would take 8 64-bit words, i.e., 64 bytes, which
is more than y, which is a struct Table + at least an array. So we had
a 32-byte system in the test. Then, sizeof (struct Table) is 32, and
the remaining 20 bytes of y must be the array part. This would make
sense because sizeof (TValue) = 8 + 4 = 12, times 2 = 24. Likewise,
the remaining 52 bytes of Y must be for the hash part. Each node in
the hash table is essentially twice the TValue, thus 24 bytes, times 2
= 48. Finally, we can see that X is sizeof (TValue) times 1M = 12M.

Cheers,
V.