Compare commits

..
Author SHA1 Message Date
Mazeyar Moeini FeizabadiandClaude Fable 5.2 dee0d4d2ab docs(changelog): add the #1708 fragment
Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-13 16:05:58 +01:00
Mazeyar Moeini FeizabadiandClaude Fable 5.2 db7cc785df perf(flights): make the fleet pass's ground-floor reads cheap
Every 80 ms the Flights and Military fleet passes read the ground floor for
each coasting aircraft. With the whole globe in view that is about 10,000
aircraft, and each read built three string keys through six toFixed calls and
rewrote the terrain cache to promote its entry. At 4x CPU throttle those reads
were 5.9 s of a 15.3 s profile.

- A coast above 9,000 m (no ground reaches it) skips the geodetic conversion
  and the floor read.
- cachedGroundFloor works its cell out once, as integers; mesh cells and the
  terrain cache's synchronous reads are found by numeric key.
- A synchronous terrain-cache read promotes its entry only when it has
  drifted halfway to eviction, not on every read.

Floors are unchanged: tests compare the reads against the string-keyed ones
on cell edges, near zero and out of range.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-13 15:05:38 +01:00
16 changed files with 684 additions and 260 deletions

No files matched your search

@@ -0,0 +1,4 @@
- Make a whole-globe view of Live Flights about three times faster on a slow
CPU. Aircraft at cruise altitude no longer ask for the ground under them on
every pass, and a ground floor read no longer formats numbers into text
(mazy1998, #1708).
-3
View File
@@ -1,3 +0,0 @@
- Halve the time the satellite cloud image takes to prepare on the 3D Tiles
map by applying its transparency curve from a lookup table, in place
(mazy1998, #1713).
@@ -1,3 +0,0 @@
- Satellite clouds on the 3D Tiles map apply their transparency in the shader
instead of rewriting each 4096 x 2048 image on the page, and changing the
cloud strength no longer builds a new image (mazy1998, #1721).
+11 -7
View File
@@ -891,11 +891,9 @@ image proxy accepts `bbox=west,south,east,north` for every product: inside the
product bounds, 2:1 within 1 %, rounded to 0.25°, up to 4096×2048 (the
default), cached by product, time, size and bbox. Diagnostics report
`shell.detail` (`bbox`, `size`, `ready`, `enabled`).
Clouds only applies a soft brightness ramp, using
Clouds only applies a soft brightness ramp to decoded pixels once, using
Cesium's sRGB-to-linear conversion (`channel ** 2.2`) and smoothstep from 0.40
to 0.70. Globe hosts apply it to decoded pixels once; a shell applies the same
ramp in its fragment shader from a strength uniform, so its images are drawn
as decoded and a strength change neither fetches nor rewrites an image. The old 0.55 threshold is the ramp midpoint. RGB and source alpha are
to 0.70. The old 0.55 threshold is the ramp midpoint. RGB and source alpha are
preserved in Full image mode; both modes use the chosen layer opacity. This
is a display filter, not a cloud mask. The cloud-filter slider is a continuous
0–100% strength: 0% is Full, 100% is the existing Clouds only key, and
@@ -911,13 +909,13 @@ and processes it before staging. Globe hosts crop that canvas into 256 px tiles
to maximum level 3 on a geographic 2×1 root grid bounded to the manifest extent,
avoiding request-dependent contrast seams.
On globe hosts, regional infrared retains network tiles and processes each
decoded tile once; shells never read a frame's pixels back.
decoded tile once; shells process each whole frame once.
Exact-time tile and image responses are immutable for 24 hours; manifests and
errors remain uncached. Each globe renderer retains up to 6 processed global
mosaics in a least-recently-used cache keyed by observation time and infrared
mode (up to 48 MiB of canvas pixels). Each shell keeps decoded full-extent and
detail images in one least-recently-used cache bounded at 128 MiB, keyed by
time and window: the shown frame and the warmed next frame, each
time, mode and window: the shown frame and the warmed next frame, each
full-extent and detail; nothing older survives them. Disable clears the caches.
Cache hits skip
fetch/decode and report `mosaic.cached: true` with zero decode time.
@@ -2951,7 +2949,13 @@ visits to one place share a key but does not bound how many distinct places a
session visits, and the cache previously had no eviction, so a long session
retained every coordinate it ever resolved. Eviction is least-recently-used:
a consumer read, including a warm hit in a batch lookup, promotes its entry, so
the cell being watched is not dropped for having been resolved early. The bound sits far above a city-scale session, so
the cell being watched is not dropped for having been resolved early. A
synchronous read (`cachedEllipsoidalGround`, `cachedRealEllipsoidalGround`)
promotes its entry only once half the bound's worth of stores and promotions
have passed since the entry was last newest, so an entry read at least that
often is still never evicted while the per-frame floor reads no longer rewrite
the cache; those reads find entries through a numeric key and format no
strings. The bound sits far above a city-scale session, so
normal use never evicts and issues no additional proxy requests. A batch's
results are assembled from what that call resolved or found warm, so eviction
during a large batch cannot report one of its points as unresolved. Re:Earth and
+34
View File
@@ -311,6 +311,10 @@ export const COAST_FLOOR_LIFT_M = 1.5;
*/
export function floorCoastedPosition(position, cachedGroundFloor) {
if (typeof cachedGroundFloor !== 'function') return position;
// Most coasting contacts are at cruise, and this runs for each of them on
// every fleet pass. One above COAST_FLOOR_CEILING_M cannot be lifted by any
// ground, so it needs neither the geodetic conversion nor the floor read.
if (clearOfAllGround(position)) return position;
const carto = Cesium.Cartographic.fromCartesian(
position,
Cesium.Ellipsoid.WGS84,
@@ -334,6 +338,36 @@ export function floorCoastedPosition(position, cachedGroundFloor) {
const _coastCarto = new Cesium.Cartographic();
/**
* @constant {number} No ground reaches this height above the ellipsoid:
* Everest's summit is about 8,821 m (8,849 m above a geoid that sits 28 m
* below the ellipsoid there).
*/
export const COAST_FLOOR_CEILING_M = 9000;
const _oneOverRadiiSq = Cesium.Ellipsoid.WGS84.oneOverRadiiSquared;
const COAST_CLEAR_SCALED_SQ =
(1 + COAST_FLOOR_CEILING_M / Cesium.Ellipsoid.WGS84.minimumRadius) ** 2;
/**
* Whether a position is certainly more than COAST_FLOOR_CEILING_M above the
* ellipsoid, from three multiplications. Scale the axes so the ellipsoid is
* the unit sphere: a point h metres up its surface normal is then at most
* 1 + h / (polar radius) from the centre, so one further out than
* 1 + ceiling / (polar radius) is above the ceiling. At the equator this
* answers yes from about 30 m above the ceiling; at the poles, from the
* ceiling itself.
* @param {Cesium.Cartesian3} position
* @returns {boolean}
*/
function clearOfAllGround(position) {
return (
position.x * position.x * _oneOverRadiiSq.x +
position.y * position.y * _oneOverRadiiSq.y +
position.z * position.z * _oneOverRadiiSq.z >
COAST_CLEAR_SCALED_SQ
);
}
/**
* Seconds to coast past the last report: the elapsed time, bounded by the
* source's coast limit and STALE_COAST_MAX_SEC; none for a contact that
+77
View File
@@ -10,6 +10,7 @@ import {
liftRepeatedGroundFix, synthesizeForwardKinematicsFix,
projectGroundArcLatLon, corridorPathLatLon,
COURSE_TRACK_ONLY_MPS, COURSE_CHORD_ONLY_MPS, COURSE_MIN_DPS, TURN_MIN_SPEED_MPS,
floorCoastedPosition, COAST_FLOOR_CEILING_M, COAST_FLOOR_LIFT_M,
} from './motionModel.js';
const AUSTIN = { lat: 30.2672, lon: -97.7431, alt: 9000 };
@@ -341,3 +342,79 @@ test('corridorPathLatLon returns nothing without a display position', () => {
extrapolating: true, displayLat: NaN, displayLon: -97.66, courseDeg: 0, speedMps: 10, lookaheadSec: 60,
}), []);
});
/** floorCoastedPosition as it was before the cruise skip: always converts and reads. */
function floorCoastedReference(position, groundAt) {
const carto = Cesium.Cartographic.fromCartesian(position);
if (!carto) return position;
const ground = groundAt(
Cesium.Math.toDegrees(carto.latitude),
Cesium.Math.toDegrees(carto.longitude),
);
if (!Number.isFinite(ground) || carto.height >= ground + COAST_FLOOR_LIFT_M)
return position;
return Cesium.Cartesian3.fromRadians(carto.longitude, carto.latitude, ground + COAST_FLOOR_LIFT_M);
}
test('a coasting contact above any terrain is not looked up against the ground', () => {
let reads = 0;
const ground = () => {
reads += 1;
return 8821; // Everest's summit, the highest floor there is
};
// Just over the ceiling and at cruise, from pole to equator.
for (let lat = -90; lat <= 90; lat += 7.5) {
for (const lon of [0, 86.925, -78.8, 179.9]) {
for (const height of [COAST_FLOOR_CEILING_M + 35, 10_000, 12_500, 40_000]) {
const position = Cesium.Cartesian3.fromDegrees(lon, lat, height);
const before = Cesium.Cartesian3.clone(position);
assert.equal(floorCoastedPosition(position, ground), position);
assert.ok(Cesium.Cartesian3.equals(position, before));
}
}
}
assert.equal(reads, 0);
});
test('below the ceiling a coast is floored exactly as before', () => {
// Terrain that rises under the coast, known in some places and not others.
const ground = (lat, lon) => (Math.floor(lat * 10 + lon) % 3 === 0 ? null : 1500 + ((lat * 7 + lon * 3) % 900));
let lifted = 0;
let checked = 0;
for (let lat = -89; lat <= 89; lat += 5.5) {
for (let lon = -180; lon < 180; lon += 17) {
for (const height of [-40, 0, 800, 1499, 1501.4, 2399, 2402, 5000, 8990, 9000, 9010, 11_000, 30_000]) {
const position = Cesium.Cartesian3.fromDegrees(lon, lat, height);
const expected = floorCoastedReference(Cesium.Cartesian3.clone(position), ground);
const actual = floorCoastedPosition(position, ground);
assert.ok(
Cesium.Cartesian3.equals(actual, expected),
`coast at ${lat}, ${lon}, ${height} m`,
);
checked += 1;
if (!Cesium.Cartesian3.equals(expected, Cesium.Cartesian3.fromDegrees(lon, lat, height))) lifted += 1;
}
}
}
assert.ok(lifted > 500, `${lifted} of ${checked} coasts were lifted onto the ground`);
});
test('nothing at or below the ceiling skips the ground', () => {
let reads = 0;
const ground = () => {
reads += 1;
return null;
};
let asked = 0;
for (let lat = -90; lat <= 90; lat += 2.5) {
for (const height of [-400, 0, 4000, 8821, COAST_FLOOR_CEILING_M - 1, COAST_FLOOR_CEILING_M]) {
floorCoastedPosition(Cesium.Cartesian3.fromDegrees(33, lat, height), ground);
asked += 1;
}
}
assert.equal(reads, asked);
});
test('the ceiling clears the highest ground with room to spare', () => {
assert.ok(COAST_FLOOR_CEILING_M > 8849 + 100);
});
+13 -36
View File
@@ -1,31 +1,22 @@
export const INFRARED_ALPHA_T0 = 0.4;
export const INFRARED_ALPHA_T1 = 0.7;
/** How much of the cloud key applies, 0–1, for a display mode or a 0–100
* strength. The weather shell passes it to its fragment shader. */
export function infraredAmount(strength) {
return strength === 'full'
? 0
: strength === 'filtered' || !Number.isFinite(strength)
? 1
: Math.max(0, Math.min(100, strength)) / 100;
}
/** Scale each pixel's alpha in place; RGB is left as it is. The scale depends
* only on the brightest channel, so it is worked out once for each of the 256
* byte values instead of once per pixel: a 4096×2048 image has 8.4 million
* pixels and the power curve per pixel held the page for over 100 ms. The
* table holds the same numbers the per-pixel formula produced, so the output
* bytes are unchanged.
/** Return display pixels without changing RGB or the source RGBA bytes.
* `strength` is 0–100: zero is the full image and 100 is the cloud key.
*/
export function applyInfraredAlpha(pixels, strength = 'filtered') {
const amount = infraredAmount(strength);
export function infraredAlpha(rgba, strength = 'filtered') {
const pixels = new Uint8ClampedArray(rgba);
const amount =
strength === 'full'
? 0
: strength === 'filtered' || !Number.isFinite(strength)
? 1
: Math.max(0, Math.min(100, strength)) / 100;
if (amount === 0) return pixels;
const scale = new Float64Array(256);
for (let value = 0; value < 256; value++) {
for (let i = 0; i < pixels.length; i += 4) {
// Match Cesium's czm_srgbToLinear conversion in draped imagery.
const linearMax = (value / 255) ** 2.2;
const linearMax =
(Math.max(rgba[i], rgba[i + 1], rgba[i + 2]) / 255) ** 2.2;
const t = Math.max(
0,
Math.min(
@@ -35,21 +26,7 @@ export function applyInfraredAlpha(pixels, strength = 'filtered') {
),
);
const filtered = t * t * (3 - 2 * t);
scale[value] = 1 - amount + filtered * amount;
}
for (let i = 0; i < pixels.length; i += 4) {
const r = pixels[i];
const g = pixels[i + 1];
const b = pixels[i + 2];
const max = r > g ? (r > b ? r : b) : g > b ? g : b;
pixels[i + 3] = pixels[i + 3] * scale[max];
pixels[i + 3] = rgba[i + 3] * (1 - amount + filtered * amount);
}
return pixels;
}
/** Return display pixels without changing RGB or the source RGBA bytes.
* `strength` is 0–100: zero is the full image and 100 is the cloud key.
*/
export function infraredAlpha(rgba, strength = 'filtered') {
return applyInfraredAlpha(new Uint8ClampedArray(rgba), strength);
}
-41
View File
@@ -1,7 +1,6 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import {
applyInfraredAlpha,
infraredAlpha,
INFRARED_ALPHA_T0,
INFRARED_ALPHA_T1,
@@ -62,43 +61,3 @@ test('full infrared preserves every channel including partial and zero alpha', (
]);
assert.deepEqual(infraredAlpha(pixels, 'full'), pixels);
});
test('the lookup table gives the per-pixel curve byte for byte, in place', () => {
// The transfer as it was written per pixel, before the table.
const perPixel = (rgba, amount) => {
const out = new Uint8ClampedArray(rgba);
for (let i = 0; i < out.length; i += 4) {
const linearMax =
(Math.max(rgba[i], rgba[i + 1], rgba[i + 2]) / 255) ** 2.2;
const t = Math.max(
0,
Math.min(
1,
(linearMax - INFRARED_ALPHA_T0) /
(INFRARED_ALPHA_T1 - INFRARED_ALPHA_T0),
),
);
out[i + 3] = rgba[i + 3] * (1 - amount + t * t * (3 - 2 * t) * amount);
}
return out;
};
// Every brightest-channel value in each channel, against a spread of alphas.
const bytes = [];
for (let value = 0; value < 256; value++)
for (const alpha of [0, 1, 37, 128, 200, 254, 255]) {
bytes.push(value, 0, 0, alpha);
bytes.push(value >> 1, value, 0, alpha);
bytes.push(0, value >> 2, value, alpha);
}
const source = new Uint8ClampedArray(bytes);
for (const strength of [1, 33, 50, 99, 100, 'filtered', Number.NaN]) {
const amount = Number.isFinite(strength) ? strength / 100 : 1;
const pixels = source.slice();
assert.equal(applyInfraredAlpha(pixels, strength), pixels);
assert.deepEqual(pixels, perPixel(source, amount), `strength ${strength}`);
}
for (const strength of [0, 'full']) {
const pixels = source.slice();
assert.deepEqual(applyInfraredAlpha(pixels, strength), source);
}
});
+6 -18
View File
@@ -6,7 +6,7 @@ import {
drawWeatherPixels,
closeWeatherPixels,
} from './source.js';
import { applyInfraredAlpha } from './infraredAlpha.js';
import { infraredAlpha } from './infraredAlpha.js';
export const MAX_MOSAIC_BYTES = 4 * 1024 * 1024;
@@ -31,22 +31,13 @@ export function processInfraredImage(
function applyInfraredTransfer(context, canvas, mode) {
const pixels = context.getImageData(0, 0, canvas.width, canvas.height);
// In place: a copy of a 4096×2048 image is 33 MB, made and written back.
applyInfraredAlpha(pixels.data, mode);
pixels.data.set(infraredAlpha(pixels.data, mode));
context.putImageData(pixels, 0, 0);
}
/** Draw pixels a frame source supplied into a new canvas, with the display
* transfer for infrared products, and release them. They must be `size`. */
function drawSuppliedImage(
pixels,
product,
mode,
size,
signal,
createCanvas,
transfer,
) {
function drawSuppliedImage(pixels, product, mode, size, signal, createCanvas) {
try {
signal.throwIfAborted();
const { width, height } = weatherPixelSize(pixels);
@@ -57,7 +48,7 @@ function drawSuppliedImage(
texture.height = height;
const context = texture.getContext('2d');
drawWeatherPixels(context, pixels);
if (transfer && (product === 'clouds' || product === 'clouds-regional'))
if (product === 'clouds' || product === 'clouds-regional')
applyInfraredTransfer(context, texture, mode);
return texture;
} finally {
@@ -97,8 +88,7 @@ export function decodeInfraredImage(blob, signal) {
}
/** Fetch one capped whole-extent frame, or a `bbox` detail window of it, and draw
* it into a canvas. Infrared frames get the display transfer unless `transfer`
* is false, for a caller that applies it when drawing; a canvas also keeps
* it into a canvas. Infrared frames get the display transfer; a canvas also keeps
* Cesium's texture row order, which an ImageBitmap upload would not. The decoded
* image is always released. With a `frameSource` that has `getImage`, the host
* supplies the decoded pixels instead and nothing is fetched or decoded here. */
@@ -117,7 +107,6 @@ export async function acquireWeatherImage(
now = () => performance.now(),
onFetched = () => {},
frameSource = null,
transfer = true,
},
) {
if (frameSource?.getImage) {
@@ -138,7 +127,6 @@ export async function acquireWeatherImage(
size,
signal,
createCanvas,
transfer,
);
return { texture, decodeMs: now() - started };
}
@@ -159,7 +147,7 @@ export async function acquireWeatherImage(
if (image.width !== size.width || image.height !== size.height)
throw new Error('Invalid weather image dimensions');
let texture;
if (transfer && (product === 'clouds' || product === 'clouds-regional'))
if (product === 'clouds' || product === 'clouds-regional')
texture = processInfraredImage(image, mode, createCanvas);
else {
texture = createCanvas();
-69
View File
@@ -26,75 +26,6 @@ function canvas() {
return output;
}
test('the transfer is applied to the canvas pixels in place, with no copy written back', () => {
let read = null;
const output = {};
output.getContext = () => ({
drawImage() {},
getImageData: () => {
read = { data: new Uint8ClampedArray([194, 0, 0, 200]) };
// A second full-size buffer copied over the first is what this guards.
read.data.set = () => {
throw new Error('the canvas pixels were overwritten from a copy');
};
return read;
},
putImageData: (pixels) => {
output.pixels = pixels;
},
});
processInfraredImage({ width: 1, height: 1 }, 'filtered', () => output);
assert.equal(output.pixels, read);
assert.deepEqual([...read.data], [194, 0, 0, 98]);
});
test('with transfer off an infrared frame is drawn and never read back', async () => {
const drawn = [];
const createCanvas = () => {
const output = {};
output.getContext = () => ({
drawImage: (image) => drawn.push(image),
getImageData: () => {
throw new Error('the frame was read back on the page');
},
putImageData: () => {
throw new Error('the frame was rewritten on the page');
},
});
return output;
};
const size = { width: 4096, height: 2048 };
const image = { ...size, close() {} };
const options = {
signal: new AbortController().signal,
mode: 'filtered',
size,
maxBytes: 1024,
createCanvas,
transfer: false,
};
const fetched = await acquireWeatherImage(
'clouds-regional',
'2026-09-15T20:00:00.000Z',
{
...options,
fetchImpl: async () => ({
ok: true,
headers: new Headers(),
arrayBuffer: async () => new ArrayBuffer(1),
}),
decodeImage: async () => image,
},
);
assert.equal(fetched.texture.width, 4096);
const supplied = { ...size };
await acquireWeatherImage('clouds-regional', '2026-09-15T20:00:00.000Z', {
...options,
frameSource: { getImage: async () => supplied },
});
assert.deepEqual(drawn, [image, supplied]);
});
test('decoded pixels are processed once on a same-sized canvas', () => {
const image = { width: 1, height: 1 };
const output = processInfraredImage(image, 'filtered', canvas);
+17 -34
View File
@@ -1,9 +1,4 @@
import { acquireWeatherImage } from './infraredImage.js';
import {
INFRARED_ALPHA_T0,
INFRARED_ALPHA_T1,
infraredAmount,
} from './infraredAlpha.js';
import { imageryHostStatus } from './imageryHost.js';
import { WEATHER_DETAIL_SIZE, WEATHER_IMAGE_SIZES } from './source.js';
import { msg } from '../../i18n/index.js';
@@ -41,10 +36,6 @@ const LIFT_STEP = 500;
// draw nothing until the full-extent image has arrived, and sample the detail
// image only once it has arrived. The window is west, south, east, north in the
// full-extent image's texture coordinates.
// `infrared` is how much of the cloud key applies, 0–1 (infraredAlpha.js has
// the same curve for the globe's draped imagery). It is applied here, to the
// image as it was decoded, so a 4096×2048 frame is never read back and
// rewritten on the page, and a strength change is one uniform, not a new image.
const MATERIAL_SOURCE = `czm_material czm_getMaterial(czm_materialInput materialInput)
{
czm_material material = czm_getDefaultMaterial(materialInput);
@@ -55,8 +46,7 @@ const MATERIAL_SOURCE = `czm_material czm_getMaterial(czm_materialInput material
float useDetail = inside * step(1.5, float(detailDimensions.x));
vec4 c = mix(coarse, texture(detail, clamp(dst, 0.0, 1.0)), useDetail);
material.diffuse = czm_gammaCorrect(c.rgb);
float key = smoothstep(${INFRARED_ALPHA_T0.toFixed(4)}, ${INFRARED_ALPHA_T1.toFixed(4)}, pow(max(c.r, max(c.g, c.b)), 2.2));
material.alpha = c.a * mix(1.0, key, infrared) * alpha * step(1.5, float(imageDimensions.x));
material.alpha = c.a * alpha * step(1.5, float(imageDimensions.x));
return material;
}
`;
@@ -73,7 +63,6 @@ function registerMaterial(cesium) {
image: cesium.Material.DefaultImageId,
detail: cesium.Material.DefaultImageId,
alpha: 1,
infrared: 0,
window: { type: 'vec4', x: 0, y: 0, z: -1, w: -1 },
},
// Diffuse and alpha share these samples, so one source, not components.
@@ -256,12 +245,6 @@ export function createShellSurface({
material.uniforms.alpha = value;
scene.requestRender();
},
/** How much of the infrared cloud key applies, 0–1. */
setInfrared(value) {
if (destroyed || material.uniforms.infrared === value) return;
material.uniforms.infrared = value;
scene.requestRender();
},
/** Draw `next` inside `rect` (west, south, east, north in the full-extent
* image's texture coordinates, 0–1); null clears it. In the same window only
* the image changes, and Cesium keeps drawing the previous texture until
@@ -455,11 +438,10 @@ export function createWeatherShell({
const visible = () => !hidden && !suspended();
const boundsKey = ({ west, south, east, north }) =>
`${west},${south},${east},${north}`;
// One image per time and window: the infrared mode is a uniform, not pixels.
const cacheKey = (time, box = null) =>
box ? `${time}|${boundsKey(box)}` : time;
const cacheKey = (time, mode, box = null) =>
`${time}|${infrared ? mode : 'none'}${box ? `|${boundsKey(box)}` : ''}`;
const wantedDetail = () =>
current && view ? cacheKey(current.time, view) : null;
current && view ? cacheKey(current.time, current.infrared, view) : null;
function evict(keep = []) {
const kept = new Set([...keep, current?.key, detail?.key]);
@@ -472,11 +454,12 @@ export function createWeatherShell({
}
async function acquire(
time,
mode,
signal,
{ box = null, keep = [], onFetched } = {},
) {
signal.throwIfAborted();
const key = cacheKey(time, box);
const key = cacheKey(time, mode, box);
const entry = images.get(key);
if (entry) {
images.delete(key);
@@ -485,8 +468,7 @@ export function createWeatherShell({
}
const result = await acquireWeatherImage(product, time, {
signal,
// The surface's shader applies the infrared transfer.
transfer: false,
mode,
size: box ? detailSize : size,
bbox: box,
maxBytes: MAX_IMAGE_BYTES,
@@ -582,7 +564,9 @@ export function createWeatherShell({
const job = { key, box: view, controller: new AbortController() };
detailJob = job;
job.timeout = setTimeout(() => job.controller.abort(), timeoutMs);
void acquire(current.time, job.controller.signal, { box: view })
void acquire(current.time, current.infrared, job.controller.signal, {
box: view,
})
.then(({ texture }) => installDetail(job, texture))
.catch(() => failDetail(job));
}
@@ -677,7 +661,6 @@ export function createWeatherShell({
loadMs: now() - frame.startedAt,
};
surface.setAlpha(alpha);
surface.setInfrared(infrared ? infraredAmount(frame.infrared) : 0);
applyVisibility();
lastError = null;
evict();
@@ -732,7 +715,7 @@ export function createWeatherShell({
return changed;
},
cancelPrefetch,
async prefetch(snapshot, time) {
async prefetch(snapshot, time, { infrared: mode = 'filtered' } = {}) {
let job;
try {
if (
@@ -746,8 +729,8 @@ export function createWeatherShell({
// Warm the next frame's detail window too while the window stays put.
const box =
view && viewBounds === boundsKey(snapshot.bounds) ? view : null;
const coarse = cacheKey(time);
const key = box ? cacheKey(time, box) : coarse;
const coarse = cacheKey(time, mode);
const key = box ? cacheKey(time, mode, box) : coarse;
if (prefetchJob?.key === key || prefetchedKey === key) return false;
cancelPrefetch();
job = { key, controller: new AbortController() };
@@ -756,8 +739,8 @@ export function createWeatherShell({
job.timeout = setTimeout(() => job.controller.abort(), timeoutMs);
// Both images at once: one after the other would outlast playback's step.
await Promise.all([
acquire(time, signal, { keep: box ? [key] : [] }),
box ? acquire(time, signal, { box, keep: [coarse] }) : null,
acquire(time, mode, signal, { keep: box ? [key] : [] }),
box ? acquire(time, mode, signal, { box, keep: [coarse] }) : null,
]);
signal.throwIfAborted();
if (prefetchJob === job) prefetchedKey = key;
@@ -799,7 +782,7 @@ export function createWeatherShell({
infrared: mode,
bounds,
product: snapshot.product,
key: cacheKey(time),
key: cacheKey(time, mode),
mosaic: { fetched: false, decodeMs: null, cached: false },
controller: new AbortController(),
startedAt: now(),
@@ -814,7 +797,7 @@ export function createWeatherShell({
});
watch(frame, signal);
frame.timeout = setTimeout(() => fail(frame), timeoutMs);
void acquire(time, frame.controller.signal, {
void acquire(time, mode, frame.controller.signal, {
onFetched: () => {
frame.mosaic.fetched = true;
},
+18 -35
View File
@@ -11,7 +11,6 @@ import {
WEATHER_SHELL_HEIGHTS,
} from './shellRendering.js';
import { NO_IMAGERY_HOST } from './imageryHost.js';
import { INFRARED_ALPHA_T0, INFRARED_ALPHA_T1 } from './infraredAlpha.js';
import {
createShellCesium,
createShellScene,
@@ -148,13 +147,12 @@ test('surface builds a flat raised rectangle drawn in primitive order without de
image: Cesium.Material.DefaultImageId,
detail: Cesium.Material.DefaultImageId,
alpha: 1,
infrared: 0,
window: { type: 'vec4', x: 0, y: 0, z: -1, w: -1 },
});
const { source } = template.fabric;
assert.match(
source,
/material\.alpha = c\.a \* mix\(1\.0, key, infrared\) \* alpha \* step\(1\.5, float\(imageDimensions\.x\)\);/,
/material\.alpha = c\.a \* alpha \* step\(1\.5, float\(imageDimensions\.x\)\);/,
'nothing draws before the full-extent image',
);
assert.match(
@@ -375,7 +373,6 @@ test('real Cesium builds the two-texture material, the opaque-pass appearance an
/uniform sampler2D image_\d+;/,
/uniform sampler2D detail_\d+;/,
/uniform float alpha_\d+;/,
/uniform float infrared_\d+;/,
/uniform vec4 window_\d+;/,
/uniform ivec3 imageDimensions_\d+;/,
/uniform ivec3 detailDimensions_\d+;/,
@@ -387,16 +384,9 @@ test('real Cesium builds the two-texture material, the opaque-pass appearance an
assert.match(shader, /step\(1\.5, float\(detailDimensions_\d+\.x\)\)/);
assert.match(
shader,
/material\.alpha = c\.a \* mix\(1\.0, key, infrared_\d+\) \* alpha_\d+ \* step\(1\.5, float\(imageDimensions_\d+\.x\)\);/,
/material\.alpha = c\.a \* alpha_\d+ \* step\(1\.5, float\(imageDimensions_\d+\.x\)\);/,
'the uniform is renamed, the material field is not',
);
// The cloud key the globe's draped imagery gets from infraredAlpha.js:
// brightest channel, 2.2 power, the shared thresholds.
assert.ok(
shader.includes(
`float key = smoothstep(${INFRARED_ALPHA_T0.toFixed(4)}, ${INFRARED_ALPHA_T1.toFixed(4)}, pow(max(c.r, max(c.g, c.b)), 2.2));`,
),
);
assert.match(shader, /float useDetail = inside \* step/, 'locals keep names');
const detail = { width: 4096, height: 2048 };
surface.setDetail(detail, { west: 0.1, south: 0.2, east: 0.3, north: 0.4 });
@@ -753,12 +743,12 @@ test('prefetch warms a decoded frame without staging and cancels cleanly', async
h.shell.clear();
});
test('infrared frames reach the shader as decoded, with the cloud key as a uniform; radar and lightning get none', async () => {
for (const [product, amount] of [
['clouds-regional', 1],
['clouds', 1],
['radar', 0],
['lightning', 0],
test('infrared frames get the display transfer; radar and lightning keep their pixels', async () => {
for (const [product, transfer] of [
['clouds-regional', true],
['clouds', true],
['radar', false],
['lightning', false],
]) {
const h = harness({ product });
const pending = h.shell.setFrame(snapshot(product), times[0]);
@@ -766,10 +756,11 @@ test('infrared frames reach the shader as decoded, with the cloud key as a unifo
const image = h.material().uniforms.image;
assert.equal(image.source, h.decoded[0], 'drawn into a canvas');
assert.equal(h.decoded[0].closed, true);
assert.equal(image.pixels, undefined, `${product}: pixels never rewritten`);
assert.equal(image.pixels !== undefined, transfer, product);
if (transfer)
assert.deepEqual([...image.pixels], [194, 0, 0, 98], 'clouds only');
h.render();
assert.equal(await pending, true);
assert.equal(h.material().uniforms.infrared, amount, product);
assert.equal(
h.shell.getDiagnostics().height,
WEATHER_SHELL_HEIGHTS[product],
@@ -778,28 +769,20 @@ test('infrared frames reach the shader as decoded, with the cloud key as a unifo
}
});
test('an infrared mode change is a uniform on the same image: no fetch, no new canvas', async () => {
test('an infrared mode change restages the same time with its own cached canvas', async () => {
const h = harness({ product: 'clouds' });
assert.equal(await show(h, times[0]), true);
const image = h.material().uniforms.image;
assert.equal(h.material().uniforms.infrared, 1);
const filtered = h.material().uniforms.image;
assert.equal(await show(h, times[0]), true, 'same frame is a no-op');
assert.equal(h.fetches.length, 1);
const renders = h.scene.renders;
assert.equal(await show(h, times[0], { infrared: 'full' }), true);
assert.equal(h.material().uniforms.image, image);
assert.equal(h.material().uniforms.infrared, 0);
const full = h.material().uniforms.image;
assert.notEqual(full, filtered);
assert.deepEqual([...full.pixels], [194, 0, 0, 200]);
assert.equal(h.shell.getDiagnostics().infrared, 'full');
assert.ok(h.scene.renders > renders, 'the new strength is drawn');
assert.equal(await show(h, times[0], { infrared: 40 }), true);
assert.equal(h.material().uniforms.infrared, 0.4);
assert.equal(h.shell.getDiagnostics().infrared, 40);
assert.equal(await show(h, times[0], { infrared: 'filtered' }), true);
assert.equal(h.material().uniforms.image, image);
assert.equal(h.material().uniforms.infrared, 1);
assert.equal(h.fetches.length, 1);
assert.equal(h.decoded.length, 1);
assert.equal(h.shell.getDiagnostics().cache.mosaics, 1);
assert.equal(h.material().uniforms.image, filtered, 'cache hit');
assert.equal(h.fetches.length, 2);
h.shell.clear();
});
+55 -11
View File
@@ -45,6 +45,39 @@ export function createGroundFloor({ terrain, signal }) {
return { lat: Number(lat.toFixed(3)), lon: Number(lon.toFixed(3)) };
}
/**
* One coordinate's coarse cell as an integer: `Number(deg.toFixed(3))` in
* thousandths of a degree, without the trip through a string. The floor is
* read for every aircraft on every fleet pass, and `coarseFloorCoord`'s two
* `toFixed` calls and the string keys built from them were most of the cost.
* `toFixed` rounds the exact decimal value; `deg * 1000` is within 1e-10 of
* it for |deg| < 1000, so the two can only round differently right at a
* half, and there the string decides.
* @param {number} deg
* @returns {number}
*/
function coarseCellIndex(deg) {
const scaled = deg * 1000;
if (
!(Math.abs(deg) < 1000) ||
Math.abs(scaled - Math.floor(scaled) - 0.5) < 1e-6
)
return Math.round(Number(deg.toFixed(3)) * 1000);
return Math.round(scaled);
}
/**
* Key of a coarse cell in `_meshCells`: the two indices packed into one
* number, or the text form for a longitude too far out of range to pack.
* @param {number} latIndex @param {number} lonIndex
* @returns {number|string}
*/
function meshCellKey(latIndex, lonIndex) {
return Math.abs(lonIndex) < 500_000 && Math.abs(latIndex) < 1e9
? latIndex * 1_000_000 + lonIndex
: `${latIndex},${lonIndex}`;
}
/**
* Pure floor clamp.
* - finite alt + finite ground → max(alt, ground + lift)
@@ -424,8 +457,8 @@ export function createGroundFloor({ terrain, signal }) {
// reported here by meshFloorSampler.js. Design principle (owner): NEVER
// below the visible surface; slightly above is always fine. The DEM stays
// the instant global prior/fallback and the sanity gate for samples.
/** @type {Map<string, Array<{lat: number, lon: number, h: number}>>}
* coarse cell key -> rendered-surface readings (m, ellipsoidal), each kept
/** @type {Map<number|string, Array<{lat: number, lon: number, h: number}>>}
* coarse cell key (`meshCellKey`) -> rendered-surface readings (m, ellipsoidal), each kept
* at the point it was probed. A read answers from the reading nearest the
* asked point, so a viaduct and the street beside it in one cell keep their
* own heights. */
@@ -468,8 +501,7 @@ export function createGroundFloor({ terrain, signal }) {
!Number.isFinite(heightM)
)
return;
const c = coarseFloorCoord(lat, lon);
const key = `${c.lat},${c.lon}`;
const key = meshCellKey(coarseCellIndex(lat), coarseCellIndex(lon));
if (!_meshCells.has(key)) _meshCells.set(key, [{ lat, lon, h: heightM }]);
}
@@ -497,7 +529,7 @@ export function createGroundFloor({ terrain, signal }) {
const cell = coarseFloorCoord(lat, lon);
const prior = cachedRealEllipsoidalGround(cell.lat, cell.lon);
if (!meshFloorSampleWithinPrior(heightM, prior)) return false;
const key = `${cell.lat},${cell.lon}`;
const key = meshCellKey(coarseCellIndex(lat), coarseCellIndex(lon));
const readings = _meshCells.get(key);
if (!readings) {
_meshCells.set(key, [{ lat, lon, h: heightM }]);
@@ -538,8 +570,16 @@ export function createGroundFloor({ terrain, signal }) {
function cachedMeshFloor(lat, lon) {
if (!_meshPreferred) return null;
if (!Number.isFinite(lat) || !Number.isFinite(lon)) return null;
const c = coarseFloorCoord(lat, lon);
const readings = _meshCells.get(`${c.lat},${c.lon}`);
return meshFloorInCell(
lat,
lon,
meshCellKey(coarseCellIndex(lat), coarseCellIndex(lon)),
);
}
/** `cachedMeshFloor` for a caller that already has the point's cell key. */
function meshFloorInCell(lat, lon, key) {
const readings = _meshCells.get(key);
if (!readings) return null;
if (readings.length === 1) return readings[0].h;
const k = Math.cos((lat * Math.PI) / 180) ** 2;
@@ -572,10 +612,14 @@ export function createGroundFloor({ terrain, signal }) {
*/
function cachedGroundFloor(lat, lon) {
if (!Number.isFinite(lat) || !Number.isFinite(lon)) return null;
const mesh = cachedMeshFloor(lat, lon);
if (mesh != null) return mesh;
const c = coarseFloorCoord(lat, lon);
return cachedRealEllipsoidalGround(c.lat, c.lon);
// The cell is worked out once, as integers, for both reads.
const latIndex = coarseCellIndex(lat);
const lonIndex = coarseCellIndex(lon);
if (_meshPreferred) {
const mesh = meshFloorInCell(lat, lon, meshCellKey(latIndex, lonIndex));
if (mesh != null) return mesh;
}
return cachedRealEllipsoidalGround(latIndex / 1000, lonIndex / 1000);
}
/**
+176
View File
@@ -0,0 +1,176 @@
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { createGroundFloor } from './groundFloor.js';
import { createTerrainHeights } from './terrainHeights.js';
/** Counts Number.prototype.toFixed calls made while `run` executes. */
function countToFixed(run) {
const original = Number.prototype.toFixed;
let calls = 0;
Number.prototype.toFixed = function (...args) {
calls += 1;
return original.apply(this, args);
};
try {
run();
} finally {
Number.prototype.toFixed = original;
}
return calls;
}
/**
* The floor read as it was before the cell became an integer: the coarse cell
* through `toFixed(3)`, mesh cells and DEM cells under string keys.
*/
function referenceFloor() {
const mesh = new Map();
const dem = new Map();
const coarse = (lat, lon) => ({
lat: Number(lat.toFixed(3)),
lon: Number(lon.toFixed(3)),
});
const demKey = (lat, lon) => `${lat.toFixed(5)},${lon.toFixed(5)}`;
return {
reportMesh(lat, lon, h) {
const c = coarse(lat, lon);
const key = `${c.lat},${c.lon}`;
if (!mesh.has(key)) mesh.set(key, [{ lat, lon, h }]);
},
storeDem(lat, lon, h) {
dem.set(demKey(lat, lon), h);
},
read(lat, lon, meshPreferred = true) {
if (!Number.isFinite(lat) || !Number.isFinite(lon)) return null;
const c = coarse(lat, lon);
const readings = meshPreferred ? mesh.get(`${c.lat},${c.lon}`) : null;
if (readings) return readings[0].h;
return dem.get(demKey(c.lat, c.lon)) ?? null;
},
};
}
/** A real terrain cache whose every resolved point takes the height asked for. */
async function warmTerrain(points) {
const heights = new Map(points.map((p) => [`${p.lat},${p.lon}`, p.h]));
const terrain = createTerrainHeights({
source: {
async getHeights(chunk) {
return chunk.map((item) => ({
ellipsoid: heights.get(`${item.lat},${item.lon}`),
}));
},
},
});
await terrain.resolveEllipsoidalGround(
points.map(({ lat, lon }) => ({ lat, lon })),
);
return terrain;
}
/** Deterministic pseudo-random numbers in [0, 1). */
function random(seed) {
let state = seed;
return () => {
state = (state * 1664525 + 1013904223) % 4294967296;
return state / 4294967296;
};
}
/** Coordinates that land on, and just either side of, a coarse-cell edge. */
function edgeCoordinates() {
const out = [];
for (const base of [0, 0.0005, -0.0005, 12.3455, -12.3455, 89.9995, 179.9995])
for (const nudge of [0, 1e-13, -1e-13, 1e-9, -1e-9, 1e-7, -1e-7, 4e-4])
out.push(base + nudge);
// Thousandths whose doubles sit a hair either side of the half.
for (let k = -40; k <= 40; k += 1) out.push((k + 0.5) / 1000);
out.push(-0, 1.0005, 1.0015, 2.5, -2.5, 1e-7, -1e-7);
return out;
}
test('the floor read answers exactly as the string-keyed read did', async () => {
const next = random(20261113);
const cells = [];
for (let i = 0; i < 400; i += 1) {
cells.push({
lat: Number((next() * 170 - 85).toFixed(3)),
lon: Number((next() * 360 - 180).toFixed(3)),
h: Math.round(next() * 4000),
});
}
// Cells along the equator and the prime meridian, where a cell index is 0.
for (const edge of edgeCoordinates()) {
const lat = Number(edge.toFixed(3));
cells.push({ lat: Math.max(-89, Math.min(89, lat)), lon: 10, h: 77 });
cells.push({ lat: 10, lon: lat, h: 88 });
}
const reference = referenceFloor();
for (const c of cells) reference.storeDem(c.lat, c.lon, c.h);
const terrain = await warmTerrain(cells);
const groundFloor = createGroundFloor({ terrain });
// A rendered-surface reading in every seventh cell, taken off its centre.
cells.forEach((c, i) => {
if (i % 7) return;
const lat = c.lat + 0.0002;
const lon = c.lon - 0.0003;
reference.reportMesh(lat, lon, c.h + 3);
groundFloor.reportMeshFloorCell(lat, lon, c.h + 3);
});
const probes = [];
for (const c of cells) {
probes.push([c.lat, c.lon]);
probes.push([c.lat + (next() - 0.5) * 0.002, c.lon + (next() - 0.5) * 0.002]);
}
for (const a of edgeCoordinates())
for (const b of [10, 10.0004, -0, 0.0005, -0.0005]) {
probes.push([a, b]);
probes.push([b, a]);
}
probes.push([NaN, 1], [1, Infinity], [95, 400], [10, 720.1234]);
for (const meshPreferred of [true, false]) {
groundFloor.setMeshFloorPreferred(meshPreferred);
let hits = 0;
for (const [lat, lon] of probes) {
const expected = reference.read(lat, lon, meshPreferred);
assert.equal(
groundFloor.cachedGroundFloor(lat, lon),
expected,
`floor at ${lat}, ${lon} (mesh ${meshPreferred})`,
);
if (expected !== null) hits += 1;
}
assert.ok(hits > 400, `${hits} probes found a floor`);
}
});
test('reading a floor formats no numbers, hit or miss', async () => {
const cell = { lat: 51.47, lon: -0.454, h: 25 };
const terrain = await warmTerrain([cell]);
const groundFloor = createGroundFloor({ terrain });
groundFloor.reportMeshFloorCell(48.3537, 11.7861, 453);
const calls = countToFixed(() => {
// The DEM cell, a rendered-surface cell, and open ocean with neither.
assert.equal(groundFloor.cachedGroundFloor(51.4702, -0.4541), 25);
assert.equal(groundFloor.cachedGroundFloor(48.3538, 11.7862), 453);
for (let i = 0; i < 1000; i += 1)
assert.equal(groundFloor.cachedGroundFloor(30 + i * 0.011, -40.2), null);
});
// Before, each read made six toFixed calls (the cell twice, the DEM key).
assert.equal(calls, 0);
});
test('a reading reported at a point is found from anywhere in its cell', async () => {
const terrain = await warmTerrain([{ lat: 41.5, lon: -97.66, h: 120 }]);
const groundFloor = createGroundFloor({ terrain });
assert.equal(groundFloor.reportValidatedMeshFloorCell(41.5003, -97.6602, 129), true);
assert.equal(groundFloor.cachedMeshFloor(41.4996, -97.6598), 129);
assert.equal(groundFloor.cachedGroundFloor(41.4996, -97.6598), 129);
// The next cell over has no reading and no DEM entry.
assert.equal(groundFloor.cachedMeshFloor(41.5006, -97.6602), null);
assert.equal(groundFloor.cachedGroundFloor(41.5006, -97.6602), null);
});
+125 -3
View File
@@ -86,6 +86,89 @@ export function createTerrainHeights({
*/
const cache = new Map();
/**
* The same entries under a numeric key, for the synchronous reads. Those run
* for every aircraft on every fleet pass (about 94,000 a second with 7,500
* aircraft in view), nearly all of them misses, and building the string key
* (two `toFixed` calls and a concatenation) was most of what a read cost.
* Kept in step with `cache` by `cacheStore`, eviction and the abort handler.
* `at` is the value of `moves` when the entry last became the newest.
* @type {Map<number, {key: string, entry: object, at: number}>}
*/
const cells = new Map();
/** Counts every move to the newest end of `cache`: stores and promotions. */
let moves = 0;
/**
* A read promotes its entry only once this many moves have passed since it
* was last the newest. An entry cannot reach the cold end in fewer than
* CACHE_MAX_ENTRIES - 1 moves, so one read at least this often is never
* evicted, without rewriting the Map on every read.
*/
const PROMOTE_AFTER_MOVES = Math.floor(CACHE_MAX_ENTRIES / 2);
/**
* `value.toFixed(5)` as an integer count of 1e-5 steps, or null when the
* fast arithmetic cannot be trusted to match it. `toFixed` rounds the exact
* decimal value; `value * 1e5` is within 2e-8 of it for |value| < 1000, so
* the two can only round differently this close to a half. A negative value
* that rounds to zero is left to the string too: it formats as "-0.00000",
* a different key from "0.00000".
* @param {number} value
* @returns {number|null}
*/
function fixed5Index(value) {
if (!(Math.abs(value) < 1000)) return null;
const scaled = value * 1e5;
if (Math.abs(scaled - Math.floor(scaled) - 0.5) < 1e-6) return null;
const index = Math.round(scaled);
return Object.is(index, -0) ? null : index;
}
/** Indices at or past this (500 degrees) do not pack into one integer. */
const CELL_INDEX_LIMIT = 5e7;
/**
* The numeric key for a coordinate: its two 5-decimal indices packed into
* one integer (below 2^53), or null when the coordinate has to go through
* its string key instead.
* @param {number} lat
* @param {number} lon
* @returns {number|null}
*/
function cellKey(lat, lon) {
const latIndex = fixed5Index(lat);
const lonIndex = fixed5Index(lon);
if (
latIndex === null ||
lonIndex === null ||
Math.abs(latIndex) >= CELL_INDEX_LIMIT ||
Math.abs(lonIndex) >= CELL_INDEX_LIMIT
)
return null;
return latIndex * 1e8 + lonIndex;
}
/**
* The numeric key of a stored string key. The text is the rounded
* coordinate itself, so this is exact. Null for a key `cellKey` never
* produces; such a key is only ever read through its string.
* @param {string} key
* @returns {number|null}
*/
function cellKeyOf(key) {
const comma = key.indexOf(',');
const latIndex = Math.round(Number(key.slice(0, comma)) * 1e5);
const lonIndex = Math.round(Number(key.slice(comma + 1)) * 1e5);
if (
!(Math.abs(latIndex) < CELL_INDEX_LIMIT) ||
!(Math.abs(lonIndex) < CELL_INDEX_LIMIT) ||
Object.is(latIndex, -0) ||
Object.is(lonIndex, -0)
)
return null;
return latIndex * 1e8 + lonIndex;
}
/**
* Store an entry and drop the coldest ones once past the cap. `Map` iterates
* in insertion order, so deleting the first key evicts the least recently
@@ -101,10 +184,15 @@ export function createTerrainHeights({
function cacheStore(key, entry) {
cache.delete(key);
cache.set(key, entry);
moves += 1;
const cell = cellKeyOf(key);
if (cell !== null) cells.set(cell, { key, entry, at: moves });
while (cache.size > CACHE_MAX_ENTRIES) {
const coldest = cache.keys().next().value;
if (coldest === undefined) break;
cache.delete(coldest);
const coldestCell = cellKeyOf(coldest);
if (coldestCell !== null) cells.delete(coldestCell);
}
}
@@ -119,9 +207,36 @@ export function createTerrainHeights({
if (entry === undefined) return undefined;
cache.delete(key);
cache.set(key, entry);
moves += 1;
const cell = cellKeyOf(key);
const held = cell === null ? undefined : cells.get(cell);
if (held) held.at = moves;
return entry;
}
/**
* The synchronous read: the entry for a coordinate, or undefined on a miss.
* Same answer as `cacheTouch(cacheKey(lat, lon))`, without building the
* string, and promoting only an entry that has drifted towards the cold end
* (see PROMOTE_AFTER_MOVES).
* @param {number} lat
* @param {number} lon
* @returns {{ellipsoid: number, source: 'reearth'|'geoid-fallback', retryAt?: number}|undefined}
*/
function cacheRead(lat, lon) {
const cell = cellKey(lat, lon);
if (cell === null) return cacheTouch(cacheKey(lat, lon));
const held = cells.get(cell);
if (held === undefined) return undefined;
if (moves - held.at > PROMOTE_AFTER_MOVES) {
cache.delete(held.key);
cache.set(held.key, held.entry);
moves += 1;
held.at = moves;
}
return held.entry;
}
/**
* Builds the rounded cache key shared between the in-memory cache and the
* `points` string sent to the proxy (see file header, point 2).
@@ -142,7 +257,7 @@ export function createTerrainHeights({
* @returns {number|null}
*/
function cachedEllipsoidalGround(lat, lon) {
const entry = cacheTouch(cacheKey(lat, lon));
const entry = cacheRead(lat, lon);
return entry ? entry.ellipsoid : null;
}
@@ -159,7 +274,7 @@ export function createTerrainHeights({
* @returns {number|null}
*/
function cachedRealEllipsoidalGround(lat, lon) {
const entry = cacheTouch(cacheKey(lat, lon));
const entry = cacheRead(lat, lon);
return entry && entry.source === 'reearth' ? entry.ellipsoid : null;
}
@@ -370,7 +485,14 @@ export function createTerrainHeights({
});
}
signal?.addEventListener('abort', () => cache.clear(), { once: true });
signal?.addEventListener(
'abort',
() => {
cache.clear();
cells.clear();
},
{ once: true },
);
return {
cachedEllipsoidalGround,
cachedRealEllipsoidalGround,
+148
View File
@@ -169,3 +169,151 @@ test('an out-of-range cache bound falls back to the default instead of disabling
);
}
});
/** Counts Number.prototype.toFixed calls made while `run` executes. */
function countToFixed(run) {
const original = Number.prototype.toFixed;
let calls = 0;
Number.prototype.toFixed = function (...args) {
calls += 1;
return original.apply(this, args);
};
try {
run();
} finally {
Number.prototype.toFixed = original;
}
return calls;
}
test('a synchronous read finds exactly the entry its 5-decimal key names', async () => {
// Stored coordinates, and for each the reads that must and must not reach it.
// The expectation is the string key the cache has always used.
const stored = [
[0, 0.001],
[51.47, -0.454],
[-33.94611, 151.17722],
[0.00001, 0.00002],
[-0.00001, 0.00003],
[89.99999, 179.99999],
[-89.99999, -179.99999],
[12.345675, 98.765435],
[1e-7, 5],
[-1e-7, 6],
[7, -1e-7],
[200.5, 361.25],
[10, 500],
[10, 499.999999],
];
const key = (lat, lon) => `${lat.toFixed(5)},${lon.toFixed(5)}`;
const heights = new Map(
stored.map(([lat, lon], i) => [key(lat, lon), 100 + i]),
);
const terrain = createTerrainHeights({
source: {
async getHeights(chunk) {
return chunk.map((item) => ({ ellipsoid: heights.get(item.key) }));
},
},
});
await terrain.resolveEllipsoidalGround(
stored.map(([lat, lon]) => ({ lat, lon })),
);
const probes = [];
const nudges = [
0, 1e-13, -1e-13, 1e-9, -1e-9, 4e-6, -4e-6, 4.9999e-6, 5e-6, -5e-6,
5.0001e-6, 1e-5, -1e-5,
];
for (const [lat, lon] of stored)
for (const a of nudges)
for (const b of [0, 4e-6, -5e-6])
probes.push([lat + a, lon + b], [lat + b, lon + a]);
probes.push([0, 0], [-0, 0.001], [0, -0], [NaN, 1], [1, Infinity], [1e21, 2]);
let hits = 0;
for (const [lat, lon] of probes) {
const expected = heights.get(key(lat, lon)) ?? null;
assert.equal(
terrain.cachedEllipsoidalGround(lat, lon),
expected,
`plain read at ${lat}, ${lon}`,
);
assert.equal(
terrain.cachedRealEllipsoidalGround(lat, lon),
expected,
`real read at ${lat}, ${lon}`,
);
if (expected !== null) hits += 1;
}
assert.ok(
hits > 100 && hits < probes.length,
`${hits} of ${probes.length} reads hit`,
);
});
test('a synchronous read of an ordinary coordinate formats no numbers', async () => {
const terrain = createTerrainHeights({ source: stubSource() });
await terrain.resolveEllipsoidalGround([pointAt(1), pointAt(2)]);
const calls = countToFixed(() => {
assert.equal(
terrain.cachedRealEllipsoidalGround(0, lonAt(1)),
heightAt(lonAt(1)),
);
assert.equal(
terrain.cachedEllipsoidalGround(0, lonAt(2)),
heightAt(lonAt(2)),
);
// Misses are the common case: an aircraft over a cell nobody resolved.
for (let i = 10; i < 1010; i += 1)
assert.equal(terrain.cachedRealEllipsoidalGround(45.123, lonAt(i)), null);
});
// Before, every read built its key with two toFixed calls.
assert.equal(calls, 0);
});
test('an entry read again and again is not rewritten on each read, and still outlives the rest', async () => {
const terrain = createTerrainHeights({
source: stubSource(),
maxCacheEntries: 8,
});
for (let i = 0; i < 8; i += 1)
await terrain.resolveEllipsoidalGround([pointAt(i)]);
// Coordinate 0 is the coldest. Read it between stores, as a parked contact
// would; the seven newer ones are never read.
for (let i = 8; i < 15; i += 1) {
for (let read = 0; read < 50; read += 1)
assert.equal(
terrain.cachedEllipsoidalGround(0, lonAt(0)),
heightAt(lonAt(0)),
);
await terrain.resolveEllipsoidalGround([pointAt(i)]);
}
assert.equal(
terrain.cachedEllipsoidalGround(0, lonAt(0)),
heightAt(lonAt(0)),
);
for (let i = 1; i < 8; i += 1)
assert.equal(
terrain.cachedEllipsoidalGround(0, lonAt(i)),
null,
`unread coordinate ${i} was evicted`,
);
});
test('an evicted coordinate is gone from the synchronous read too', async () => {
const terrain = createTerrainHeights({
source: stubSource(),
maxCacheEntries: 2,
});
for (let i = 0; i < 5; i += 1)
await terrain.resolveEllipsoidalGround([pointAt(i)]);
for (let i = 0; i < 3; i += 1)
assert.equal(terrain.cachedRealEllipsoidalGround(0, lonAt(i)), null);
for (let i = 3; i < 5; i += 1)
assert.equal(
terrain.cachedRealEllipsoidalGround(0, lonAt(i)),
heightAt(lonAt(i)),
);
});