Repository navigation
Lua: call the enumeration natives and their getters directly - #1517
Conversation
ForGroup, ForForce, EnumItemsInRect and EnumDestructablesInRect were wrapped on Lua: each call collected the enumerated objects into a new table through a new closure, then ran the callback for each with a global override, and every GetEnum* call tested the native and then that override. Measured on the 3.0.0.24268 client, the game's Lua natives behave as on Jass, nested enumerations included: the getter answers the outer object again after an inner enumeration returns, also across kinds and in a trigger run from the callback. A Jass build, a Lua build with the wrappers and one without them record the same traces, so the natives are now called directly behind the usual inlined nil guard, and the getters are plain native calls. In the same client, 20,000 ForGroup calls over 16 units with one GetEnumUnit each took 1475 ms without the wrappers against 1957 ms with them (medians of three alternating runs), and 160,000 ForForce calls 960 ms against 1638 ms. The test runtime gets the group natives the game showed (ForGroup visits the units the group held when it was called), and two interpreter defects which the new two-backend tests exposed are fixed: the Jass interpreter made a new set of native mocks for each native name, so ForGroup and GetEnumUnit kept separate enumeration stacks and GetEnumUnit always answered null there; and GetEnumUnit outside an enumeration answered a Java null, which the IM interpreter rejects. ForGroup's mock iterates a copy, as the game does.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 875ab3f5e3
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
…upOrderAsMeasured reads GroupAddUnit is now defined, so the test's Lua exemption, which expected it to be missing, no longer held under -PallBackends. The shim keeps a group's units in creation order, as measured on the 3.0.0 client and as the interpreter's GroupProvider does, defines FirstOfGroup, BlzGroupUnitAt, BlzGroupGetSize and KillUnit, and the test runs on Lua.
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
On Lua,
ForGroup,ForForce,EnumItemsInRectandEnumDestructablesInRectwere wrapped (__wurst_ForGroupand siblings, #1161). Each call:__wurst_enum*_override.Every
GetEnum*call was a helper that called the native and then tested the override. The helpers also carriedif ForGroup == nil then return end, which AGENTS.md §7 rules out.This PR calls the natives and getters directly. The usual inlined nil guard on the handle argument stays.
What the game does
Measured on the 3.0.0.24268 client. One test map was built three times: Jass, Lua with the wrappers, and Lua without them. All three recorded the same traces for:
ForForceinsideForGroupand the reverse;EnumItemsInRectinsideForGroup;EnumItemsInRectand nestedEnumDestructablesInRect;ForGroup: an unvisited unit removed, a unit added, the current unit removed, the group cleared;TriggerEvaluateand aTriggerExecutefrom the callback;The getter answers the inner object inside a nested enumeration and the outer one again after it returns. Each getter is null outside any enumeration.
ForGroupvisits the units the group held when it was called.The one difference is the order a group visits its units: Jass and Lua disagree, but both Lua builds agree, so this change does not cause it.
Cost
Same client, three alternating runs per build. Medians, with ranges:
ForGroupover 16 units, oneGetEnumUniteachForGroupover 16 units, no getterForForceover one playerIn stock Lua 5.3, against a Lua model of the natives, the 16-unit
ForGroupcost 23.1 µs with the wrappers and 9.6 µs without. The game's VM is noisier, and its own native dispatch is a larger share of the total.ForForceis what stdlib'sexecute()and the error listeners run through, so those paths gain too.Real map output
I built a large map's Lua with this branch's compiler and with master's (
-inline -localOptimizations). With generated-name numbers normalised, 259 diff lines remain, all of these kinds:The raw diff is larger only because generated-name numbers shift.
Interpreter and test runtime
The new tests run the nested and mutated
ForGroupon all three test backends: IM interpreter, Jass interpreter and Lua. They exposed two interpreter defects:ForGroupandGetEnumUnittherefore kept separate enumeration stacks, andGetEnumUnitalways answered null there. It now keeps one set per run.GetEnumUnitoutside an enumeration answered a Java null, which the IM interpreter rejects. It now answers a Wurst null.The
ForGroupmock also iterates a copy of the group, as the game does; iterating the live set would have thrown when the callback changed the group.The Lua test runtime (
wc3shim.lua) gets the group natives with the measured behaviour. Before this, the wrapper's nil check madeForGroupsilently do nothing in Lua test runs.The interpreter's
ForForcestill runs its callback once and has noGetEnumPlayer, so the nestedForForcetest runs on Lua only.Tests
LuaBackendAuditTests.enumerationNativesAreCalledDirectlyOnLuachecks the emitted shape: no helpers, override globals or per-call table; the natives are called with the callback adapter; getters are native calls.nestedForGroupKeepsTheOuterEnumUnit, its optimised variant, andforGroupVisitsTheUnitsItHeldWhenCalledrun on all three backends.nestedForForceKeepsTheOuterEnumPlayerruns on Lua.LuaTranslationTeststhat pinned the wrapper shape are removed.LuaBackendAuditTests,LuaTranslationTests,InterpreterTests,BugTests,ClosureTestsandNewFeatureTestspass locally on Windows. Linux and macOS have not been run here.