The Playdate's CPU is fast, but its 4KB instruction cache will punish your code
Dirty Optimization Secrets (C for Playdate)
A developer shares hard-won optimization tricks for Playdate C programming, learned while building a full-speed Game Boy emulator. The key insight: the Playdate's CPU is fast but memory access is slow, and its tiny 4KB instruction cache means -Os can beat -O3. The post covers custom linker scripts, placing code and data in tightly-coupled memory, and fighting the 'performance lottery' caused by cache misalignment and branch prediction.
The first thing I did to improve performance for gameboy emulation was to squeeze the 20 kb emulator core into just 2 kb by removing a giant switch table and expressing it using much less code with just a few branches. The behaviour was identical, but it was much faster as a result.
- boricj
That reminds me of some of the tricks I've used while tinkering with a voxel space rendering tech demo on the PlayStation.
The quirks of that console for this is that the CPU takes a 6 cycle stall on any read from main RAM (it's not even stall-on-use) and doesn't have a data cache. That would make it a very poor fit for this, so I had to improvise a bunch of tricks to reclaim performance:
- Made all of my hot loop fit within one function, so that it fits entirely in the 4 KiB direct-mapped instruction cache (so less than 1024 instructions)
- Wrote a C++ template that can trampoline a function call into the 2 KiB scratchpad
- Used the rest of the scratchpad for a CLUT table because it doesn't stall on read
- Used the GTE to transform the N set of coordinates while the CPU stalls fetching the N+1 set of heightmap data
- Figured ways to abuse the GTE into transforming more points per instructions than it theoretically can, taking advantage of simplified formulas compared to real 3D projection
- Write the rectangle primitives straight to the GPU registers instead of going through a display list
It's such an abuse of the PlayStation that PCSX-Redux and DuckStation disagree on its performance by up to a factor of two. I should finish it someday and see how it runs on real hardware...
- mococa
Rembers me - https://graphics.stanford.edu/~seander/bithacks.html
- nxobject
The tips about laying out and carefully segmenting code are a blast from the past!