Cesium: a blob's object URL is revoked once the blob is collected, not by time

Revoking a URL idle for a minute broke two things: the icon atlas, keyed by
URL and never freeing a slot, took a new slot each time a blob was drawn
again until it had no room; and an image material, which loads on its first
draw, made hidden and shown later fetched a revoked URL and never drew. A
blob now keeps one URL, revoked by a FinalizationRegistry once the blob
itself is collected: anything that can still draw it holds it.
This commit is contained in:
Sameh Khamis committed 2026-11-11 20:02:20 -08:00
1 parent db80c9f141
commit b3c6f35023
4 files changed
+70 -103

No files matched your search

+22 -63
View File
@@ -67,70 +67,29 @@ export function uncaught(error) {
}
/**
* How long a blob's object URL lives after it was last asked for, ms. The
* object URL store keeps its blob alive, so a URL never revoked keeps every
* blob ever drawn. An image starts loading from its URL as it is given (a
* billboard, the icon atlas) or in the next frame drawn (a material), and a
* load started goes on after the URL is revoked, so a URL idle this long,
* with `BLOB_URL_FRAMES` frames drawn since, is revoked; the blob drawn
* again later gets a new one.
* Blob object URLs: one stable URL a blob, revoked only once the blob itself
* has been collected. Every item that can still draw a blob holds it, so its
* URL stays valid for as long as anything can load from it (a material that
* loads on its first draw, a set shown long after it was made); and the
* same blob always keys the same icon atlas slot.
* @param {typeof FinalizationRegistry} [Registry] What finds the blobs
* collected; `FinalizationRegistry` where there is one.
*/
export const BLOB_URL_IDLE_MS = 60000;
/** Frames drawn after a blob's URL was last asked for before it is revoked. */
export const BLOB_URL_FRAMES = 2;
let blobFrames = 0;
/** A frame was drawn: lazily loaded images have started loading. */
export function blobUrlFrameDrawn() {
blobFrames += 1;
}
/**
* Each blob's object URL: `{blob, url, askedAt, askedFrame}`, while it is
* not revoked.
*/
const blobUrls = new WeakMap();
/** The same entries, to sweep. */
const liveBlobUrls = new Set();
let blobSweep = null;
/** Revoke the object URLs idle for `BLOB_URL_IDLE_MS`; sweep again while any live. */
function sweepBlobUrls() {
blobSweep = null;
const now = Date.now();
for (const entry of liveBlobUrls)
if (
now - entry.askedAt >= BLOB_URL_IDLE_MS &&
blobFrames - entry.askedFrame >= BLOB_URL_FRAMES
) {
liveBlobUrls.delete(entry);
blobUrls.delete(entry.blob);
URL.revokeObjectURL(entry.url);
export function createBlobUrls(Registry = globalThis.FinalizationRegistry) {
const urls = new WeakMap();
const collected = Registry
? new Registry((url) => URL.revokeObjectURL(url))
: null;
return (blob) => {
let url = urls.get(blob);
if (!url) {
url = URL.createObjectURL(blob);
urls.set(blob, url);
collected?.register(blob, url);
}
if (liveBlobUrls.size) scheduleBlobSweep();
return url;
};
}
function scheduleBlobSweep() {
if (blobSweep !== null) return;
blobSweep = setTimeout(sweepBlobUrls, BLOB_URL_IDLE_MS);
// Never what keeps a process running.
blobSweep?.unref?.();
}
/**
* A blob's object URL: the same one each time it is drawn, until it has
* gone `BLOB_URL_IDLE_MS` and `BLOB_URL_FRAMES` drawn frames without being
* asked for, when it is revoked.
*/
export function blobUrl(blob) {
let entry = blobUrls.get(blob);
if (!entry) {
entry = { blob, url: URL.createObjectURL(blob), askedAt: 0, askedFrame: 0 };
blobUrls.set(blob, entry);
liveBlobUrls.add(entry);
}
entry.askedAt = Date.now();
entry.askedFrame = blobFrames;
scheduleBlobSweep();
return entry.url;
}
/** A blob's object URL: the same one every time it is drawn. */
export const blobUrl = createBlobUrls();
+46 -30
View File
@@ -1,13 +1,8 @@
import assert from 'node:assert/strict';
import test, { mock } from 'node:test';
import {
BLOB_URL_FRAMES,
BLOB_URL_IDLE_MS,
blobUrl,
blobUrlFrameDrawn,
} from './common.js';
import { blobUrl, createBlobUrls } from './common.js';
test('a blob’s object URL is kept while it is drawn and revoked once idle', () => {
test('a blob keeps one object URL, revoked only once the blob is collected', () => {
const made = [];
const revoked = [];
const create = mock.method(URL, 'createObjectURL', () => {
@@ -17,32 +12,53 @@ test('a blob’s object URL is kept while it is drawn and revoked once idle', ()
const revoke = mock.method(URL, 'revokeObjectURL', (url) =>
revoked.push(url),
);
mock.timers.enable({ apis: ['setTimeout', 'Date'] });
try {
const blob = new Blob(['a']);
const url = blobUrl(blob);
assert.equal(blobUrl(blob), url, 'one URL a blob');
// Asked for again within the idle time: kept.
mock.timers.tick(BLOB_URL_IDLE_MS / 2);
assert.equal(blobUrl(blob), url);
for (let i = 0; i < BLOB_URL_FRAMES; i++) blobUrlFrameDrawn();
mock.timers.tick(BLOB_URL_IDLE_MS / 2 + 1);
assert.deepEqual(revoked, [], 'asked for within the idle time');
// Asked for, then idle, but no frame drawn since (a material loads in
// the next frame): kept.
assert.equal(blobUrl(blob), url);
mock.timers.tick(3 * BLOB_URL_IDLE_MS);
assert.deepEqual(revoked, [], 'no frame drawn since it was asked for');
for (let i = 0; i < BLOB_URL_FRAMES; i++) blobUrlFrameDrawn();
mock.timers.tick(BLOB_URL_IDLE_MS);
assert.deepEqual(revoked, [url], 'idle, frames drawn: revoked');
// Drawn again later: a new URL.
const again = blobUrl(blob);
assert.notEqual(again, url);
assert.equal(made.length, 2);
let collect;
const registered = [];
class Registry {
constructor(cleanup) {
collect = cleanup;
}
register(blob, url) {
registered.push([blob, url]);
}
}
const urlOf = createBlobUrls(Registry);
const a = new Blob(['a']);
const b = new Blob(['b']);
const url = urlOf(a);
assert.equal(urlOf(a), url);
assert.notEqual(urlOf(b), url);
assert.equal(made.length, 2, 'one URL a blob');
assert.deepEqual(registered, [
[a, url],
[b, made[1]],
]);
assert.deepEqual(revoked, []);
// The registry finds `a` collected: its URL goes.
collect(url);
assert.deepEqual(revoked, [url]);
} finally {
mock.timers.reset();
create.mock.restore();
revoke.mock.restore();
}
});
test('a blob’s URL is not revoked by time: one undrawn for an hour still loads', () => {
const revoked = [];
const revoke = mock.method(URL, 'revokeObjectURL', (url) =>
revoked.push(url),
);
mock.timers.enable({ apis: ['setTimeout', 'setInterval', 'Date'] });
try {
const blob = new Blob(['icon']);
const url = blobUrl(blob);
mock.timers.tick(3600_000);
assert.equal(blobUrl(blob), url, 'the same URL: the same atlas slot');
mock.timers.tick(3600_000);
assert.deepEqual(revoked, []);
} finally {
mock.timers.reset();
revoke.mock.restore();
}
});
+1 -8
View File
@@ -36,13 +36,7 @@ import { temeToPseudoFixed } from '../../geo/celestial.js';
import { alongLength } from '../../geo/polyline.js';
import { clusterScreenPoints } from '../../geo/screenCluster.js';
import { provideUnboundDraw } from '../rendererOf.js';
import {
blobUrl,
blobUrlFrameDrawn,
listenerList,
toCartesian,
uncaught,
} from './common.js';
import { blobUrl, listenerList, toCartesian, uncaught } from './common.js';
import { createGpuIcons, GPU_ICON_CAPABILITIES } from './gpuIcons.js';
import { blendHost } from './setBlend.js';
import { createModelSets } from './drawModels.js';
@@ -5202,7 +5196,6 @@ export function createCesiumDraw({
},
/** Each frame, after it drew: clamp state, and ground swaps. */
afterRender() {
blobUrlFrameDrawn();
watchClamp();
swapReady();
// Clamped GPU icons take the ground under their segments.