Stop Navara's memory growth on engine switches, hide polygon zones reliably, and drop the Cesium camera rig

- Navara 0.2.1 never frees pick batch ids, and the mover banks rebuilt
  their instanced meshes on each attach, leaking about 260 KiB of
  WebAssembly memory per switch. A per-view pool hands released ids back
  out; the long soak now asserts native growth of at most 32 KiB per switch.
- Navara 0.2.1 sometimes drops the first show:false update on a new
  vector layer, so a zone turned off right after a switch back stayed
  drawn. Polygon displays hide by deleting their layers and rebuild them
  when shown.
- Camera verbs drive both engines through the globe contract; the Cesium
  viewer rig and the tests of states it can no longer reach are gone.
- Flights and military tracking share src/layers/aircraft/tracking.js.
- The geo import scenario reads movers only where the engine has them.
- Add the Navara section to docs/CURRENT-STATE.md and a changelog fragment.
This commit is contained in:
German Khokhlov committed 2026-11-05 21:18:23 +04:00
1 parent 7abf74518f
commit a6c64713ee
46 files changed
+2367 -2894

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
// Navara 0.2.1 never takes a global batch id back: its core keeps every id
// ctx.genGlobalBatchId() hands out (about 20 bytes of WASM memory each,
// never freed). A PickableInstancedMeshWrapper takes one id per instance of
// its capacity, and a layer builds its mover sets again each time it
// attaches to the engine, so every switch back to Navara with 10 000
// flights took some 32 000 new ids (about 650 KiB). This pool gives the ids
// of a wrapper that is gone to the next wrapper on the same view. Upstream
// ask: release a batch id, or a wrapper's ids when it is unregistered.
/** A pool of one view's released global batch ids. */
export function createBatchIdPool() {
const free = [];
const released = new WeakSet();
return {
/**
* The view's `ctx` to build a pickable wrapper with, whose batch ids
* come from the pool first; everything else is the view's own.
*/
source(ctx) {
const take = () => (free.length ? free.pop() : ctx.genGlobalBatchId());
return new Proxy(ctx, {
get: (target, key, receiver) =>
key === 'genGlobalBatchId'
? take
: Reflect.get(target, key, receiver),
});
},
/**
* Takes back the ids of a wrapper no longer drawn or registered for
* picking; a wrapper's ids come back once.
*/
release(wrapper) {
if (!wrapper || released.has(wrapper)) return;
released.add(wrapper);
for (const id of wrapper.batchIds) if (id > 0) free.push(id);
},
/** The ids waiting for a wrapper. */
size: () => free.length,
};
}
+68
View File
@@ -0,0 +1,68 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import { createBatchIdPool } from './batchIds.js';
// A view context that never takes an id back, as Navara 0.2.1's core.
function fakeContext() {
let next = 0;
return {
issued: () => next,
genGlobalBatchId: () => ++next,
};
}
// The wrapper's part that matters here: one id per instance, from its ctx.
const wrapper = (ctx, count) => ({
batchIds: Array.from({ length: count }, () => ctx.genGlobalBatchId() ?? 0),
});
test('a rebuilt set takes the ids of the set it replaces', () => {
const ctx = fakeContext();
const pool = createBatchIdPool();
let set = wrapper(pool.source(ctx), 1000);
for (let attach = 0; attach < 50; attach++) {
pool.release(set);
set = wrapper(pool.source(ctx), 1000);
}
assert.equal(ctx.issued(), 1000);
assert.equal(pool.size(), 0);
assert.equal(new Set(set.batchIds).size, 1000);
});
test('live sets never share an id, and a set gives its ids back once', () => {
const ctx = fakeContext();
const pool = createBatchIdPool();
const a = wrapper(pool.source(ctx), 3);
pool.release(a);
pool.release(a);
assert.equal(pool.size(), 3);
const b = wrapper(pool.source(ctx), 2);
const c = wrapper(pool.source(ctx), 2);
const ids = [...b.batchIds, ...c.batchIds];
assert.equal(new Set(ids).size, 4);
assert.equal(ctx.issued(), 4);
});
test('ids Navara could not give (0) are not pooled', () => {
const pool = createBatchIdPool();
pool.release({ batchIds: [0, 0, 7] });
pool.release(null);
assert.equal(pool.size(), 1);
const none = { genGlobalBatchId: () => undefined };
assert.equal(pool.source(none).genGlobalBatchId(), 7);
assert.equal(pool.source(none).genGlobalBatchId(), undefined);
});
test('everything else reaches the view context itself', () => {
const ctx = {
_meshes: new Map(),
genGlobalBatchId: () => 1,
registerPickableMesh(key, wrapper) {
this._meshes.set(key, wrapper);
},
};
const source = createBatchIdPool().source(ctx);
source.registerPickableMesh('a', 'wrapper');
assert.equal(ctx._meshes.get('a'), 'wrapper');
assert.equal(source._meshes, ctx._meshes);
});
+14 -7
View File
@@ -5,7 +5,8 @@
// (modelMover.js). In the cinematic look, depth stays in that scene and
// lit model colour resolves in the display pass. Pickable sets register a
// PickableInstancedMeshWrapper so Navara's pick pass sees each instance;
// pickIds.js turns its batch ids back into instances. commit() uploads and
// pickIds.js turns its batch ids back into instances, and batchIds.js gives
// a removed set's ids to the next one. commit() uploads and
// asks for one frame (view.forceUpdate); per-frame work runs only on frames
// the view draws anyway, so an idle view stays idle.
@@ -17,6 +18,7 @@ import {
} from '@navaramap/three';
import { Group } from 'three';
import { createMoverRegistry } from '../../movers.js';
import { createBatchIdPool } from './batchIds.js';
import { createModelMover } from './modelMover.js';
import { createMoverMesh } from './moverMesh.js';
import { createPickIndex } from './pickIds.js';
@@ -34,16 +36,16 @@ class GevMoverSetDesc extends withFrameUpdates(MeshDesc) {
}
createMesh() {
const { mover, pickable, onPickable } = this.gev;
const { mover, pickable, onPickable, batchIds } = this.gev;
const register = (mesh, key) => {
if (!pickable) return;
const wrapper = new PickableInstancedMeshWrapper(
mesh,
mesh.instanceMatrix.count,
this.ctx,
batchIds.source(this.ctx),
);
this.ctx.registerPickableMesh(key, wrapper);
this.wrappers.push(key);
this.wrappers.push({ key, wrapper });
onPickable(wrapper);
};
const group = new Group();
@@ -66,8 +68,9 @@ class GevMoverSetDesc extends withFrameUpdates(MeshDesc) {
onDestroy() {
this.destroyedByGev = true;
for (const key of this.wrappers) this.ctx.unregisterPickableMesh(key);
for (const { key } of this.wrappers) this.ctx.unregisterPickableMesh(key);
this.gev.mover.dispose();
for (const { wrapper } of this.wrappers) this.gev.batchIds.release(wrapper);
super.onDestroy();
}
}
@@ -90,6 +93,7 @@ export function createNavaraMovers(view, { overlay, aces = false }) {
registered.add(view);
}
const pickIndex = createPickIndex();
const batchIds = createBatchIdPool();
const readFrameCamera = createMoverFrameCamera(view);
let loader = null;
const loadGltf = (url) => {
@@ -111,11 +115,12 @@ export function createNavaraMovers(view, { overlay, aces = false }) {
overlayUniforms: targetOverlay.uniforms,
});
const key = `gevMovers:${options.id}`;
let wrapper = null;
if (options.pickable) {
const wrapper = new PickableInstancedMeshWrapper(
wrapper = new PickableInstancedMeshWrapper(
mover.mesh,
options.capacity,
targetOverlay.ctx,
batchIds.source(targetOverlay.ctx),
);
targetOverlay.registerPickable(key, wrapper);
pickIndex.add(options.id, wrapper, (index) => store.pickIds[index]);
@@ -134,6 +139,7 @@ export function createNavaraMovers(view, { overlay, aces = false }) {
if (options.pickable) targetOverlay.unregisterPickable(key);
targetOverlay.remove(mover.mesh);
mover.dispose();
batchIds.release(wrapper);
if (options.sharpen && --preSharpenUsers === 0) {
preSharpenOverlay.destroy();
preSharpenOverlay = null;
@@ -155,6 +161,7 @@ export function createNavaraMovers(view, { overlay, aces = false }) {
readFrameCamera,
kind: options.kind,
pickable: options.pickable,
batchIds,
onPickable: (wrapper) =>
pickIndex.add(options.id, wrapper, (index) => store.pickIds[index]),
},