mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
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:
1 parent
0ab31d4fb8
commit
725f2f2998
7 files changed
+359
-138
No files matched your search
@@ -9,6 +9,21 @@ export const PI_OVER_TWO = Math.PI / 2;
|
||||
export const EPSILON14 = 1e-14;
|
||||
|
||||
export const vec = (x = 0, y = 0, z = 0) => ({ x, y, z });
|
||||
/**
|
||||
* A three-vector of a shape its own, for a position rewritten every frame.
|
||||
* Every `{x, y, z}` literal in the page shares one hidden shape, and once
|
||||
* any code stores a non-number in one (libraries the page loads do) each
|
||||
* number written to any of them is allocated. An instance of this class
|
||||
* shares its shape with no other code, so writing a new position into it
|
||||
* allocates nothing. Read as a plain `{x, y, z}` everywhere.
|
||||
*/
|
||||
export class Vec3 {
|
||||
constructor(x = 0, y = 0, z = 0) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
}
|
||||
}
|
||||
export const clone = (a) => ({ x: a.x, y: a.y, z: a.z });
|
||||
export const add = (a, b) => vec(a.x + b.x, a.y + b.y, a.z + b.z);
|
||||
export const sub = (a, b) => vec(a.x - b.x, a.y - b.y, a.z - b.z);
|
||||
|
||||
@@ -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
@@ -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,
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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() {
|
||||
|
||||
+27
-11
@@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
Reference in new issue
Block a user