fix(layers): release bundled GeoJSON entities on toggle-off instead of hiding them

Toggling datacenters, dams, or submarine cables off only set `show = false`
on their data sources. Cesium's DataSourceDisplay still walks every
visualizer of a hidden source each frame, and the ~11.5k hidden entities
(hundreds of MB) stayed resident, so the whole scene rendered ~2x slower
for the rest of the session after a single on/off round trip (owner field
test 2026-09-13).

disable() now removes the data sources from the viewer and drops the built
records; the parsed dataset (NDJSON features / TeleGeography JSON) is cached
so enable() rebuilds without a refetch or re-parse. A disable that lands
while a load is in flight releases the finished build instead of parking it
hidden. destroy() clears the cache.

Measured with a headful Chrome harness (flights+military+satellites on,
camera rotating, 250-frame windows), before -> after:
- entities resident after OFF: 11,537 -> 0
- DataSourceDisplay.update per frame after OFF: 1.09 ms -> 0.02 ms
- scene.render per frame after OFF: 13.9 ms (control 6.6 ms) -> 5.2 ms
  (fresh baseline 5.0 ms in the same run)
- JS heap 30 s after OFF: 1.6 GB and climbing -> 342 MB after GC

Tests: the GeoJSON lifecycle and cables suites assert disable leaves zero
sources/contexts and re-enable rebuilds with fetch called once.
This commit is contained in:
Bilawal Sidhu committed 2026-09-13 17:08:23 -05:00
1 parent ca5f5fff93
commit 499e1168a7
4 files changed
+190 -32

No files matched your search

+57 -9
View File
@@ -496,6 +496,14 @@ export function createTeleGeographySubmarineCableLayer({
let _cableDataSource = null;
let _landingDataSource = null;
let _referenceDataSource = null;
/**
* Fetched cable/landing JSON kept across disable/enable. A disable RELEASES
* the three data sources (see releaseDataSources) instead of hiding them,
* so a re-enable has to rebuild the entities; the network/parse half of
* that rebuild is the expensive part users would notice, so it is cached.
*/
let _cachedCableJson = null;
let _cachedLandingJson = null;
let _referenceRecords = [];
let _surfaceRecords = [];
let _clickHandler = null;
@@ -553,11 +561,17 @@ export function createTeleGeographySubmarineCableLayer({
const owns = () => generation === _loadGeneration && !abort.signal.aborted;
try {
const [cableJson, landingJson] = await Promise.all([
fetchJson(cableUrl, abort.signal),
fetchJson(landingPointUrl, abort.signal),
]);
if (!owns()) return;
let cableJson = _cachedCableJson;
let landingJson = _cachedLandingJson;
if (!cableJson || !landingJson) {
[cableJson, landingJson] = await Promise.all([
fetchJson(cableUrl, abort.signal),
fetchJson(landingPointUrl, abort.signal),
]);
if (!owns()) return;
_cachedCableJson = cableJson;
_cachedLandingJson = landingJson;
}
const cableFeatures = normalizeFeatures(cableJson, 'cable');
const landingFeatures = normalizeFeatures(landingJson, 'landing');
@@ -706,6 +720,37 @@ export function createTeleGeographySubmarineCableLayer({
}
}
/**
* Remove the three data sources from the viewer and forget the built
* entities. Hiding them (`show = false`) is NOT enough: Cesium's
* DataSourceDisplay still walks every visualizer of a hidden source on
* every frame, and the thousands of hidden polylines/billboards keep
* costing CPU and GC pressure long after the user toggled the layer off —
* that is the "everything gets sluggish after I turn layers off" report.
* The fetched JSON stays cached, so the next enable rebuilds without a
* network round trip or a re-parse.
*/
function releaseDataSources(viewer) {
const sources = [_cableDataSource, _landingDataSource, _referenceDataSource];
_cableDataSource = null;
_landingDataSource = null;
_referenceDataSource = null;
_referenceRecords = [];
_surfaceRecords = [];
_pickByEntity = new WeakMap();
_referenceLabelCount = 0;
_publishScratch.length = 0;
// The blend pass targets the collections being dropped; the rebuilt
// sources need their own pass.
_markerBlendDone = false;
_loaded = false;
_referenceSweepGate.reset();
for (const source of sources) {
if (!source) continue;
try { viewer?.dataSources?.remove(source, true); } catch { /* collection gone */ }
}
}
function updateVisibility() {
if (_cableDataSource) _cableDataSource.show = _enabled;
if (_landingDataSource) _landingDataSource.show = _enabled;
@@ -1093,7 +1138,6 @@ export function createTeleGeographySubmarineCableLayer({
disable() {
_enabled = false;
updateVisibility();
_overlayPublisher.hide();
resetPublishSignature();
if (_loading && _abort) {
@@ -1101,6 +1145,10 @@ export function createTeleGeographySubmarineCableLayer({
_loading = false;
_loadingLabel = '';
}
// Free the entities rather than hide them; enable() rebuilds from the
// cached JSON. An in-flight load's own post-await ownership check
// removes whatever that stale generation adds later.
releaseDataSources(_viewer);
},
update(viewer) {
@@ -1109,9 +1157,9 @@ export function createTeleGeographySubmarineCableLayer({
destroy(viewer) {
if (_abort) _abort.abort();
if (_cableDataSource && viewer) viewer.dataSources.remove(_cableDataSource, true);
if (_landingDataSource && viewer) viewer.dataSources.remove(_landingDataSource, true);
if (_referenceDataSource && viewer) viewer.dataSources.remove(_referenceDataSource, true);
releaseDataSources(viewer || _viewer);
_cachedCableJson = null;
_cachedLandingJson = null;
// hide() clears every published host entry and goes invisible while
// keeping the publisher reusable — the legacy layer supported re-init
// after destroy, and hidden publishers already drop late publishes.