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

+74
View File
@@ -1,6 +1,7 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import * as Cesium from 'cesium';
import inspector from 'node:inspector/promises';
import { createTransitLayer } from './index.js';
import { spriteDrawOrder } from '../../data/spriteOrder.js';
import {
@@ -3727,6 +3728,79 @@ test('intercepted scripted reports resolve their explicit floor and wake without
assert.equal(app.state()._qaFixtureFloors.size, 0);
});
/** Source files whose allocations are the layer's own, as the fleet QA reads them. */
const LAYER_SOURCE =
/\/layers\/transit\/|\/data\/(?:contactPlayback|contactTrailRenderer|transit[^/]*)\.js/;
/**
* Bytes per frame the layer's own code allocates over `frames` frames: what
* its functions allocate, and what the engine's built-ins (a set or array
* growing) allocate for them. Sampled, collected objects included.
*/
async function layerFrameBytes(h, frames) {
const session = new inspector.Session();
session.connect();
try {
await session.post('HeapProfiler.startSampling', {
samplingInterval: 32,
includeObjectsCollectedByMajorGC: true,
includeObjectsCollectedByMinorGC: true,
});
for (let i = 0; i < frames; i++) {
h.advance(16);
h.frame();
}
const { profile } = await session.post('HeapProfiler.stopSampling');
let bytes = 0;
const sites = new Map();
const layer = (url) => LAYER_SOURCE.test(url) && !url.includes('.test.');
(function visit(node, parentUrl) {
const { url, functionName, lineNumber } = node.callFrame;
const own = layer(url) || (!url && layer(parentUrl));
if (own && node.selfSize) {
bytes += node.selfSize;
const site = `${functionName || '(anonymous)'} ${url || parentUrl}:${lineNumber + 1}`;
sites.set(site, (sites.get(site) || 0) + node.selfSize);
}
for (const child of node.children) visit(child, url || parentUrl);
})(profile.head, '');
return { perFrame: bytes / frames, sites: [...sites] };
} finally {
session.disconnect();
}
}
test('a moving fleet frame allocates next to nothing in the layer', async (t) => {
// The fleet QA's size: the engine optimises a frame of it as it would in
// the app (a smaller fleet's frame can stay in a tier that allocates).
const FLEET = 3000;
const h = harness(t, { altitude: 600 });
await h.layer.enable(h.viewer);
h.layer._loadTransitFleetForTest(FLEET, BOSTON, 120);
// Warm: the engine optimises the frame path before it is measured.
for (let i = 0; i < 300; i++) {
h.advance(16);
h.frame();
}
assert.equal(h.state()._moving.size, FLEET);
// What the draw port does with the markers is its own (draw.test.mjs):
// here each push is only counted, so none of its work is counted as the
// layer's where the engine runs the two as one.
let pushed = 0;
for (const set of [h.state()._stillIcons, h.state()._movingIcons])
set.upsert = (list) => {
pushed += list.length;
};
const frames = 120;
const { perFrame, sites } = await layerFrameBytes(h, frames);
assert.equal(h.state()._moving.size, FLEET);
assert.equal(pushed, FLEET * frames, 'every marker moved every frame');
// Every vehicle moves every frame; what the layer allocates must not
// grow with them: the fleet QA's gate, 8 KiB a frame (a position boxed
// per vehicle would be ~100 KiB).
assert.ok(perFrame <= 8192, JSON.stringify({ perFrame, sites }));
});
test('hot path reuses sample outputs and reads camera altitude once per frame', async (t) => {
const h = harness(t);
await h.layer.enable(h.viewer);
+45 -123
View File
@@ -1,5 +1,4 @@
import {
canvasOf,
drawOf,
findCamera,
findFrame,
@@ -9,6 +8,7 @@ import {
import { poseSignature } from '../../render/camera/poseSignature.js';
import { heightsOf } from '../../queries/heights.js';
import { fromDegreesInto } from '../../geo/ellipsoid.js';
import { Vec3 } from '../../geo/vector.js';
import { frustumPlanes } from '../../geo/frustum.js';
import { perspectiveCourseRotation } from '../../data/screenRotation.js';
import {
@@ -74,9 +74,32 @@ export function createRendering({ state, services, parts }) {
const sampleMeshFloorCells = services.mesh?.sampleMeshFloorCells || null;
const instance = (instances += 1);
/** Markers changed since they were last pushed to their sets. */
const dirty = new Set();
// Kept between pushes, so a push allocates no lists.
// Markers changed since they were last pushed to their sets, in the order
// first changed: a list kept between pushes (a set emptied and filled
// again every frame allocates its table again), each entry holding its
// place in it (`markerDirtyAt`; -1, or none, when not listed).
const dirtyList = [];
let dirtyCount = 0;
const dirty = {
add(entry) {
if (entry.markerDirtyAt >= 0) return;
entry.markerDirtyAt = dirtyCount;
dirtyList[dirtyCount++] = entry;
},
delete(entry) {
entry.markerDirtyAt = -1;
},
clear() {
for (let i = 0; i < dirtyCount; i++) {
const entry = dirtyList[i];
if (entry.markerDirtyAt === i) entry.markerDirtyAt = -1;
dirtyList[i] = undefined;
}
dirtyCount = 0;
},
};
// Kept between pushes, at the length the last push gave them, so a push
// of as many markers as the last allocates no lists.
const stillUpserts = [];
const movingUpserts = [];
const stillRemovals = [];
@@ -231,26 +254,34 @@ export function createRendering({ state, services, parts }) {
const still = state._stillIcons;
const moving = state._movingIcons;
if (!still || !moving) return;
if (!dirty.size && !stillRemovals.length && !movingRemovals.length) return;
for (const entry of dirty) {
if (!entry.marker) continue;
if (!dirtyCount && !stillRemovals.length && !movingRemovals.length) return;
let stillCount = 0,
movingCount = 0;
for (let i = 0; i < dirtyCount; i++) {
const entry = dirtyList[i];
// Taken out of the list (or listed again later) since it was put here.
if (entry.markerDirtyAt !== i || !entry.marker) continue;
const target = entry.animated ? moving : still;
if (entry.drawnIn && entry.drawnIn !== target)
(entry.drawnIn === still ? stillRemovals : movingRemovals).push(
entry.key,
);
entry.drawnIn = target;
(target === still ? stillUpserts : movingUpserts).push(itemOf(entry));
if (target === still) stillUpserts[stillCount++] = itemOf(entry);
else movingUpserts[movingCount++] = itemOf(entry);
}
dirty.clear();
// Emptied and filled again, a list allocates its storage again: each
// is cut only to what this push has.
if (stillUpserts.length !== stillCount) stillUpserts.length = stillCount;
if (movingUpserts.length !== movingCount)
movingUpserts.length = movingCount;
if (stillRemovals.length) still.remove(stillRemovals);
if (movingRemovals.length) moving.remove(movingRemovals);
if (stillUpserts.length) still.upsert(stillUpserts);
if (movingUpserts.length) moving.upsert(movingUpserts);
if (stillCount) still.upsert(stillUpserts);
if (movingCount) moving.upsert(movingUpserts);
stillRemovals.length = 0;
movingRemovals.length = 0;
stillUpserts.length = 0;
movingUpserts.length = 0;
}
/**
@@ -664,7 +695,8 @@ export function createRendering({ state, services, parts }) {
function addMarker(key, entry) {
const at = entry.toCart;
const marker = {
position: { x: at?.x ?? 0, y: at?.y ?? 0, z: at?.z ?? 0 },
// Moved every frame in place: a `Vec3`, so a move allocates nothing.
position: new Vec3(at?.x ?? 0, at?.y ?? 0, at?.z ?? 0),
show: false,
image: transitIcon(entry.mode),
width: MARKER_PIXEL_SIZE,
@@ -1183,114 +1215,6 @@ export function createRendering({ state, services, parts }) {
flushMarkers();
}
let stopProbe = null;
/** Stop a running frame probe, resolving it with what it has. */
function stopFrameProbe() {
stopProbe?.();
}
/**
* Measure inside the renderer's frames with fixed typed storage; no sample
* objects per frame. `uploads`, when given, counts the bytes the
* renderer uploads for this layer's sets each frame (the Cesium one's is
* `src/render/cesium/uploadProbe.js`, which QA hands in): `reset()` at
* each frame's start, `bytes()` at its end, `stop()` when done.
* @param {number} [durationMs]
* @param {{reset: () => void, bytes: () => number, stop: () => void}|null} [uploads]
*/
function measureFrames(durationMs = 12000, uploads = null) {
stopProbe?.();
const frame = findFrame(state._viewer);
const canvas = canvasOf(state._viewer);
const capacity = 20000,
stride = 12,
data = new Float64Array(capacity * stride);
const previousPositions = new Float64Array(state._vehicles.size * 3);
let cpu = 0,
count = 0,
previous = 0;
const wasRunning = state._preRenderRemove !== null;
stopFrames();
const removePre = frame.on('preRender', () => {
uploads?.reset();
const start = performance.now();
onPreRender();
cpu = performance.now() - start;
});
let removePost, timer;
return new Promise((resolve) => {
const finish = () => {
clearTimeout(timer);
removePre();
removePost?.();
uploads?.stop();
if (wasRunning && state._enabled) startFrames(state._viewer);
stopProbe = null;
const rows = [];
for (let i = 1; i < count; i++) {
const k = i * stride;
rows.push({
intervalMs: data[k],
cpuMs: data[k + 1],
uploadBytes: data[k + 2],
moving: data[k + 3],
x: data[k + 4],
y: data[k + 5],
z: data[k + 6],
displayT: data[k + 7],
expectedMps: data[k + 8],
segment: data[k + 9],
});
}
resolve({
rows,
dpr: devicePixelRatio,
width: canvas?.clientWidth,
height: canvas?.clientHeight,
});
};
stopProbe = finish;
removePost = frame.on('postRender', () => {
const now = performance.now();
if (count >= capacity) return;
const k = count++ * stride;
const entry = state._vehicles.get('mbta:qa-0');
data[k] = previous ? now - previous : 0;
previous = now;
data[k + 1] = cpu;
data[k + 2] = uploads ? uploads.bytes() : 0;
let actuallyMoved = 0;
for (const vehicle of state._visible) {
const p = vehicle.marker.position,
index = vehicle.qaIndex * 3;
if (
count > 1 &&
Math.hypot(
p.x - previousPositions[index],
p.y - previousPositions[index + 1],
p.z - previousPositions[index + 2],
) > 0.00001
)
actuallyMoved++;
previousPositions[index] = p.x;
previousPositions[index + 1] = p.y;
previousPositions[index + 2] = p.z;
}
data[k + 3] = actuallyMoved;
if (entry?.marker) {
const p = entry.marker.position;
data[k + 4] = p.x;
data[k + 5] = p.y;
data[k + 6] = p.z;
data[k + 7] = entry.sample.displayT;
data[k + 8] = entry.sample.segmentSpeedMps;
data[k + 9] = entry.sample.fromSeq;
}
});
timer = setTimeout(finish, durationMs);
});
}
return {
createMarkers,
showMarkers,
@@ -1310,8 +1234,6 @@ export function createRendering({ state, services, parts }) {
cameraHeightM,
globeSurface,
sampleMeshFloors,
measureFrames,
stopFrameProbe,
resumePlayback,
maintainPresentation,
requestVisibility,
+3 -2
View File
@@ -1,4 +1,5 @@
import { createHistoryBudget } from '../../data/contactPlayback.js';
import { Vec3 } from '../../geo/vector.js';
import { worldTimeFor } from '../../time/worldClock.js';
/**
@@ -134,8 +135,8 @@ export function createState({ services }) {
state._limitWarned = false;
// Where `samplePosition` writes a marker's position, each frame, before
// it is copied into the marker.
state._scratchCartesian = { x: 0, y: 0, z: 0 };
// it is copied into the marker: a `Vec3`, so the write allocates nothing.
state._scratchCartesian = new Vec3();
return state;
}
+112 -2
View File
@@ -1,4 +1,5 @@
import { fromDegrees } from '../../geo/ellipsoid.js';
import { canvasOf, findFrame } from '../../render/rendererOf.js';
import { recordFix, updatePlayback, attachFloorToPlace } from './movement.js';
import { seek, setRate, mergeHistory } from '../../data/contactPlayback.js';
import { getRegisteredTransitFeed } from '../../data/transitFeeds.js';
@@ -11,12 +12,121 @@ import { getRegisteredTransitFeed } from '../../data/transitFeeds.js';
import { transitIconCacheSize } from '../../data/transitIcons.js';
export function createTesting({ state, parts }) {
let stopProbe = null;
/** Stop a running frame probe, resolving it with what it has. */
function stopFrameProbe() {
stopProbe?.();
}
/**
* Measure inside the renderer's frames with fixed typed storage; no sample
* objects per frame. `uploads`, when given, counts the bytes the
* renderer uploads for this layer's sets each frame (the Cesium one's is
* `src/render/cesium/uploadProbe.js`, which QA hands in): `reset()` at
* each frame's start, `bytes()` at its end, `stop()` when done.
* @param {number} [durationMs]
* @param {{reset: () => void, bytes: () => number, stop: () => void}|null} [uploads]
*/
function measureFrames(durationMs = 12000, uploads = null) {
stopProbe?.();
const frame = findFrame(state._viewer);
const canvas = canvasOf(state._viewer);
const capacity = 20000,
stride = 12,
data = new Float64Array(capacity * stride);
const previousPositions = new Float64Array(state._vehicles.size * 3);
let cpu = 0,
count = 0,
previous = 0;
const wasRunning = state._preRenderRemove !== null;
parts.rendering.stopFrames();
const removePre = frame.on('preRender', () => {
uploads?.reset();
const start = performance.now();
parts.rendering.onPreRender();
cpu = performance.now() - start;
});
let removePost, timer;
return new Promise((resolve) => {
const finish = () => {
clearTimeout(timer);
removePre();
removePost?.();
uploads?.stop();
if (wasRunning && state._enabled)
parts.rendering.startFrames(state._viewer);
stopProbe = null;
const rows = [];
for (let i = 1; i < count; i++) {
const k = i * stride;
rows.push({
intervalMs: data[k],
cpuMs: data[k + 1],
uploadBytes: data[k + 2],
moving: data[k + 3],
x: data[k + 4],
y: data[k + 5],
z: data[k + 6],
displayT: data[k + 7],
expectedMps: data[k + 8],
segment: data[k + 9],
});
}
resolve({
rows,
dpr: devicePixelRatio,
width: canvas?.clientWidth,
height: canvas?.clientHeight,
});
};
stopProbe = finish;
removePost = frame.on('postRender', () => {
const now = performance.now();
if (count >= capacity) return;
const k = count++ * stride;
const entry = state._vehicles.get('mbta:qa-0');
data[k] = previous ? now - previous : 0;
previous = now;
data[k + 1] = cpu;
data[k + 2] = uploads ? uploads.bytes() : 0;
let actuallyMoved = 0;
for (const vehicle of state._visible) {
const p = vehicle.marker.position,
index = vehicle.qaIndex * 3;
if (
count > 1 &&
Math.hypot(
p.x - previousPositions[index],
p.y - previousPositions[index + 1],
p.z - previousPositions[index + 2],
) > 0.00001
)
actuallyMoved++;
previousPositions[index] = p.x;
previousPositions[index + 1] = p.y;
previousPositions[index + 2] = p.z;
}
data[k + 3] = actuallyMoved;
if (entry?.marker) {
const p = entry.marker.position;
data[k + 4] = p.x;
data[k + 5] = p.y;
data[k + 6] = p.z;
data[k + 7] = entry.sample.displayT;
data[k + 8] = entry.sample.segmentSpeedMps;
data[k + 9] = entry.sample.fromSeq;
}
});
timer = setTimeout(finish, durationMs);
});
}
return {
_setTransitFixtureFloorsForTest(keys, heightM) {
state._qaFixtureFloors = new Map(keys.map((key) => [key, heightM]));
},
_stopTransitProbeForTest() {
parts.rendering.stopFrameProbe();
stopFrameProbe();
},
/** Deterministic observed-position fixtures, independent of upstream availability. */
_loadTransitFleetForTest(count, origin, spacingM = 6, routes = ['741']) {
@@ -157,7 +267,7 @@ export function createTesting({ state, parts }) {
* (`src/render/cesium/uploadProbe.js` on the default renderer).
*/
_measureTransitFramesForTest(durationMs = 12000, uploads = null) {
return parts.rendering.measureFrames(durationMs, uploads);
return measureFrames(durationMs, uploads);
},
/** The ids of the markers' two draw sets: still, then moving. */
_transitSetIdsForTest() {