Moving transit allocates nothing per frame: reusable positions, kept lists, and the frame probe back in testing.js

- Scratch and marker positions are a Cesium-free Vec3 (src/geo/vector.js), so writing a coordinate does not allocate.
- The dirty set and the upsert lists are kept between frames instead of rebuilt; patchDrawnIcons reuses its lookup arrays.
- measureFrames and stopFrameProbe live in src/layers/transit/testing.js again.
- Tests: 3000 moving vehicles stay under 8 KiB per frame of layer allocation; moving 500 icons allocates under 256 B per upsert.
This commit is contained in:
Sameh Khamis committed 2026-11-15 18:27:10 -08:00
1 parent 0ab31d4fb8
commit 725f2f2998
7 files changed
+359 -138

No files matched your search

+27 -11
View File
@@ -1724,6 +1724,11 @@ export function createCesiumDraw({
billboard.show = shownNow(record, placed);
}
// What `patchDrawnIcons` looks up for each item, kept between upserts so
// a set whose icons move every frame makes no lists for the move.
const patchedEntries = [];
const patchedImages = [];
/**
* `upsert`'s quick case for an icon set on billboards: every item is
* drawn, every one of `list` keeps the image it is drawn with, and
@@ -1749,26 +1754,37 @@ export function createCesiumDraw({
drawn.size !== record.items.size
)
return false;
const entries = new Array(list.length);
const images = new Array(list.length);
for (let i = 0; i < list.length; i++) {
const n = list.length;
const entries = patchedEntries;
const images = patchedImages;
let ok = true;
for (let i = 0; i < n; i++) {
const entry = drawn.get(list[i].id);
if (!entry) return false;
if (!entry) {
ok = false;
break;
}
const image = iconImage(record, list[i].image);
if (
image === undefined ||
imageKeyOf(image) !== entry.image ||
(isKeyed(image) && image.canvas !== entry.canvas)
)
return false;
) {
ok = false;
break;
}
entries[i] = entry;
images[i] = image;
}
for (let i = 0; i < list.length; i++) {
record.items.set(list[i].id, list[i]);
patchIcon(record, entries[i], list[i], images[i]);
}
return true;
if (ok)
for (let i = 0; i < n; i++) {
record.items.set(list[i].id, list[i]);
patchIcon(record, entries[i], list[i], images[i]);
}
// Held only for this pass: what it looked up is let go.
entries.fill(undefined, 0, n);
images.fill(undefined, 0, n);
return ok;
}
/**
+83
View File
@@ -0,0 +1,83 @@
import assert from 'node:assert/strict';
import inspector from 'node:inspector/promises';
import * as Cesium from 'cesium';
import test from 'node:test';
import { createCesiumDraw } from './draw.js';
// Alone in its file, so the engine has seen only these icons when it
// measures what an upsert allocates (`draw.test.mjs` covers what it draws).
/** A scene with what an icon set touches: primitives and its events. */
function fakeScene() {
const added = [];
return {
added,
postRender: new Cesium.Event(),
preRender: new Cesium.Event(),
camera: { positionWC: new Cesium.Cartesian3(2e7, 0, 0) },
frameState: { mode: Cesium.SceneMode.SCENE3D },
updateHeight: () => () => {},
getHeight: () => undefined,
primitives: {
add: (primitive) => (added.push(primitive), primitive),
remove: (primitive) => {
const at = added.indexOf(primitive);
if (at >= 0) added.splice(at, 1);
return at >= 0;
},
length: 0,
},
requestRender() {},
isDestroyed: () => false,
};
}
test('icons moved every frame are moved without lists made for the move', async () => {
const scene = fakeScene();
const part = createCesiumDraw({
scene,
report: (error) => assert.fail(error),
});
const set = part.port.icons({ id: 'icons:moved-fleet' });
const items = Array.from({ length: 500 }, (_, i) => ({
id: `v${i}`,
pos: { ...Cesium.Cartesian3.fromDegrees(i / 100, 10) },
image: 'a.png',
size: [20, 20],
color: '#33ccff',
rotationMode: 'screen',
rotation: 0,
disableDepthTest: Infinity,
meta: { i },
}));
set.set(items);
const frame = () => {
for (const item of items) item.pos.x += 0.5;
set.upsert(items);
};
for (let i = 0; i < 300; i++) frame();
const session = new inspector.Session();
session.connect();
await session.post('HeapProfiler.startSampling', {
samplingInterval: 32,
includeObjectsCollectedByMajorGC: true,
includeObjectsCollectedByMinorGC: true,
});
const frames = 100;
for (let i = 0; i < frames; i++) frame();
const { profile } = await session.post('HeapProfiler.stopSampling');
session.disconnect();
// What the upsert's own patch pass allocates, built-ins it calls included.
let bytes = 0;
(function visit(node, inPatch) {
const patch = inPatch || node.callFrame.functionName === 'patchDrawnIcons';
const ownFile =
/\/render\/cesium\/draw\.js$/.test(node.callFrame.url) ||
!node.callFrame.url;
if (patch && ownFile) bytes += node.selfSize;
for (const child of node.children) visit(child, patch);
})(profile.head, false);
// Two lists of 500 would be ~4 KB an upsert.
assert.ok(bytes / frames <= 256, `${bytes / frames} B per upsert`);
set.dispose();
});