luau-lang / luau

A fast, small, safe, gradually typed embeddable scripting language derived from Lua
https://luau-lang.org
MIT License
3.8k stars 352 forks source link

Constant array boundary lookups in lua_ref #1224

Open Mooshua opened 3 months ago

Mooshua commented 3 months ago

Hi,

I'm seeing some semi-expensive luaH_getn invocations on the registry a la lua_ref. It seems like aboundary is being used by the hashtable, causing the VM to continuously find the array boundary again. Stepping through it in a debugger, it looks like t->aboundary/t->lastfree is always 1.

This has little impact except where lua_ref is in a hot path. Unfortunately, this means a lot of binding layers are impacted.

VTune Call Tree in a simple benchmark program:

Callers                         Effective Time  Spin Time   Overhead Time   Effective Time  Spin Time   Overhead Time   Wait Time: Total    Wait Time: Self
[Loop at line 781 in luaH_getn] 100.0%  0.0%    0.0%    637619.390usec  0usec   0usec       
  luaH_getn                 100.0%  0.0%    0.0%    637619.390usec  0usec   0usec       
    lua_ref                 100.0%  0.0%    0.0%    637619.390usec  0usec   0usec