diff --git a/docs/RENDERER-CONTRACT.md b/docs/RENDERER-CONTRACT.md index c319ad56e..d2e33cacc 100644 --- a/docs/RENDERER-CONTRACT.md +++ b/docs/RENDERER-CONTRACT.md @@ -160,8 +160,7 @@ draws each. The Cesium port takes every kind of `ImageRef` wherever an image is drawn (icons on billboards and on the GPU, line patterns, shape fills, quads and raster shells): a URL, SVG, a blob (by an object URL made once per blob, -revoked once not asked for in a minute and two drawn frames, so blobs no -longer drawn are let go; drawn again, a blob gets a new one), a +revoked once the blob itself has been collected), a keyed canvas, and an atlas slot as the image it holds (an icon waits for an unregistered slot; anything else refuses it). The check `draw.icons.images` draws each kind on still and receding icons. diff --git a/src/render/cesium/common.js b/src/render/cesium/common.js index 11fe53552..280e36c8f 100644 --- a/src/render/cesium/common.js +++ b/src/render/cesium/common.js @@ -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(); diff --git a/src/render/cesium/common.test.mjs b/src/render/cesium/common.test.mjs index 29c132e7b..14d45bbda 100644 --- a/src/render/cesium/common.test.mjs +++ b/src/render/cesium/common.test.mjs @@ -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(); + } +}); diff --git a/src/render/cesium/draw.js b/src/render/cesium/draw.js index 678ddd93e..81fbb452f 100644 --- a/src/render/cesium/draw.js +++ b/src/render/cesium/draw.js @@ -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.