mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 08:12:24 +00:00
Cesium: revoke blob object URLs no longer drawn
The object URL store keeps its blob alive, so a blob image's URL, never revoked, kept every blob ever drawn. A blob's URL is now revoked once it has not been asked for in a minute with two frames drawn since (so a material, which loads in the next frame, has started its load); images already loading or loaded are unaffected, and a blob drawn again gets a new URL.
This commit is contained in:
1 parent
a3ffb33563
commit
748dc0a156
4 files changed
+124
-8
No files matched your search
@@ -159,7 +159,9 @@ 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), a
|
||||
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
|
||||
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.
|
||||
|
||||
@@ -66,12 +66,71 @@ export function uncaught(error) {
|
||||
});
|
||||
}
|
||||
|
||||
/** Each blob's object URL, made once and kept while the blob lives. */
|
||||
const blobUrls = new WeakMap();
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
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 blob's object URL: the same one every time it is drawn. */
|
||||
/** 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);
|
||||
}
|
||||
if (liveBlobUrls.size) scheduleBlobSweep();
|
||||
}
|
||||
|
||||
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 url = blobUrls.get(blob);
|
||||
if (!url) blobUrls.set(blob, (url = URL.createObjectURL(blob)));
|
||||
return url;
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
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';
|
||||
|
||||
test('a blob’s object URL is kept while it is drawn and revoked once idle', () => {
|
||||
const made = [];
|
||||
const revoked = [];
|
||||
const create = mock.method(URL, 'createObjectURL', () => {
|
||||
made.push(`blob:test/${made.length}`);
|
||||
return made.at(-1);
|
||||
});
|
||||
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);
|
||||
} finally {
|
||||
mock.timers.reset();
|
||||
create.mock.restore();
|
||||
revoke.mock.restore();
|
||||
}
|
||||
});
|
||||
@@ -36,7 +36,13 @@ 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, listenerList, toCartesian, uncaught } from './common.js';
|
||||
import {
|
||||
blobUrl,
|
||||
blobUrlFrameDrawn,
|
||||
listenerList,
|
||||
toCartesian,
|
||||
uncaught,
|
||||
} from './common.js';
|
||||
import { createGpuIcons, GPU_ICON_CAPABILITIES } from './gpuIcons.js';
|
||||
import { blendHost } from './setBlend.js';
|
||||
import { createModelSets } from './drawModels.js';
|
||||
@@ -5196,6 +5202,7 @@ 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.
|
||||
|
||||
Reference in new issue
Block a user