Skip to content

Lower KeyedTable membership to a native Lua table - #1302

Merged
Frotty merged 2 commits into
masterfrom
feat/lua-keyed-table-membership
Sep 10, 2026
Merged

Frotty merged 2 commits into
masterfrom
feat/lua-keyed-table-membership

Conversation

@Frotty

@Frotty Frotty commented Sep 10, 2026

Copy link
Copy Markdown
Member

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 KeyedTable package 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 KeyedTable package itself lives in the tests for now; the stdlib package and a HashSet built on it land separately in WurstStdlib2, as does the benchmark against group.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 10, 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-09-10T12:38:02.257584Z 473dc4a 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: 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".

@Frotty

Frotty commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

@codex review

@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: 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".

Comment on lines +161 to +165
call.replaceBy(JassIm.ImFunctionCall(
call.attrTrace(), replacement,
JassIm.ImTypeArguments(),
call.getArguments().copy(),
false, CallType.NORMAL));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@Frotty
Frotty merged commit ee89a28 into master Sep 10, 2026
3 checks passed
@Frotty
Frotty deleted the feat/lua-keyed-table-membership branch September 10, 2026 14:46
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