Skip to content

Lua: give a class's own allocator as a plain function, not a class-table method - #1537

Merged
Frotty merged 2 commits into
masterfrom
lua-plain-allocators
Oct 22, 2026
Merged

Frotty merged 2 commits into
masterfrom
lua-plain-allocators

Conversation

@Frotty

@Frotty Frotty commented Oct 22, 2026

Copy link
Copy Markdown
Member

An allocation of a class with its own allocator called Class:create(). That looks the allocator up in the class table and passes the table as self, which the allocator never reads. The allocator is now a plain function, Class_create(), called directly.

The constructor's name no longer shares the class table's key space with the dispatch slots. So the loop in createMethods that renamed a constructor clashing with a slot is removed.

Measured on the optimised Lua (-inline -localOptimizations) of a large real map, counting opcodes with the bundled luac53 -l -p:

  • SELF fell from 2,245 to 0, one per allocation site.
  • The 459 allocator definitions moved from class-table writes to global writes: SETTABLE −459, SETTABUP +459, GETTABUP −459.
  • CALL, NEWTABLE and CLOSURE are unchanged.
  • The script is 7.8 KB smaller.

In stock Lua 5.3, a loop of two allocations and two destroys ran about 6% faster: median of 7 runs, 1.057 s down to 0.992 s, about 11 ns per allocation. That is the test Lua, not a measurement in the game.

Test. LuaBackendAuditTests.ownAllocatorIsAPlainFunction runs on Lua, Jass and the interpreter. It covers inheritance with array fields, an id recycled after destroy, dispatch and instanceof. It also checks that no allocator is a method of its class table, so it fails with master's translator.

Seven shape assertions in four test classes now expect _create: CompiletimeTests, LuaBackendAuditTests, LuaTranslationTests and LuaNameCompressionTests. These pass: LuaBackendAuditTests, LuaTranslationTests, LuaNameCompressionTests, CompiletimeTests, FieldIterationTests, LuaKeyedMapTests, DeterministicChecks and ClassesTests. Only Windows was run.

…ass-table method

An allocation of a class with an allocator of its own called Class:create(), which looked the
allocator up in the class table and passed the table, which the allocator never read. The
allocator is now a plain function, Class_create(), called directly. Constructor names no longer
share the class table's key space with dispatch slots, so the rename that kept them apart goes.

On a large real map's optimised Lua this removes every SELF instruction (2245 allocation sites);
in stock Lua 5.3 an allocate-and-destroy loop runs about 6% faster.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 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-22T09:05:31.811483Z 9048b14 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.

FastHashMapTests and TypeClassTests looked for a specialised class's allocator as Class:create();
it is Class_create() now.
@Frotty

Frotty commented Oct 22, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 9048b142c5

ℹ️ 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 ee2c6b6 into master Oct 22, 2026
2 checks passed
@Frotty
Frotty deleted the lua-plain-allocators branch November 25, 2026 08:54
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