Keep a tilemap with outlined solids at 60 fps in scene and play
game-maker / gate (push) Successful in 3m1s
game-maker / suite (push) Failing after 8m33s

A project with three tilemap layers, two of them with collision on
water and hill tiles, ran at 33 to 52 fps. A CPU profile showed that
almost all of the time went into the collision geometry of the map.
It was rebuilt from scratch every frame although nothing had changed.
The animated water makes the views redraw every frame, so the cost
was never skipped.

- New module tilemapMemo keeps what is derived from the cells: the
  tiles of a layer, the blocking cells, the rectangles and polygons.
  It is reused as long as the content stays the same.
- A brush stroke paints into the existing cells object. The memo
  therefore compares the content cell by cell, not the identity.
  The comparison walks two arrays in insertion order.
- Clipped outlines are kept per outline list. The model always
  replaces that list instead of changing it.
- PhysicsSystem no longer rebuilds the shapes of a tilemap each tick
  while its cells, placement and settings are unchanged. Before, it
  also built a JSON key from all world shapes every tick.
- The overlaid layers after Set tile in a run stay stable. Otherwise
  the memo would miss on every call after the first edit.

The #184 work only helped Move and raycasts, not this path.

Measured on stein2 (i5-7500T, HD 630), script time per frame:

- Scene, nothing selected: 17.7 ms, 52 fps -> 5.8 ms, 60 fps
- Tilemap selected: 26.2 ms, 38 fps -> 8.6 ms, 60 fps
- Play: 29.9 ms, 33 fps -> 8.7 ms, 60 fps

About 3 ms per frame remain. The content comparison costs roughly
60 us per 700-cell layer and runs about 40 times a frame. The mesh
geometries in TilemapField are still rebuilt every frame.

Tests:
- ./check.sh: 162 files, 2377 tests, CHECK EXIT 0 (before 2370)
- e2e 1-5 and 39-43: ALL PASSED, 289 checks
- 7 new unit tests in tests/core/tilemap-memo.test.ts
- 5 counter checks (content ignored, no memo, outline by kind only,
  physics always rebuilds, layer settings ignored) each fail their
  test. All originals are back, checked with cmp.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
adminandClaude Opus 5.5 committed 2026-09-25 12:23:18 +02:00
1 parent 021a6dcb56
commit 03e7a78c3d
10 files changed
+491 -79

No files matched your search

+30 -7
View File
@@ -8,6 +8,7 @@ import type { TilemapAsset, TilemapLayer } from './Asset';
import { apply, determinant, invert, type Mat } from './matrix';
import type { PropertyBag, Vec2 } from './Properties';
import { type Cell, cellAt, cellAtPoint, cellKey, cellOrigin, inGrid, tilemapTileSize } from './tilemap';
import { cellsVersion } from './tilemapMemo';
/**
* What a run changed on one placement: per layer id, the cells it set, `''` for one it emptied.
@@ -32,16 +33,38 @@ export function liveLayers(asset: TilemapAsset, edits: TileEdits | undefined): T
if (!edits) return asset.layers;
return asset.layers.map((layer) => {
const changed = edits[layer.id];
if (!changed) return layer;
const cells: Record<string, string> = { ...layer.cells };
for (const [key, tileId] of Object.entries(changed)) {
if (tileId) cells[key] = tileId;
else delete cells[key];
}
return { ...layer, cells };
return changed ? editedLayer(layer, changed) : layer;
});
}
/**
* Laid over once per edits and layer content: the same object each frame is what lets the tiles and
* solids worked out from it be remembered (`tilemapMemo.ts`).
*/
function editedLayer(layer: TilemapLayer, changed: Readonly<Record<string, string>>): TilemapLayer {
let byLayer = laidOver.get(changed);
if (!byLayer) laidOver.set(changed, (byLayer = new WeakMap()));
const version = cellsVersion(layer.cells);
const held = byLayer.get(layer);
if (held?.version === version && sameBesidesCells(held.layer, layer)) return held.layer;
const cells: Record<string, string> = { ...layer.cells };
for (const [key, tileId] of Object.entries(changed)) {
if (tileId) cells[key] = tileId;
else delete cells[key];
}
const edited = { ...layer, cells };
byLayer.set(layer, { version, layer: edited });
return edited;
}
/** A layer's settings (name, z, hidden …) are changed in place too, so they are compared one by one. */
function sameBesidesCells(a: TilemapLayer, b: TilemapLayer): boolean {
const keys = Object.keys(b) as (keyof TilemapLayer)[];
return keys.length === Object.keys(a).length && keys.every((key) => key === 'cells' || a[key] === b[key]);
}
const laidOver = new WeakMap<object, WeakMap<TilemapLayer, { version: number; layer: TilemapLayer }>>();
/** The asset as a run sees it: the same one when nothing was changed. */
export function liveTilemap(asset: TilemapAsset, edits: TileEdits | undefined): TilemapAsset {
return edits ? { ...asset, layers: liveLayers(asset, edits) } : asset;