I’m not sure how to address this. All of these points so far have been outlandish and/or wrong.
This is the Lua source code. This is what runs on your computer when you run Lua. Lua isn’t self hosted, it’s a language made in C. Roblox’s implementation is a fork of the Lua 5.1 C implementation.
The compiled (hereafter “native”) stack is irrelevant; the native stack is constantly changing whenever Lua calls into Roblox code or Roblox code into Lua or native functions entering and exiting.
What does “the Lua stack gets transcompiled into C” mean? Lua’s stack is an array of values in on the heap. The lua_State
struct in the source code has a stack
field which refers to it, and 2 fields (top
, base
) to help identify which parts of it are active. When Lua code is compiled, it compiles to a bytecode format which runs in the Lua interpreter.
The bytecode comes with the variable offsets baked into the instructions. Instead of “access variable abc
”, you see “access variable at index 4
” for example.
The reason you see local variables having faster lookup times is because internally it’s an array index. It just indexes from the Lua stack, which is on the heap. Alternatively, if abc
was a global, a GETGLOBAL
instruction would be produced, which does a dictionary look-up over the environment table that getfenv(1) normally returns.
I’m not sure what this refers to. The Lua stack is stored on the heap. The heap is just available and allocated RAM pages.
His sources are for the native stack, which like I mentioned before has nothing to do with this.
In Lua, because, as I explained, declaring a global involves a dictionary which is orders of magnitude slower to access than an array. If you test this on any compiled language (C, C++, Rust, etc) you will find that heap allocated values in those languages are at worst ever so slightly slower than stack values.
It is a rounding error usually, and only shows itself when you are dealing with gigantic amounts of data. This is because of the CPU cache and other optimizations in the hardware. These optimizations generally don’t affect Lua, because Lua is a language hosted on top of C. It is not running directly on the CPU.