from Hacker News

Compact Representations for Arrays in Lua [pdf]

by tkhattra on 6/18/25, 11:49 PM with 19 comments

  • by ufo on 6/21/25, 10:37 PM

    This optimization might land in the next Lua release. More specifically, the "Reflected Arrays" version (Figure 6).

    https://github.com/lua/lua/blob/f71156744851701b5d5fabdda506...

  • by nzzn on 6/22/25, 7:07 PM

    Lua uses the table type to represent both dictionaries (hash tables) and arrays of values. This seems to have been predicated on keeping the language “simple” with a minimal number of defined types. A laudable goal.

    However, arrays of a single type are just enormously common in applications. Support for arrays is pretty much ubiquitous in other languages, including ones that are in the same general dynamic space.

    Internally Lua does treat arrays in their own pathway to keep performance reasonable. There is also some user facing special syntax for arrays. Arrays should be part of the core language — some learning overhead for the newcomer but worth it.

  • by marhee on 6/22/25, 7:34 AM

    I wonder, in reality, if a Lua program uses large (consecutive) arrays, its values will likely have the same type? At the very least it is a common use-case: large arrays of only strings, numbers etc. Wouldn’t it make sense to (also) optimize just for this case with a flag and a single type tag. Simple and it optimizes memory use for 98% of use cases?
  • by kzrdude on 6/21/25, 8:33 PM

    It was published in September 2024, so it's relatively recent.
  • by Jyaif on 6/21/25, 8:59 PM

    Jesus christ, 40% waste in arrays that can be solved by using `__attribute__((packed))`.

    Irresponsible of them of not advertising this as an option in luaconf.h