lua-users home
lua-l archive

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


On Tue, Oct 25, 2022 at 2:08 PM bil til <biltil52@gmail.com> wrote:
>
> But how do you know, that Lua would not already used TValue elements
> for the index part of tables in a packed form?

I am not sure what your question is about. Lua is entirely open
source, so anyone can tell exactly what (unmodified) Lua does. I am
also not sure what you mean by the "index part" of the table.

In Lua 51.4., TValue is defined lobject.h thus:

#define TValuefields Value value; int tt

typedef struct lua_TValue {
  TValuefields;
} TValue;

Table-related structured are define in the same file below:

/*
** Tables
*/

typedef union TKey {
  struct {
    TValuefields;
    struct Node *next;  /* for chaining */
  } nk;
  TValue tvk;
} TKey;


typedef struct Node {
  TValue i_val;
  TKey i_key;
} Node;


typedef struct Table {
  CommonHeader; // GCObject *next; lu_byte tt; lu_byte marked
  lu_byte flags;  /* 1<<p means tagmethod(p) is not present */
  lu_byte lsizenode;  /* log2 of size of `node' array */
  struct Table *metatable;
  TValue *array;  /* array part */
  Node *node;
  Node *lastfree;  /* any free position is before this position */
  GCObject *gclist;
  int sizearray;  /* size of `array' array */
} Table;


As you can see, there are no packing attributes in these definitions,
and both parts of the table point to the same TValue structures. This
actually means that, at least in the hash part, Lua could have saved
some memory by using one memory word to store the tags of the key and
the value, but it did not do that in 5.1.4. This was done in a later
version and is still done in Lua 5.4.4.

Cheers,
V.