hn.today

Dirty Optimization Secrets (C for Playdate)

devforum.play.date49 points6 comments
Screenshot of Dirty Optimization Secrets (C for Playdate)

Practical, low-level optimization techniques for C on Playdate aimed at extreme workloads like emulators, sims, or renderers. Key observation: the CPU is fast but memory (especially non-cached accesses) is slow, so code that stays in registers and minimizes loads runs much faster. Instruction cache is tiny (Rev A ~4 KB, Rev B ~16 KB), so smaller, denser core loops can beat -O3-produced larger code; using -Os and compressing a 20 KB emulator core down to ~2 KB produced large speedups. Keep hot core paths contiguous in memory (use attribute ((section(".text.foo"))) on core functions) and move rare-edge handlers out of the hot section to preserve icache residency and branch locality.

Concrete tooling and practices: use a custom linker script (copy link_map.ld and set override LDSCRIPT in your makefile/CMake) to place object files and align sections precisely, and inspect symbol addresses/sizes with nm Source/pdex.elf | sort to verify icache fit. Real-world results include a reported 20-30% slowdown from adding unused C functions (icache/size effects) and a surprising speed gain when replacing integer divisions in a tight loop with float additions; that latter case likely benefitted from freeing integer registers (float registers reduced register pressure) rather than raw division cycle counts.

Read on devforum.play.date6 comments on Hacker News

Summary generated by AI from the linked article. hn.today is not affiliated with Hacker News or Y Combinator.

More in Programming

The daily digest

Today's best Hacker News stories, summarized and screenshotted, one email a day.