perf(render): redraw on demand and only changed cells

Rendering used to clear the whole canvas and redraw every cell on
every frame callback (~60 fps) even though the simulation ticks at
20 tps and starts paused, keeping CPU usage at ~85% while idle.

Split simulation timing out of redraw() into the event loop and
render only when displayed content can have changed (tick, painting,
pattern placement, preview movement, grid-line toggle, resize);
idle frames commit only a new frame callback. Repaint incrementally
by diffing the grid against a rendered-state snapshot per row and
redrawing just the changed cells, with damage limited to the
repainted bounding box.

Idle CPU drops to ~1% of one core; running frames render at tick
rate (~20 fps) at microsecond cost. Adds grid_copy() snapshot
helper with tests and archives the optimize-render-on-demand change
with the new on-demand-rendering spec.
This commit is contained in:
2026-11-08 20:11:55 +01:00
parent 80fa575317
commit 81a1b26842
10 changed files with 510 additions and 99 deletions
+10
View File
@@ -45,6 +45,16 @@ void grid_clear(grid_t *g) {
memset(g->cells, 0, (size_t)g->cols * (size_t)g->rows);
}
void grid_copy(grid_t *dst, const grid_t *src) {
if (!dst || !src || !src->cells) return;
if (dst->cols != src->cols || dst->rows != src->rows) {
grid_resize(dst, src->cols, src->rows);
}
if (dst->cols == src->cols && dst->rows == src->rows && dst->cells) {
memcpy(dst->cells, src->cells, (size_t)src->cols * (size_t)src->rows);
}
}
int grid_get(const grid_t *g, int c, int r) {
if (!g || !g->cells) return 0;
if (c < 0 || r < 0 || c >= g->cols || r >= g->rows) return 0;