Keep the scene command trim in the Cesium adapter and take its installer off the Cesium list

The private-field trim of Cesium's reused per-pass command arrays moves to
src/render/cesium/sceneCommandRelease.js. The installer waits for the
renderer frame port's postRender after each layer disable and calls a trim
the application scene hands over, so it no longer touches the Cesium scene.
This commit is contained in:
Sameh Khamis committed 2026-11-07 05:29:57 -08:00
1 parent 71ff682c7f
commit b9248bfce5
10 files changed
+226 -170

No files matched your search

+17 -55
View File
@@ -1,73 +1,35 @@
/**
* Let a turned-off layer's memory go.
*
* Cesium (engine 22.3, `Scene/View.js`) reuses each view's per-pass command
* arrays: every frame resets `frustumCommands.indices[pass]` to 0 and
* overwrites `commands[pass][i]` from the start. `Scene.js` truncates the
* voxel, Gaussian-splat and translucent passes to their counts; the opaque
* and other passes keep the slots past the new count. When a layer is turned
* off the next frame has fewer commands, so that tail keeps the destroyed
* primitives' DrawCommands, and through `_owner` → primitive → entity →
* EntityCollection the whole data source stays reachable. The offscreen view
* `scene.sampleHeight` / `pickFromRay` render into keeps its last list the
* same way, until the next height sample.
*
* Measured (production build, heap after GC, Data Centers on → off): 116 MiB
* kept on main, 13 MiB with this module; heap snapshots traced the entities
* through `View.frustumCommandsList` and `Picking._pickOffscreenView`.
*
* After a layer finishes turning off, this renders one frame, trims the main
* view's arrays to that frame's counts, and empties the offscreen pick view's
* lists (it rebuilds them in full on every pick). It reads private Cesium
* fields: `src/sceneCommandRelease.test.mjs` fails if any of them is renamed
* or if Cesium changes which passes it truncates, and once Cesium clears the
* tail itself this module can go.
* A renderer may keep a turned-off layer's draw commands, and through them
* its whole data source, reachable after the layer stops drawing: the
* default renderer does (see `src/render/cesium/sceneCommandRelease.js`).
* After a layer finishes turning off, this asks for one frame and, once it
* is drawn, has the renderer trim what that frame no longer drew.
*/
import {
commandExtents,
defaultView,
pickOffscreenView,
} from './render/cesium/internals.js';
/** Drop command references a view no longer draws. */
function trimView(view, { keepCurrent }) {
for (const frustum of view?.frustumCommandsList || []) {
const { commands, indices } = frustum;
for (let pass = 0; pass < commands.length; pass += 1) {
const keep = keepCurrent ? indices[pass] : 0;
if (commands[pass].length > keep) commands[pass].length = keep;
if (!keepCurrent) indices[pass] = 0;
}
}
if (!keepCurrent)
for (const extent of commandExtents(view)) extent.command = undefined;
}
/**
* Release stale command references in the scene's main view (keeping what
* the last frame drew) and in its offscreen pick view (all of it).
* @param {object} scene Cesium Scene, right after a render.
*/
export function trimStaleSceneCommands(scene) {
trimView(defaultView(scene), { keepCurrent: true });
trimView(pickOffscreenView(scene), { keepCurrent: false });
}
/**
* After every layer disable, render one frame and trim once it is drawn.
* @param {{scene: object, dataManager: object, requestRender: (reason: string) => void}} deps
* @param {{frame: import('./render/contract/frame').FramePort,
* dataManager: object, requestRender: (reason: string) => void,
* trim: () => void}} deps `trim` drops what the last drawn frame did not
* draw.
* @returns {() => void} uninstall
*/
export function installLayerReleaseTrim({ scene, dataManager, requestRender }) {
export function installLayerReleaseTrim({
frame,
dataManager,
requestRender,
trim,
}) {
let removeOneShot = null;
const unsubscribe = dataManager.subscribe((change) => {
if (change?.type !== 'visibility' || change.enabled !== false) return;
if (removeOneShot) return; // a trim is already waiting for its frame
removeOneShot = scene.postRender.addEventListener(() => {
removeOneShot = frame.on('postRender', () => {
removeOneShot();
removeOneShot = null;
trimStaleSceneCommands(scene);
trim();
});
requestRender('layer-release-trim');
});