Halve the frame time of a playing scene with a tilemap (#284)

The performance panel showed Physics and Editor as the large parts of
a frame in a scene of 222 entities. Nearly all of it came from a
tilemap that only stands there.

- Whoever writes cells in place now says so with cellsChanged. Only
  then does cellsVersion compare the content. The comparison ran about
  60 times per frame and was the largest single item at 14 percent.
- The four writers call it: the painter during and at the end of a
  stroke, PaintTilesCommand on execute and on undo. A new writer that
  forgets it stays invisible.
- The game view is drawn once per frame in play. With an animated tile
  in view it was drawn twice, once by the run and once by animate().
  animate() now gets the redraw count from the start of the tick.
- TilemapField builds the geometry of a mesh only when its source
  changes: the cells, the tile size, the origin or the image. For that
  groupByTile and layerRows return the same objects for the same list.
- PhysicsSystem skips a tilemap whose source and switch are unchanged.
  The switch is kept because a map that starts switched off would
  otherwise have dead colliders once it is switched on.
- World.syncToModel looks entities and assets up in a map per pass
  instead of a linear search per object.

Measured on stein2 with a copy of the reference project, ms per frame:
Physics 2.64 to 0.97, Drawing 6.89 to 2.56, Editor 1.95 to 1.31.

Left open: syncToModel still mirrors every entity every frame. That
is its own issue.

Tests:
- Gate: 198 files, 2887 tests (2883 + 4), exit 0
- e2e 1-17,28,33,39-43,59: 810 checks passed, 40 has +3
- Smokes: 24 / 19 / 21, exit 0
- Three unit tests wrote cells directly and now call cellsChanged
- Eleven counter-probes fail. Two did not at first: the stroke needed
  a check of its own, the physics test has to start with the map
  switched off. That last one was repeated on stein2 only.
- e2e total 1613 is computed, not measured in a full run

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
adminandClaude Opus 5.5 committed 2026-10-01 09:52:57 +02:00
1 parent 6491888f56
commit cfd288c4b7
15 files changed
+303 -43

No files matched your search

+19 -4
View File
@@ -4,8 +4,10 @@
* again each time is what made a map with outlined tiles cost a whole frame.
*
* The cells are painted in place, so an object being the same says nothing about its content.
* `cellsVersion` compares the content with what it saw last and hands out a new number only when it
* differs – a number that then stands for exactly that content in a cache key.
* Whoever writes into them says so with `cellsChanged`; `cellsVersion` then compares the content
* with what it saw last and hands out a new number only when it differs – a number that stands for
* exactly that content in a cache key. Without that word nothing is compared: a map is asked some
* sixty times a frame, and walking every cell each time was the largest single cost of a run (#284).
*/
interface Seen {
@@ -13,6 +15,8 @@ interface Seen {
values: string[];
version: number;
tileIds: string[];
/** Written to since it was looked at. */
stale: boolean;
}
const seen = new WeakMap<Record<string, string>, Seen>();
@@ -33,14 +37,25 @@ function unchanged(cells: Record<string, string>, held: Seen): boolean {
function look(cells: Record<string, string>): Seen {
const held = seen.get(cells);
if (held && unchanged(cells, held)) return held;
if (held && !held.stale) return held;
if (held && unchanged(cells, held)) {
held.stale = false;
return held;
}
const keys = Object.keys(cells);
const values = keys.map((key) => cells[key]!);
const fresh = { keys, values, version: ++versions, tileIds: [...new Set(values)].filter(Boolean) };
const tileIds = [...new Set(values)].filter(Boolean);
const fresh = { keys, values, version: ++versions, tileIds, stale: false };
seen.set(cells, fresh);
return fresh;
}
/** Says that cells were written to in place; a replaced object needs no word, it is a new one. */
export function cellsChanged(cells: Record<string, string>): void {
const held = seen.get(cells);
if (held) held.stale = true;
}
/** A number that is the same exactly as long as the cells hold the same tiles in the same places. */
export function cellsVersion(cells: Record<string, string>): number {
return look(cells).version;