lua-users home
lua-l archive

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


Em ter., 15 de nov. de 2022 às 11:47, Roberto Ierusalimschy <roberto@inf.puc-rio.br> escreveu:
> #define ARRAY_FLEXIBLE_MEMBER            1
>
> Lua 5.4.2 Windows (64 bits)
> type sizes (bytes):
> Struct                             Size
> [...]
> Udata                                48
> CClosure                          48
> [...]
>
> #define ARRAY_FLEXIBLE_MEMBER            0
>
> Lua 5.4.2 Windows (64 bits)
> type sizes (bytes):
> Struct                             Size
> [...]
> Udata                               32
> CClosure                         32
> [...]

I believe you are printing the result of 'sizeof'. What Gé just
explained is that we don't use 'sizeof' for these structures.
If you print the result of 'offsetof' for the last field of
each structure (which is what Lua uses), you will see that the
actual size of those arrays (that is, the value of ARRAY_FLEXIBLE_MEMBER)
is irrelevant.
Yeah, this is sizeof printing.
 

As a quick experiment, change the size of these arrays to something
absurdly large, such as this:

     size_t lnglen;  /* length for long strings */
     struct TString *hnext;  /* linked list for hash table */
   } u;
-  char contents[1];
+  char contents[1000000];
 } TString;

The resulting code uses exactly as much memory as the original.
None has talked about the memory usage by the array field.
Of course, that memory varies in usage, by zero to infinite.

This is about the memory usage by the struct itself.
With 32 bytes vs 48 bytes, stores 1 milions struct vars into another array of struct Udata, per example.