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

+56
View File
@@ -310,6 +310,29 @@ export async function tilemap(t) {
JSON.stringify(await painted()),
);
// 40c-ii. The cells show while the stroke is still under way, before it is a command (#284): the
// brush writes them in place and has to say so, or the view keeps drawing the old map.
const drawnTiles = async () => (await json(`__gm.tilemap.info(${JSON.stringify(MAP_OBJECT)})`))?.tiles;
const drawnBefore = await drawnTiles();
await t.mouse('mouseMoved', from.x, from.y);
await t.mouse('mousePressed', from.x, from.y);
for (let i = 1; i <= 12; i++) {
await t.mouse('mouseMoved', from.x + ((to.x - from.x) * i) / 12, from.y, 'left', { buttons: 1 });
await t.sleep(16);
}
await settle();
const drawnHeld = await drawnTiles();
await t.mouse('mouseReleased', to.x, to.y);
await settle();
const drawnReleased = await drawnTiles();
await undo();
await settle();
check(
'the cells of a stroke are drawn while the button is still held',
drawnBefore === 0 && drawnHeld === 3 && drawnReleased === 3 && Object.keys(await painted()).length === 0,
JSON.stringify({ drawnBefore, drawnHeld, drawnReleased, left: await painted() }),
);
// 40d. The rectangle tool fills the box a drag spans.
await clickInPanel(t, '__gm.tilemap.toolAt("rect")');
await settle();
@@ -956,6 +979,39 @@ export async function tilemap(t) {
game && game.tiles === 10 && game.layers === 2,
JSON.stringify(game),
);
// 40j-ii. A frame of the run draws the game view once and leaves a still map's meshes alone
// (#284): the animated grass used to have the view drawn a second time each frame, and
// every geometry of the map built anew with each drawing. Counted in the page over the
// same frames, so a slow machine changes neither ratio.
const drawing = JSON.parse(
await t.evaluate(`(async () => {
const read = () => ({
frames: __gm.renderStats().drawn,
redraws: __gm.gameInfo().redraws,
builds: __gm.tilemap.gameInfo(${JSON.stringify(MAP_OBJECT)}).builds,
});
const before = read();
await __gm.afterFrames(40, 4000);
const after = read();
return {
frames: after.frames - before.frames,
redraws: after.redraws - before.redraws,
builds: after.builds - before.builds,
meshes: __gm.tilemap.gameInfo(${JSON.stringify(MAP_OBJECT)}).drawCalls,
};
})().then(JSON.stringify)`),
);
check(
'a frame of the run draws the game view once, not twice',
drawing.frames >= 40 && drawing.redraws > 0 && drawing.redraws <= drawing.frames + 2,
JSON.stringify(drawing),
);
check(
'and builds a mesh again only when its animated tile steps on, not with every drawing',
drawing.builds < drawing.redraws * drawing.meshes * 0.5,
JSON.stringify(drawing),
);
await t.stop();
await settle();
await mutate(`