lua-users home
lua-l archive

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


On Mon, Jan 10, 2022 at 4:51 PM Francisco Olarte <folarte@peoplecall.com> wrote:

> My main curiosity was I thought it may gain performance
> in my code which is full of ~12 byte strings.

A particular use case for small strings in Lua is their use as table
keys. A typical (short) string constant 'key' in Lua code like
table.key = foo; bar = table.key has its hash value readily available,
and, moreover, the string is interned and unique (per VM), so  the
table lookup is essentially an integer index operation followed by a
pointer comparison. If, naively, such a constant value would be
subject to a small-string optimization that places the literal value
into TValue, there is no hash and no uniqueness, so the hash would
have to be recomputed each time, to be followed by a full string
comparison.

As a matter of fact, there is no real difference between a string
"constant" and a string variable like in table[key] = foo. I think it
is safe to say that Lua users would take it for granted that either
"constant" or "variable" string indexing is generally fast.

I do not have any hard data, but it seems far from obvious that the
small-string optimization would be beneficial for this case, and, if
it is not (always) beneficial, it does not seem obvious that it would
be easy to decide at compile or run-time, when, besides the length
considerations, the small-string optimization should be effected.

Cheers,
V.