Keep the frame rate up while painting a big tilemap (#347)

Painting on the 60x60 map of a project with fifty objects dropped the
editor to 20 to 39 fps. A profile of a brush stroke showed four causes,
none of them the brush itself.

- Each frame of a stroke previews the model. The asset and hierarchy
  panels then built every row anew, so every label became a new text
  and a new upload to the GPU. One run spent 46 % of the main thread
  in those uploads. ListView.setItems now keeps a row whose item draws
  the same (sameListItem) and builds only the rows that changed.
- The brush drew the whole scene on every pointer event, on top of the
  preview. Now the preview draws what the stroke changed, once a frame.
  The rectangle tool asks for one drawing a frame for its box.
- An animated tile made the view draw in the tick and the preview drew
  it again in the same frame. animate now asks just before the frame
  is drawn and stays out when the view was drawn since the tick began.
- layoutSymbols ran on every drawing and costs up to the cube of the
  symbols. SymbolLayoutMemo keeps the spots while no anchor moves.
- A painted cell made the map read every "x,y" key again, and once
  more for each of the eight neighbours of every cell. layerCells reads
  a layer once per content into an index by place. layerTiles and the
  blocking cells take their neighbours from there (neighboursIn).
  parseCellKey reads a key without split, with the same results.

The test hook sceneRedraws counts how often the scene view was drawn.

Not done: a painted cell still works out the tiles and blocking cells
of the whole map again, so that part grows with the map.

Tests:
- Brush stroke on a copy of the project (stein2, Chrome): 20 to 39 fps
  before, 47.5 after, 55 to 57 without painting. Labels drawn again
  during the stroke 2012 before, none after. Scene drawings per frame
  1.32 before, 1 after.
- One painted cell, headless: 28 to 16 ms on 60x60, 109 to 49 ms on
  the same map doubled to 120x120.
- frame-preview: 2 new, red with the old pointer redraw.
- tilemap: 3 new (key reading, index, refresh), list-ping 1 new,
  symbol-layout 1 new. 5 single reversals turned each red.
- e2e 39,40 (melektaus-atx): ALL PASSED, 107 checks (1 new, a fast
  stroke: 6 drawings in 6 frames, no label drawn again). With the row
  reuse and the pointer redraw reverted it fails: 24 drawings in
  3 frames, and the panels draw their labels again.
- Gate (melektaus-atx): CHECK EXIT 0, 260 files, 4038 tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
adminandClaude Opus 5.5 committed 2026-10-07 16:54:59 +02:00
1 parent 101b12504e
commit 4c7861b5b8
13 files changed
+397 -44

No files matched your search

+67 -14
View File
@@ -60,9 +60,14 @@ export function cellKey(x: number, y: number): string {
return `${x},${y}`;
}
/** Read without `split`: a map reads every key of a layer again after each painted cell (#347). */
export function parseCellKey(key: string): Cell | null {
const [x, y] = key.split(',').map(Number);
return Number.isInteger(x) && Number.isInteger(y) ? { x: x as number, y: y as number } : null;
const comma = key.indexOf(',');
if (comma < 0) return null;
const end = key.indexOf(',', comma + 1);
const x = Number(key.slice(0, comma));
const y = Number(end < 0 ? key.slice(comma + 1) : key.slice(comma + 1, end));
return Number.isInteger(x) && Number.isInteger(y) ? { x, y } : null;
}
export function cellAt(layer: TilemapLayer, x: number, y: number): string {
@@ -127,6 +132,61 @@ export function neighboursOf(
};
}
/**
* A layer's painted cells inside the grid, read once per content and grid size: in reading order,
* and by place. Reading the `x,y` keys again for every cell and each of its neighbours was most of
* what one painted cell cost a big map (#347).
*/
export interface LayerCells {
columns: number;
rows: number;
list: readonly { x: number; y: number; tileId: string }[];
/** Tile id by `y * columns + x`. */
at: ReadonlyMap<number, string>;
}
const indexed = new WeakMap<Record<string, string>, LayerCells & { version: number }>();
export function layerCells(props: PropertyBag, layer: TilemapLayer): LayerCells {
const columns = tilemapColumns(props);
const rows = tilemapRows(props);
const version = cellsVersion(layer.cells);
const held = indexed.get(layer.cells);
if (held && held.version === version && held.columns === columns && held.rows === rows) return held;
const at = new Map<number, string>();
for (const [key, tileId] of Object.entries(layer.cells)) {
const cell = parseCellKey(key);
if (!tileId || !cell || cell.x < 0 || cell.y < 0 || cell.x >= columns || cell.y >= rows) continue;
at.set(cell.y * columns + cell.x, tileId);
}
const list = [...at.keys()]
.sort((a, b) => a - b)
.map((i) => ({ x: i % columns, y: Math.floor(i / columns), tileId: at.get(i)! }));
const fresh = { columns, rows, list, at, version };
indexed.set(layer.cells, fresh);
return fresh;
}
/** `neighboursOf`, read from the index. */
export function neighboursIn(cells: LayerCells, x: number, y: number, tileId: string): Neighbours {
const { columns, rows, at } = cells;
const same = (dx: number, dy: number): boolean => {
const nx = x + dx;
const ny = y + dy;
return nx >= 0 && ny >= 0 && nx < columns && ny < rows && at.get(ny * columns + nx) === tileId;
};
return {
n: same(0, -1),
e: same(1, 0),
s: same(0, 1),
w: same(-1, 0),
ne: same(1, -1),
se: same(1, 1),
sw: same(-1, 1),
nw: same(-1, -1),
};
}
/** One cell as it is drawn: where it sits, which tile it is and which quarters build it. */
export interface TileDraw {
x: number;
@@ -162,21 +222,14 @@ function workOutLayerTiles(
layer: TilemapLayer,
kinds: Map<string, TileKind>,
): TileDraw[] {
const kindOf = (id: string): TileKind | null => kinds.get(id) ?? null;
const cells = layerCells(props, layer);
const tiles: TileDraw[] = [];
for (const [key, tileId] of Object.entries(layer.cells)) {
const at = parseCellKey(key);
if (!at || !tileId || !inGrid(props, at.x, at.y)) continue;
const kind = kindOf(tileId);
for (const { x, y, tileId } of cells.list) {
const kind = kinds.get(tileId);
if (!kind) continue;
tiles.push({
x: at.x,
y: at.y,
tileId,
quadrants: tileQuadrants(kind, neighboursOf(props, layer, at.x, at.y, tileId)),
});
tiles.push({ x, y, tileId, quadrants: tileQuadrants(kind, neighboursIn(cells, x, y, tileId)) });
}
return tiles.sort((a, b) => a.y - b.y || a.x - b.x);
return tiles;
}
/**
+10 -14
View File
@@ -45,9 +45,8 @@ import {
} from './tile';
import {
cellOrigin,
inGrid,
neighboursOf,
parseCellKey,
layerCells,
neighboursIn,
tilemapColumns,
tilemapRows,
tilemapSize,
@@ -334,18 +333,15 @@ function workOutBlockingCells(
asset: TilemapAsset,
tiles: Map<string, BlockingTile>,
): CellBlock[] {
const tileOf = (id: string) => tiles.get(id) ?? null;
const found = new Map<string, CellBlock>();
const found = new Map<number, CellBlock>();
for (const layer of asset.layers) {
for (const [key, tileId] of Object.entries(layer.cells)) {
const tile = tileId ? tileOf(tileId) : null;
const cells = layerCells(props, layer);
for (const { x, y, tileId } of cells.list) {
const tile = tiles.get(tileId);
if (!tile) continue;
let cell = found.get(key);
if (!cell) {
const at = parseCellKey(key);
if (!at || !inGrid(props, at.x, at.y)) continue;
found.set(key, (cell = { ...at, box: null, polygons: [] }));
}
const index = y * cells.columns + x;
let cell = found.get(index);
if (!cell) found.set(index, (cell = { x, y, box: null, polygons: [] }));
const { shape, scale } = tile;
if (!shape) {
cell.box = Math.max(cell.box ?? 0, scale);
@@ -355,7 +351,7 @@ function workOutBlockingCells(
for (const outline of shape.outlines)
cell.polygons.push(placed(outline, cell.x + at, cell.y + at, scale));
} else {
const around = neighboursOf(props, layer, cell.x, cell.y, tileId);
const around = neighboursIn(cells, cell.x, cell.y, tileId);
const quarters = tileQuadrants(shape.tileKind, around).map(
(q) => shape.quarters.get(`${q.x},${q.y}`) ?? [],
);