Optimize phone CPU usage

This commit is contained in:
James Babcock committed 2026-09-27 23:16:41 -07:00
1 parent 449cb0bf73
commit 45d2ece0d2
19 files changed
+485 -103

No files matched your search

+27 -7
View File
@@ -36,14 +36,34 @@ export type DisplayList = {
};
export type PlacedDisplayList = { displayList: DisplayList; x: number; y: number; width: number; height: number; depth: number };
/**
* Growable byte buffer. Lists are encoded on every frame that carries one, and
* twice more for its fingerprint and the preview; resources (whole menu rows,
* scroll strips) are tens of KB, so they go in with one set() rather than a
* push per byte.
*/
class Writer {
bytes: number[] = [];
u8(n: number): void { this.bytes.push(integer(n, 0, 255)); }
private buffer = new Uint8Array(256);
length = 0;
private reserve(count: number): void {
const needed = this.length + count;
if (needed <= this.buffer.length) return;
let size = this.buffer.length * 2;
while (size < needed) size *= 2;
const grown = new Uint8Array(size);
grown.set(this.buffer.subarray(0, this.length));
this.buffer = grown;
}
u8(n: number): void { integer(n, 0, 255); this.reserve(1); this.buffer[this.length++] = n; }
i8(n: number): void { this.u8(integer(n, -128, 127) & 255); }
u16(n: number): void { integer(n, 0, 65535); this.bytes.push(n & 255, n >>> 8); }
u16(n: number): void { integer(n, 0, 65535); this.reserve(2); this.buffer[this.length++] = n & 255; this.buffer[this.length++] = n >>> 8; }
i16(n: number): void { this.u16(integer(n, -32768, 32767) & 65535); }
u32(n: number): void { integer(n, 0, 4294967295); this.u16(n & 65535); this.u16(n >>> 16); }
raw(bytes: ArrayLike<number>): void { for (let i = 0; i < bytes.length; i++) this.bytes.push(bytes[i]); }
raw(bytes: ArrayLike<number>): void { this.reserve(bytes.length); this.buffer.set(bytes, this.length); this.length += bytes.length; }
/** The written bytes, in a buffer of their own. */
bytes(): Uint8Array { return this.buffer.slice(0, this.length); }
/** A view of the written bytes; valid until the next write. */
view(): Uint8Array { return this.buffer.subarray(0, this.length); }
}
/** Length-delimited bridge envelope; call operands use fixed widths except extended x/y. */
@@ -85,9 +105,9 @@ export function encodeDisplayList(placed: PlacedDisplayList, now = Date.now()):
}
}
}
if (out.bytes.length > 4 * 1024 * 1024) throw new Error('Display list exceeds bridge limit');
const header = new Writer(); header.u8(DISPLAY_LIST_RECORD); header.u32(out.bytes.length); header.raw(out.bytes);
return Uint8Array.from(header.bytes);
if (out.length > 4 * 1024 * 1024) throw new Error('Display list exceeds bridge limit');
const header = new Writer(); header.u8(DISPLAY_LIST_RECORD); header.u32(out.length); header.raw(out.view());
return header.bytes();
}
/** Replay-record sizes by tag, as in glyph-wire's frame draw buffer. */
+47 -32
View File
@@ -277,17 +277,28 @@ export class GrayImage {
const destX = dx | 0;
const destY = dy | 0;
for (let row = 0; row < copyHeight; row++) {
const sy = srcY + row;
const ty = destY + row;
if (sy < 0 || sy >= source.height || ty < 0 || ty >= this.height) continue;
for (let col = 0; col < copyWidth; col++) {
const sx = srcX + col;
const tx = destX + col;
if (sx < 0 || sx >= source.width || tx < 0 || tx >= this.width) continue;
const value = source.pixels[sy * source.width + sx] ?? 0;
// Clip once, then copy row spans: flattening a frame blits every plane,
// and per-pixel bounds checks made that a sizeable share of a paint.
const colStart = Math.max(0, -destX);
const colEnd = Math.min(copyWidth, source.width - srcX, this.width - destX);
const rowStart = Math.max(0, -destY);
const rowEnd = Math.min(copyHeight, source.height - srcY, this.height - destY);
const span = colEnd - colStart;
const src = source.pixels;
const dst = this.pixels;
// A blit within one image copies pixel by pixel, forwards, as it always has.
const bulk = !opts.transparentZero && source !== this;
for (let row = rowStart; span > 0 && row < rowEnd; row++) {
const from = (srcY + row) * source.width + srcX + colStart;
const to = (destY + row) * this.width + destX + colStart;
if (bulk) {
dst.set(src.subarray(from, from + span), to);
continue;
}
for (let i = 0; i < span; i++) {
const value = src[from + i]!;
if (opts.transparentZero && value === 0) continue;
this.pixels[ty * this.width + tx] = value;
dst[to + i] = value;
}
}
@@ -398,7 +409,7 @@ export class GrayImage {
hash = mixInt(hash, placed.source.height);
hash = mixInt(hash, placed.source.sourceContentHash32());
if (placed.presentation) { const p = placed.presentation; for (const value of [p.radius, p.background, p.border, p.depth, p.mode === "image" ? 1 : p.mode === "masked-image" ? 2 : 0]) hash = mixInt(hash, value);
if (p.displayList) for (const byte of encodeDisplayList({ displayList: p.displayList, x: 0, y: 0, width: placed.source.width, height: placed.source.height, depth: p.depth }, p.displayList.timeline?.startedAt ?? 0)) hash = mixInt(hash, byte);
if (p.displayList) hash = mixInt(hash, bytesHash32(encodeDisplayList({ displayList: p.displayList, x: 0, y: 0, width: placed.source.width, height: placed.source.height, depth: p.depth }, p.displayList.timeline?.startedAt ?? 0)));
}
} else {
hash = mixInt(hash, 0xf17e);
@@ -415,28 +426,9 @@ export class GrayImage {
return hash >>> 0;
}
/**
* FNV-1a over the raster pixels. Four bytes at a time where alignment
* allows: this runs on every frame's fingerprint, over the whole screen, on
* the thread that is about to hand the frame to the transport.
*/
/** Hash of the raster pixels; see bytesHash32. */
private pixelsHash32(): number {
const pixels = this.pixels;
let hash = 2166136261;
let i = 0;
if ((pixels.byteOffset & 3) === 0) {
const words = new Uint32Array(pixels.buffer, pixels.byteOffset, pixels.length >>> 2);
for (let w = 0; w < words.length; w++) {
hash ^= words[w]!;
hash = Math.imul(hash, 16777619);
}
i = words.length << 2;
}
for (; i < pixels.length; i++) {
hash ^= pixels[i]!;
hash = Math.imul(hash, 16777619);
}
return hash >>> 0;
return bytesHash32(this.pixels);
}
/**
@@ -744,6 +736,29 @@ function clampByte(value: number): number {
}
/** Fold a 32-bit int into an FNV-style running hash. */
/**
* FNV-1a over bytes. Four bytes at a time where alignment allows: this runs on
* every frame's fingerprint, over the whole screen, on the thread that is
* about to hand the frame to the transport.
*/
function bytesHash32(bytes: Uint8Array): number {
let hash = 2166136261;
let i = 0;
if ((bytes.byteOffset & 3) === 0) {
const words = new Uint32Array(bytes.buffer, bytes.byteOffset, bytes.length >>> 2);
for (let w = 0; w < words.length; w++) {
hash ^= words[w]!;
hash = Math.imul(hash, 16777619);
}
i = words.length << 2;
}
for (; i < bytes.length; i++) {
hash ^= bytes[i]!;
hash = Math.imul(hash, 16777619);
}
return hash >>> 0;
}
function mixInt(hash: number, value: number): number {
hash ^= value | 0;
return Math.imul(hash, 16777619);