hn.today

Beating the Compiler

mattkeeter.com4 points0 comments
Screenshot of Beating the Compiler

A hands-on exploration of building a high-performance interpreter for the Uxn stack CPU, comparing a Rust implementation to a hand-written AArch64 version. The Rust build inlines and monomorphizes opcode functions and keeps some values in registers, yielding modest gains over a reference implementation, but disassembly and profiling reveal two key inefficiencies: several VM state values remain in memory (extra loads/stores) and the compiler emits a single indirect dispatch via a jump-table offset that makes the branch highly unpredictable and expensive. The dispatch ldrh and indirect branching account for a large share of runtime.

To address those costs, a complete AArch64 interpreter was written that keeps all critical VM state in registers and uses indirect threading (a table of opcode function pointers) so each opcode ends by jumping directly to the next implementation. The design documents exact register assignments (stack pointers, indices, RAM pointer, PC, VM and device handles), a macro-driven next dispatch sequence, and helper macros to generate common opcode patterns; the assembly runs to about 2,400 lines. Practical constraints of the ABI are handled with a C-compatible EntryHandle struct passed from Rust into the assembly entry point. The result is a low-level interpreter that eliminates many memory accesses and the central unpredictable dispatch, yielding a substantially more targeted execution model than the compiler-produced code.

Read on mattkeeter.com0 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.