Repository navigation
perf: read Lua values straight off the stack - #720
Merged
Merged
Conversation
LuaScriptValue and MultiLuaScriptValue implement mlua's from_stack hooks: strings are read in place and tables are walked with the raw iteration API (lua_next, or lua_rawiter on Luau), so converting them creates no Lua references. Functions and userdata convert as before, results are the same. Adds a test checking both conversions agree, on every Lua variant.
Changed Files
|
lua_next raises an error when the table changed under it, which a finalizer run by a collection during the conversion can do. The error jumped over the Rust frames of the conversion.
…function can't fail
This was referenced Nov 13, 2026
Owner
Author
|
/bench |
Contributor
|
| Project | Bevy Scripting |
| Branch | agent/lua-stack-conversion |
| Testbed | linux-gha |
🐰 View full continuous benchmarking report in Bencher
⚠️ WARNING: Truncated view!The full continuous benchmarking report exceeds the maximum length allowed on this platform.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Lua values are read straight off the stack:
LuaScriptValueandMultiLuaScriptValueimplement mlua'sfrom_stackhooks, so strings are read in place and tables are walked with the raw API (lua_next,lua_rawiteron Luau) without creating a Lua reference per string or table. Functions and userdata convert as before. After #717, half of a table's conversion was mlua creating those references.lua_nextraises an error when the table changed under it, which a__gcfinalizer can do: converting an entry may allocate (a number key turned into a string) and so run a collection. Tables are therefore iterated inside a protected call, one per table (asTable::for_eachdoes), so the error becomes a conversion error instead of jumping over the Rust frames of the conversion:lua_pcallofiterate_table, pushing a C function without upvalues allocates nothing there.Lua::exec_rawmakes the call.lua_rawiterraises no errors.Same results on every variant: a test converts the same values both ways, and a table changed by a finalizer during its conversion is an error, on lua54, luau, lua51 and luajit.
Prebuilt binaries, 3 alternating rounds, two tables per call:
exec_rawon every variant