The Pokemon Sylveon

Skylie Ivory

kaleidosphere

by Kate, published

Kaleidosphere is a program that aims to improve the performance of Lua on the web through ahead-of-time compilation.

Transcription of original poster into web form. Full blog post soon maybe.

Introduction

Lua and WebAssembly are both popular virtual machines commonly embedded into a larger piece of software to extend its capabilities. While Lua might be fast on it’s own, after compiling it’s virtual machine to WebAssembly, a lot of performance is lot. Prior work suggests that we can analyze the static types of Lua code to generate a binary that can maintain the performance of natively running Lua.

Design

Our solution is built off the idea that Lua (and most dynamic languages) maintain a fair level of typed-consistency. A well-designed analyzer is able to trace through a program to determine the typings of variables, along with the overall call flow.

We take Lua code, lower it into an analyzable intermediate representation, analyze the types of the program, before compiling to WebAssembly. To analyze the types of the program, we trace the known values, such as constants, through their usages to uncover more types. For example:

local function iterative_fib(n)
   local p0, p1 = 0, 1
   for _ = 1, n do
      p0, p1 = p1, p0 + p1
   end
   return p0
end

In this program, we can analyze p0 and p1 to be integer values due to their constant declaration, and the only operations that are performed on them being addition. Then, we can infer n to be an integer as a for loop limit must be an integer.

Implementation

We choose to represent Lua in single-static assignment (SSA) form to allow for more classes of analysis and optimization. We then propagate types through the SSA, propagating known values to their users. At phi nodes, we calculate the least-upper bounds of the operands. For code generation, we make use of the WebAssembly Garbage Collector proposal for both garbage collection and as a means of maintaining a typed heap.

We can rewrite the loop body of iterate_fib from:

p0, p1 = p1, p0 + p1

Into:

v17 = phi(.0: v1, .2: v15) :: int
v15 = phi(.0: v2, .2: v11) :: int
v11 = add v17, v15 :: int

Then, we can rewrite our SSA form directly into WebAssembly:

(local.set 5
  (i64.add (local.get 1) (local.get 2)))
(local.set 1 (local.get 2))
(local.set 2 (local.get 5))

Evaluations

For our evaluations, we chose to compare Kaleidosphere to Lua 5.5.1 compiled to wasm-wasip1 (wasm-lua). We set a baseline score of 1.0 for Lua 5.5.1, compiled to x86_64, where 2 is twice the time taken and 0.5 is half the time. Tests are ran in Node v26.7.0. Benchmarks are sourced from the Computer Language Benchmarks Game.

00.511.522.533.54binarytreemerkletreesspectral-normRelative Speedwasm_luakaleidosphere -O0kaleidosphere -O3

In some cases, Kaleidosphere is able to not only be faster than wasm-lua, but be faster than baseline native Lua!

Future Work

  • Just-In-Time compilation through lazy basic block versioning and host-environment support
  • Inline Caches to reduce the overhead of type checks
  • Global Analysis to provide inter-function analysis

Conclusion

Ahead of time compiling Lua to WebAssembly can help reduce or eliminate the overhead of it’s virtual machine, while still providing a cross-platform and safe environment.

Acknowledgements

  • Kate - #1 bug fixer
  • Eryn Raine - analyzer-er
  • Aurora - #1 bug causer
  • Kori - anyref user, parser-er
  • Sylvie - codegen-er