Skip to content

Lua: call the enumeration natives and their getters directly - #1517

Merged
Frotty merged 3 commits into
masterfrom
claude/blissful-shannon-eec1ea
Oct 21, 2026
Merged

Frotty merged 3 commits into
masterfrom
claude/blissful-shannon-eec1ea

Conversation

@Frotty

@Frotty Frotty commented Oct 21, 2026

Copy link
Copy Markdown
Member

On Lua, ForGroup, ForForce, EnumItemsInRect and EnumDestructablesInRect were wrapped (__wurst_ForGroup and siblings, #1161). Each call:

  • built a new table and a closure;
  • collected the enumerated objects through the native;
  • replayed the callback for each object with a global __wurst_enum*_override.

Every GetEnum* call was a helper that called the native and then tested the override. The helpers also carried if 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:

  • an enumeration nested in another of the same kind, and in itself;
  • ForForce inside ForGroup and the reverse;
  • EnumItemsInRect inside ForGroup;
  • nested EnumItemsInRect and nested EnumDestructablesInRect;
  • a group changed during ForGroup: an unvisited unit removed, a unit added, the current unit removed, the group cleared;
  • a TriggerEvaluate and a TriggerExecute from the callback;
  • each getter outside any enumeration.

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. ForGroup visits 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:

Block With wrappers Direct
20,000 × ForGroup over 16 units, one GetEnumUnit each 1957 ms (1725–3008) 1475 ms (1301–1541)
20,000 × ForGroup over 16 units, no getter 1612 ms (1249–2062) 1287 ms (1003–1410)
160,000 × ForForce over one player 1638 ms (1185–1779) 960 ms (676–1006)

In stock Lua 5.3, against a Lua model of the natives, the 16-unit ForGroup cost 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.

ForForce is what stdlib's execute() 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 eight helper definitions are gone;
  • 34 helper call sites are now direct native calls, each enumeration behind the inlined nil guard;
  • getters are plain native calls;
  • the inliner picked different local names next to the new guards.

The raw diff is larger only because generated-name numbers shift.

Interpreter and test runtime

The new tests run the nested and mutated ForGroup on all three test backends: IM interpreter, Jass interpreter and Lua. They exposed two interpreter defects:

  • The Jass interpreter made a new set of native mocks for every native name. ForGroup and GetEnumUnit therefore kept separate enumeration stacks, and GetEnumUnit always answered null there. It now keeps one set per run.
  • GetEnumUnit outside an enumeration answered a Java null, which the IM interpreter rejects. It now answers a Wurst null.

The ForGroup mock 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 made ForGroup silently do nothing in Lua test runs.

The interpreter's ForForce still runs its callback once and has no GetEnumPlayer, so the nested ForForce test runs on Lua only.

Tests

  • LuaBackendAuditTests.enumerationNativesAreCalledDirectlyOnLua checks 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, and forGroupVisitsTheUnitsItHeldWhenCalled run on all three backends.
  • nestedForForceKeepsTheOuterEnumPlayer runs on Lua.
  • Five LuaTranslationTests that pinned the wrapper shape are removed.

LuaBackendAuditTests, LuaTranslationTests, InterpreterTests, BugTests, ClosureTests and NewFeatureTests pass locally on Windows. Linux and macOS have not been run here.

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.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-21T09:23:33.596126Z a1db790 Manual request
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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.
@Frotty

Frotty commented Oct 21, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: a1db790cfc

ℹ️ 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".

@Frotty
Frotty merged commit f764baa into master Oct 21, 2026
2 checks passed
@Frotty
Frotty deleted the claude/blissful-shannon-eec1ea branch November 25, 2026 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant