Balloon Pop in Clojure
Balloon Pop is also written in Clojure, on jolt, and runs on the Wii through the same pipeline as jolt-hello. The game plays. It also misses the 60 fps budget on most frames, and this page records why, with numbers. Every number here comes from Dolphin 2609a with MMU emulation on. The game has since been played on the family Wii (2026-11-06), but nothing was measured there, so treat the numbers as Dolphin's, not the Wii's.
| Title | Play (robot demo) | Results |
|---|---|---|
![]() | ![]() | ![]() |
![]() | ![]() | ![]() |
The top row is the C game from Balloon Pop, the bottom row the Clojure one. The rules and the drawing are the same on purpose.
Trying it
jolt build:jolt-balloons
jolt run:jolt-balloons # robot demo, prints the events
jolt run:jolt-balloons-wiimote # a scripted Wiimote plays a round
jolt play:jolt-balloons # you play, with the Mac's mouse as the pointer
jolt compare:balloons # C and Clojure side by side, one table
jolt play:jolt-balloons works like jolt play:balloons: left click is A, Return is HOME. The Clojure game takes about 7 seconds to show its first frame.
How it's built
flowchart TD rules["balloons.rules<br/>pure Clojure: state in, state out"] --> main["balloons.main<br/>frame loop"] draw["balloons.draw"] --> main main --> gfx["wii.gfx<br/>one ffi/defcfn per C function"] gfx -->|"uintptr_t jg_xxx(uintptr_t ...)"| shim["jolt-gfx/jolt_gfx.c<br/>registered at link time"] shim --> raylib["raylib 6.0<br/>SDL2, OpenGX"] shim --> wpad["libogc WPAD<br/>IR, buttons, rumble"]
The game is jolt-balloons/src/balloons/{rules,draw,main}.clj. rules.clj is plain immutable maps and vectors: one function takes the old state and the remotes' input and returns the new state, so it runs on the Mac under jolt -A:test. draw.clj turns a state into raylib calls and main.clj runs the loop and logs the same event lines the C game does, with a jolt-balloons: prefix.
jolt-gfx/ is the glue. wii.gfx (jolt-gfx/src/wii/gfx.clj) declares one defcfn per C function in jolt-gfx/jolt_gfx.c, and wii.frame-stats prints the timing line both games emit every 120 frames.
Why the glue looks the way it does
Each piece answers something the first spike hit.
- Every C function is
uintptr_t jg_xxx(uintptr_t ...), at most 7 arguments. Chez on the Wii runs portable bytecode with no libffi, so it can only call shapes from a fixed table. A:stringargument next to a:uptrfailed withforeign-procedure: protocol not supported (libffi unavailable). Strings go over as the address fromffi/string->ptr, and negative numbers go as two's complement and come back through(int).jolt-gfx/jolt_gfx.hhas the full rule. - Registration happens at link time. There is no
dlsymon the Wii, sojolt_gfx.cregisters every function withSforeign_symbolfrom insideSbuild_heap's init, and the graphics pack links with-Wl,--wrap=Sbuild_heap(scripts/make-gfx-pack). - SDL's
mainhas to run first. jolt generates its ownmain.scripts/wii-cc-sdlcompiles with-Dmain=SDL_main, so devkitPro's SDL wrapper runs first and callsWPAD_Init. It is the same bug Balloon Pop hit in C. - Values stay under 229. A 32-bit Chez fixnum is 30 bits. The shim masks what it returns, and the IR pointer comes back biased by 4096 (
JG_IR_BIAS) so a pointer slightly off screen is not a negative number.
The shim has since grown for Super Mona in jolt: sprite sheets embedded as PNG bytes and jg_sprite (with in-place flips and a 4x scale), tile layers (jg_tiles_load, jg_tile_set, jg_tiles_draw, and jg_tiles_map for a minimap), the call counter jg_calls, and a settings bridge (jg_setting_get, jg_setting_put) over examples/common/settings.c. That page shows how they are used.
The graphics pack is the plain pack minus the text console, plus the shim, raylib and SDL: jolt gfx-pack. Whether to keep this link-time registration or teach jolt's cross builds about :jolt/native is still open (see ROADMAP.md).
The measurement
The first Clojure draw was slow, so the work went into finding out why. The raw numbers and every run are in jolt-balloons/MEASUREMENTS.md.
1. A busy frame
balloons.stress draws a busy frame: 4 players aiming, 24 balloons, 100 particles. That is 368 draw calls per frame. Steady state was a draw median of about 253 ms and a work median of about 269 ms, against a 16.7 ms frame. Draw was 94% of the work, about 0.69 ms per call.
2. Why there is no command buffer
The obvious fix is to batch the 368 calls into a buffer and cross once. Before building it, balloons.ffi-bench (jolt run:jolt-ffi-bench) timed the pieces.
| Operation | Per op |
|---|---|
defcfn call, no arguments | 3.9 us |
defcfn call, 5 fixnum arguments | 18.0 us |
ffi/write of one :int32 | 48.8 us |
write-array :int32, per element | 89.0 us |
conj onto a persistent vector | 22.4 us |
One ffi/write costs 2.7 times as much as a whole 5-argument call. A buffer of 5 fields per shape would cost about 244 us per shape against 18 us for the direct call. So the per-shape calls stay.
3. Where the time goes
Three builds of the same busy frame split the draw time:
| Part | Time per frame |
|---|---|
| Clojure compute | 232 ms |
| The FFI crossing, 368 calls | 7 ms |
| raylib and OpenGX | 24 ms |
Of the 252 ms draw, about 232 ms was Clojure computing the shapes. The crossing and raylib together were about 31. The figures don't sum exactly (a 5% gap, probably from GC timing), but they are small next to 232. The first guess, that the missing time sat in the C draw functions, was wrong.
4. The hot spot: (long x)
balloons.draw-profile timed single operations on the Wii. A keyword lookup costs 2.4 us and Math/sin 11.7 us. (long x) on a double costs 135 us.
wii.gfx cast every coordinate with long, which makes 1324 double casts in a busy frame. On a 32-bit Chez the checked cast compares the double against +-263, and those bounds are bignums. A ^double hint changes nothing (135.6 us against 135.5 us). unchecked-long skips the comparison and costs 26 us.
Switching the coordinate casts to unchecked-long took the draw from 252 ms to 60 ms, 4.2 times faster. The picture didn't change, and two tests pin that: a golden test of the 1129 draw calls for 13 states (jolt-balloons/test/balloons/draw_test.clj), and casts-agree-on-every-recorded-argument, which checks that every double argument those states draw gives the same value under both casts. One behaviour differs for values outside the tested range: unchecked-long saturates an out-of-range double and turns NaN into 0, where long throws.
5. The real game
The stress frame is a worst case. jolt compare:balloons runs both real games for a 45 second robot demo and for a scripted round (64.5 seconds for C, 70 for Clojure), drops each run's first summary line, and prints one table.
| C, demo | Clojure, demo | C, round | Clojure, round | |
|---|---|---|---|---|
.dol size (bytes) | 1339808 | 14478048 | 1339808 | 14478048 |
| Summary lines used | 21 | 12 | 31 | 22 |
| Work median (us) | 3882 | 24167 | 3122 | 22711 |
| Work worst, max (us) | 5557 | 41086 | 5144 | 42474 |
| Frame worst, max (us) | 16671 | 61977 | 16672 | 61347 |
| Dropped frames, total | 0 | 1003 | 0 | 2173 |
Each summary line covers 120 frames, so the Clojure round dropped 2173 frames across 22 summaries. A frame counts as dropped when its interval passes 20000 us. The Clojure game works through a frame in 23 to 24 ms (the two work medians are 24167 and 22711 us) where C needs 3 to 4 ms. That is over the 16.7 ms budget, and the dropped counts above are what was measured: the Clojure game drops most frames in both runs, and C drops none. The .dol is about 14.48 MB.
Lines of code, from wc -l:
| Lines | |
|---|---|
C game (examples/balloons/{game.h,game.c,main.c}) | 747 |
Clojure game (jolt-balloons/src/balloons/{rules,draw,main}.clj) | 602 |
Glue the Clojure game needs, C (jolt_gfx.c, jolt_gfx.h) | 262 |
Glue the Clojure game needs, Clojure (gfx.clj, frame_stats.clj) | 131 |
The game itself is shorter in Clojure. The glue it needs is 393 lines on top of that, and a second Clojure game could use it unchanged.
6. The cost of pure rules
The rules are pure on purpose: no mutable arrays, atoms or volatiles in balloons.rules, even to win frames. That was a decision, and its price is recorded here, not engineered away. One update of a real board takes about 6 us in C and about 4 ms in Clojure. On the busy stress frame the update takes 15.5 ms, just under a 16.7 ms frame, and the draw 60 ms after the cast fix, so the draw, and the two together, are over budget.
Levers that remain, all estimates in MEASUREMENTS.md: a cheaper double-to-fixnum truncation (a saving of 15 to 20 ms from a 48 ms draw), caching C strings for static text (about 1 ms), and a sine lookup table (about 1.7 ms). None is done.
What this taught us about jolt
- The checked cast is slow on 32 bits.
(long x)on a double is 135 us because it compares with bignum bounds. Useunchecked-longwhere the value is known to be in range. ffi/writeis expensive. One:int32write costs 2.7 times a whole 5-argument call, so batching through memory loses to calling directly.- No libffi means a fixed table of call shapes. Anything outside the table fails, and
:stringis one of them. That is why every shim function isuintptr_tin and out, with at most 7 arguments (jolt-gfx/jolt_gfx.h). - No
dlsym, so registration is link-time. The-Wl,--wrap=Sbuild_heaptrick registers the shim's functions as the heap builds. - SDL's
mainmust run first.-Dmain=SDL_mainrenames jolt'smain, so SDL's wrapper can initialise the Wiimotes. - The GC trips too late. jolt sizes its allocation window at 16 MB, and the first runs died with "out of memory". The shim's constructor sets
JOLT_GC_TRIP_BYTESto 2097152. - Fixnums are 30 bits. Anything above 229 is a bignum. Colours (
0xRRGGBBAA) are bignums, which costs about 2 us per call, and the IR position is biased by 4096 so an off-screen pointer isn't negative. - The heap is nearly full before the program starts. A jolt program's runtime holds about 41 MB live on the Wii (58 MB at the boot peak) in a heap of about 60 MB: what MEM1 has left after the
.dolloads, plus MEM2. What looked like a.dolsize cliff at about 14.76 MB was that heap running out; a build a little bigger, or one that keeps a little more data, dies with "out of memory" before its first line. The shim wrapsmallocin the pack's link and serves the overflow from what libogc's sbrk strands in both arenas (3.4 MB), and prints the heap's state atjg_initto Dolphin's log. The numbers are inMEASUREMENTS.md.
What's next
The real console, first. Then the open decision: keep the link-time glue or teach jolt build --target about :jolt/native archives. See ROADMAP.md.





