-
Notifications
You must be signed in to change notification settings - Fork 28
Lower KeyedTable membership to a native Lua table #1302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
...rstscript/src/main/java/de/peeeq/wurstscript/translation/imtranslation/LuaKeyedTable.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package de.peeeq.wurstscript.translation.imtranslation; | ||
|
|
||
| import de.peeeq.wurstscript.CompilerIntrinsics; | ||
| import de.peeeq.wurstscript.ast.FuncDef; | ||
| import de.peeeq.wurstscript.jassIm.ImFunction; | ||
| import de.peeeq.wurstscript.jassIm.ImVoid; | ||
| import de.peeeq.wurstscript.types.TypesHelper; | ||
|
|
||
| /** | ||
| * Recognises the compiler-owned {@code KeyedTable} membership operations. | ||
| * | ||
| * <p>Jass has no hashing, so every keyed structure in the library bottoms out in the hashtable | ||
| * natives. Those 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 - a Jass compromise carried into Lua, which AGENTS.md section 7 asks us not to do. On Lua | ||
| * each keyed table is its own table and the element is the key, so each operation is one index. | ||
| * | ||
| * <p>Matching is by <b>declaration</b>, not by name: a function qualifies only if it is annotated | ||
| * {@code @compilerintrinsic} and its IM signature is exactly the one the lowering assumes. | ||
| * Name-only matching would silently replace the body of any user function that happened to share | ||
| * the name, including one with different types, which section 7 rules out - semantic identity must | ||
| * come from the declaration and its structural signature, never from string comparison. | ||
| * | ||
| * <p><b>Iteration is deliberately absent.</b> Enumerating a Lua table needs {@code pairs()}, whose | ||
| * order follows internal hash layout and so differs between clients, which desyncs a lockstep | ||
| * game. Anything that must be iterated needs a separately maintained insertion-ordered array - | ||
| * what SparseSet's dense half provides. This primitive is membership only. | ||
| */ | ||
| public final class LuaKeyedTable { | ||
|
|
||
| /** Source-level names of the operations. Necessary to identify them, never sufficient. */ | ||
| private static final String CREATE = "keyedTableCreate"; | ||
| private static final String ADD = "keyedTableAdd"; | ||
| private static final String CONTAINS = "keyedTableContains"; | ||
| private static final String REMOVE = "keyedTableRemove"; | ||
|
|
||
| /** Stub names whose Lua bodies live in {@code LuaNatives}. */ | ||
| public static final String NATIVE_CREATE = "__wurst_keyedTableCreate"; | ||
| public static final String NATIVE_ADD = "__wurst_keyedTableAdd"; | ||
| public static final String NATIVE_CONTAINS = "__wurst_keyedTableContains"; | ||
| public static final String NATIVE_REMOVE = "__wurst_keyedTableRemove"; | ||
|
|
||
| private LuaKeyedTable() { | ||
| } | ||
|
|
||
| /** | ||
| * The {@code __wurst_} stub {@code f} should be lowered to on Lua, or null if {@code f} is not | ||
| * a compiler-owned KeyedTable operation. | ||
| */ | ||
| public static String nativeStubFor(ImFunction f) { | ||
| if (!(f.attrTrace() instanceof FuncDef fd) | ||
| || !fd.attrHasAnnotation(CompilerIntrinsics.ANNOTATION)) { | ||
| return null; | ||
| } | ||
| int params = f.getParameters().size(); | ||
| return switch (fd.getName()) { | ||
| case CREATE -> params == 0 && TypesHelper.isIntType(f.getReturnType()) | ||
| ? NATIVE_CREATE : null; | ||
| case ADD -> isKeyedPair(f) && f.getReturnType() instanceof ImVoid | ||
| ? NATIVE_ADD : null; | ||
| case REMOVE -> isKeyedPair(f) && f.getReturnType() instanceof ImVoid | ||
| ? NATIVE_REMOVE : null; | ||
| case CONTAINS -> isKeyedPair(f) && TypesHelper.isBoolType(f.getReturnType()) | ||
| ? NATIVE_CONTAINS : null; | ||
| default -> null; | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * A (table, key) parameter pair. Both are {@code int} at source level: on Jass everything is an | ||
| * integer anyway, and on Lua {@code castTo int} is the identity for class types, so the value | ||
| * arriving here is already a usable table key with reference identity. | ||
| */ | ||
| private static boolean isKeyedPair(ImFunction f) { | ||
| return f.getParameters().size() == 2 | ||
| && TypesHelper.isIntType(f.getParameters().get(0).getType()) | ||
| && TypesHelper.isIntType(f.getParameters().get(1).getType()); | ||
| } | ||
| } |
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
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
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Every keyed-table call is replaced with an
ImFunctionCallto anIS_NATIVEstub 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.
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.