RoyalIcing / Orb

Write WebAssembly with Elixir
https://useorb.dev
BSD 3-Clause "New" or "Revised" License
174 stars 1 forks source link

Tree shake functions using ets counters #28

Open RoyalIcing opened 2 months ago

RoyalIcing commented 2 months ago

Calls are two steps: there's an Elixir wrapper for the natural experience and autocomplete, and there's the call instruction.

Tree shaking aka dead code elimination is ideal for keeping the resulting modules down. If we use a number formatter but only format unsigned 32-bit ints we should not include the code for formatting floats.

Initially I thought this would require walking through the AST and detecting every call and then removing the functions we noticed were never called.

But there's an even easier way with our two layers. The Elixir call is a real call. We can have an ets table at compile time and increment a counter whenever a function is called, passing the function name as a key. So there's no need to do another pass: we have the Elixir runtime pass already.

We then loop through every function at assembly time and omit the ones that aren't in the ets table. Possibly the counters could be used for some debug output or optimisation (e.g. inlining?)