M7: hardening and final pass

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
lucioandClaude Opus 5.5 committed 2026-11-04 05:10:46 -08:00
1 parent 00ee2a6d12
commit ae898a2787
30 files changed
+878 -107

No files matched your search

+22 -15
View File
@@ -1,5 +1,7 @@
import { releaseImage, type TileImage } from "./scheduler";
export interface CachedTile {
bitmap: ImageBitmap;
bitmap: TileImage;
bytes: number;
at: number;
}
@@ -8,22 +10,27 @@ interface NavigatorWithMemory extends Navigator {
deviceMemory?: number;
}
export function cacheBudget(): number {
const mb = 1024 * 1024;
const mb = 1024 * 1024;
export function isConstrainedDevice(): boolean {
const memory = (navigator as NavigatorWithMemory).deviceMemory;
const coarse = typeof matchMedia === "function" && matchMedia("(pointer: coarse)").matches;
const smallScreen = Math.min(screen.width, screen.height) <= 820;
if ((memory !== undefined && memory <= 4) || (coarse && smallScreen)) {
return 120 * mb;
if (typeof memory === "number" && memory <= 4) {
return true;
}
return 250 * mb;
const coarse = typeof matchMedia === "function" && matchMedia("(pointer: coarse)").matches;
const shortSide = Math.min(screen.width, screen.height);
return coarse && shortSide <= 820;
}
export function cacheBudget(): number {
return isConstrainedDevice() ? 120 * mb : 250 * mb;
}
export class TileCache {
private readonly entries = new Map<string, CachedTile>();
private used = 0;
constructor(private readonly budget: number) {}
constructor(readonly budget: number) {}
get(id: string): CachedTile | undefined {
const entry = this.entries.get(id);
@@ -38,22 +45,22 @@ export class TileCache {
return this.entries.has(id);
}
put(id: string, bitmap: ImageBitmap): void {
put(id: string, bitmap: TileImage): void {
const existing = this.entries.get(id);
if (existing) {
if (existing.bitmap !== bitmap) {
bitmap.close();
releaseImage(bitmap);
}
return;
}
const bytes = bitmap.width * bitmap.height * 4;
this.entries.set(id, { bitmap, bytes, at: performance.now() });
this.used += bytes;
this.evict();
this.trim(this.budget);
}
private evict(): void {
while (this.used > this.budget && this.entries.size > 1) {
trim(limit: number): void {
while (this.used > limit && this.entries.size > 0) {
const oldest = this.entries.keys().next();
if (oldest.done) {
return;
@@ -62,7 +69,7 @@ export class TileCache {
this.entries.delete(oldest.value);
if (entry) {
this.used -= entry.bytes;
entry.bitmap.close();
releaseImage(entry.bitmap);
}
}
}