lua-users home
lua-l archive

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

Em qua., 23 de dez. de 2020 às 00:09, 云风 Cloud Wu <> escreveu:
Hugo Musso Gualandi <> 于2020年12月23日周三 上午4:21写道:
> > I think using a pointer to hold the string data would be better then
> > putting the data into a continuous TString structure.
> One advantage of putting the string data in a contiguous structure is that it avoids having to go through an additional layer of pointer indirection to read the string contents.
> But to be honest, I don't know how much that matters in practice. That's an interesting question.

In my opinion, the string object in lua VM core is only a reference.
We seldom use long-string as a key of the table,
and to the short-string key, lua doesn't need to read the string
contents. It only compares the pointers of TString.

A smaller GCObject is more (cache) friendly to the collector because
it always traverses the objects.
With C99 is possible declare a struct with 0 size, like this:

typedef struct TString {
  lu_byte extra;  /* reserved words for short strings; "has hash" for longs */
  lu_byte shrlen;  /* length for short strings */
  unsigned int hash;
  union {
    size_t lnglen;  /* length for long strings */
    struct TString *hnext;  /* linked list for hash table */
  } u;
  char contents[];
} TString;

Which effectively removes the "contents" field from the structure itself and places it immediately afterwards in memory.

Ranier Vilela