SCUA

News

Where does your SCUA program's time go?

October 28, 2026

During the 0.40.0 work we doubled the number of functions the JIT compiled in a set of game simulations, and their running time moved by a few per cent, because most of it was going on work the compiler doesn't touch. This article covers how to find that out for your own program, and what to change once you know. The full version is a page in the manual, Tune a program for speed.

#Measure the real thing first

Time a run that's long enough to matter (a second or more) and as close to real use as you can make it. A loop of ten iterations tells you about startup, not about your code. Compare CPU time (user in time's output) rather than wall time, run each version several times and alternate between them. On a laptop doing other things, differences under about 5% are usually noise. And check that the output is the same before and after.

Then ask the JIT what it did:

$ scua --jit-stats app.scua
[jit] mode=on compiled=12 native-entries=48210 deopts=31 ...
[jit] refused loop in update at line 40: a while loop nested inside this one (line 44) (x1)

compiled= above zero means hot code is running natively. A refused line names a loop that stays at the slower speed and says why. Fixing one often makes that loop several times faster.

#Your code, or the runtime?

The JIT turns your loops and functions into native code. It doesn't change the cost of the built-in work they ask for: looking a key up in a table, comparing two strings, allocating a record, checking an argument against its declared type, collecting garbage. That work happens in the runtime, and it costs the same whether the code that asked for it was compiled or not.

So programs fall into two kinds:

  • Arithmetic-heavy code (decoders, physics, numeric loops, bit manipulation) spends its time in your own instructions, and getting its hot loops compiled is what counts. For the JPEG decoder in @scua/image, compiling its bit reader and decode loops took a 12-megapixel progressive decode from 5.1 s to about 3 s.
  • Data-heavy code (simulations that walk game state, rule engines, business logic) spends much of its time in the runtime. That's what we saw in the game simulations.

A quick way to tell them apart: run once more with --jit=off. If that's less than about 1.5 times slower, your time is mostly in the runtime, and the next section is where the wins are.

#Shapes that are fast

  • Records for things with fixed fields. A record has a fixed layout, so p.hp is a read at a known position that compiled code can keep in a register, and a typed parameter is checked against it in one step.
  • Read fields by name. price.sword, with the name in the code, is looked up once and remembered at that line. price[k], with the key known only at run time, is a call into the runtime that compares strings each time. Where the key comes from a fixed set, make it an enum and index an array with it:
enum Kind { Sword, Shield, Bow }
const COST = [30, 20, 25] -- in the order Kind lists them
let k = Kind.Bow
print(COST[k])
$ scua costs.scua
25
  • Arrays built with []. {} filled with t[i] = … is a hash table keyed by numbers, which none of the array fast paths apply to.
  • One kind of value per array. An array of all integers, or all floats, is stored as plain machine numbers, half the memory and read directly by compiled loops. Put a string, nil, or a float into an integer array and it falls back to the general layout for good.
  • Enums instead of string tags. state == State.Running is one comparison; state == "running" compares lengths, hashes and then bytes.
  • Constant data at the top level. A top-level const is shared by every partition, never copied, and compiled code reads its fields once, when it compiles.
  • Less allocation in the innermost loop. Every new record, array or string is work for the garbage collector later. Return two numbers rather than a two-field record from a function called millions of times, reuse a scratch array, and key by number (y * width + x) rather than by a built string.

#Helping the JIT compile your hot code

In 0.40.0 most everyday code compiles: arithmetic, comparisons, nil tests, record fields, array reads and writes, method calls and constants. These still keep a hot loop at the slower speed:

  • A loop inside another loop. The inner one compiles; move it into its own function if the outer one matters too.
  • A variable that changes kind. Compiled code is specialised for the kind of value it saw. Start a float total at 0.0, not 0, and don't let a variable be false on one path and a number on another.
  • A very long function, or many calls in one loop. Split it. Small, single-purpose functions are the easiest thing for the JIT to compile and to copy into their callers.
  • A module called through a local alias. mod.f(x) compiles; let m = mod and then m.f(x) currently doesn't.

#ARM and x86 aren't the same

Apple silicon and other ARM machines have more registers than x86-64, so a busy loop with several calls in it can compile on one and not the other, or run with more values spilled to memory on x86. Division by a constant is a cheap multiply on x86 and a fast hardware divide on ARM, and % by a power of two is a mask on both, so i % 8 beats i % 7. Measure on the machine you'll deploy on: a change worth 10% on one can be flat on the other.

#A checklist

  1. Time a representative run and keep its output to compare against.
  2. Run with --jit-stats, and fix any refused line in a loop you know is hot.
  3. Run with --jit=off. If it's not much slower, look at your data shapes.
  4. Look for strings used as tags, {} used as an array, arrays that mix kinds, and tables read by a run-time key in a tight loop.
  5. Look for allocation in the innermost loop.
  6. Re-measure after each change, and keep only the changes that moved the number.

The manual's Tune a program for speed has all of this in one place, along with the full list of what the JIT compiles in Make scripts run faster with the JIT.