Lower KeyedTable membership to a native Lua table - #1302
Conversation
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: 7d6c565757
ℹ️ 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".
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 473dc4a3b1
ℹ️ 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".
| call.replaceBy(JassIm.ImFunctionCall( | ||
| call.attrTrace(), replacement, | ||
| JassIm.ImTypeArguments(), | ||
| call.getArguments().copy(), | ||
| false, CallType.NORMAL)); |
There was a problem hiding this comment.
Inline keyed-table operations at their call sites
Every keyed-table call is replaced with an ImFunctionCall to an IS_NATIVE stub whose IM body is empty, so the IM inliner cannot inline it; the emitted Lua therefore still pays a helper call for each add, contains, or remove, including with -inline. This defeats the hot-path objective of replacing the original call-plus-indexes with a direct table access, and the new output-shape test only inspects the helper body rather than the caller. Lower these operations to a representation that emits the table access directly at the call site.
AGENTS.md reference: AGENTS.md:L240-L242
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Correct that a stub cannot be inlined, and I tried the fix you describe: rewrite the operations in place as ordinary IM so the inliner can reach them, using ImVarArrayAccess (which emits a bare var[index] on Lua) over the table parameter. Reads work - contains emitted return not((tbl[key] == nil)), a direct index at the call site. Writes do not: keyedTableAdd came out with an empty body, and this was without -opt, so an IM pass drops the ImSet before emission. The cause is that IM has no notion of a Lua table and this design smuggles one through an int, so an array write to an int-typed parameter is not something IM reasoning preserves. Making it inlinable properly needs a first-class IM representation for a Lua table, which is a much larger change - a new node in a .parseq sum type breaks every exhaustive matcher, per AGENTS.md section 2. Deferring that to a follow-up and keeping the stub, which is correct and consistent; raising the scope call with the author rather than shipping a half-working inline path.
Every keyed structure in the library bottoms out in the Jass hashtable natives, which take a (parent, child) pair and are emitted on Lua as a nested table plus a nil check — so a membership test costs a call and two indexes on a runtime that is already a hash table, which is the Jass compromise AGENTS.md §7 asks us not to carry into Lua. This adds a
KeyedTablepackage whose four membership operations the Lua backend rewrites to a table keyed directly by the element (return {},t[k] = true,t[k] ~= nil,t[k] = nil), leaving the ordinary Wurst body as the Jass path, where correctness matters and performance does not.Iteration is deliberately absent: enumerating a Lua table needs
pairs(), whose order depends on internal hash layout and so differs between clients and desyncs a lockstep game. Anything that must be iterated needs a separately maintained insertion-ordered array — which is what SparseSet's dense half already provides.Checks: 256 targeted tests across the Lua backend classes, 0 failures. Emitted Lua asserted to be a single index per operation and free of
pairs(/next(, plus a membership test executed under both the Jass interpreter and real Lua.Known gap: the
KeyedTablepackage itself lives in the tests for now; the stdlib package and aHashSetbuilt on it land separately in WurstStdlib2, as does the benchmark againstgroup.