Performance optimizations

This commit is contained in:
James Babcock committed 2026-08-20 02:46:03 -07:00
1 parent e49c699062
commit f6035ea9ec
18 files changed
+715 -158

No files matched your search

+25 -5
View File
@@ -65,11 +65,21 @@ export type InProcessAppOptions = {
};
export function createInProcessWindow(options: InProcessWindowOptions): InProcessWindow {
const requestRender = () => {
void render(0).catch((error) => {
/**
* Repaint under a new frame. causeFrameId links it to the frame that made it
* necessary (a stale-cache paint asking for a redo); 0 for an app-initiated
* repaint. Each repaint gets a frame either way: an app-initiated one (a
* clock tick, arriving data) is a real screen update with real latency, and
* submitting it under frame 0 made it invisible in the timing export except
* as an anonymous "superseded by frame#0" line in some other frame's log.
*/
const renderInNewFrame = (causeFrameId: number) => {
void render(frameTimings.startFrame(`render:${options.windowId}`, causeFrameId)).catch((error) => {
console.error(`${options.windowId} render failed: ${error}`);
});
};
// Handed to app code as a bare callback, so it takes no arguments.
const requestRender = () => renderInNewFrame(0);
let heightMode = options.heightMode ?? "min";
const stack = new LayerStack(
options.baseLayer,
@@ -91,16 +101,21 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces
frameTimings.finishFrame(frameId, "discarded: window closed");
return;
}
frameTimings.annotateFrame(frameId, `window=${options.windowId}`);
const wantFreshData = nextRenderWantsFreshData;
nextRenderWantsFreshData = false;
beginRenderPass(!wantFreshData);
const paintStartedAtMs = Date.now();
const planes = stack.paint();
// runWithFrame so leaf data sources (notification icons, calendar) attach
// their own spans to this frame.
const planes = frameTimings.span(frameId, "paint", () =>
frameTimings.runWithFrame(frameId, () => stack.paint()),
);
const paintUsedStaleData = endRenderPass();
await options.submitFrame(planes, Date.now() - paintStartedAtMs, frameId);
if (paintUsedStaleData) {
nextRenderWantsFreshData = true;
requestRender();
renderInNewFrame(frameId);
}
}
@@ -165,7 +180,12 @@ export function createInProcessWindow(options: InProcessWindowOptions): InProces
await render(frameId);
return;
}
await stack.handleInput(event);
// Spanned separately from the paint: an app's input handler may await
// real work (launching an app, a network call), and that used to show up
// as a bare multi-second gap inside the shell's handle-input span.
await frameTimings.spanAsync(frameId, `app-input:${options.windowId}`, () =>
stack.handleInput(event),
);
await render(frameId);
},
requestRender,
+14
View File
@@ -599,6 +599,20 @@ class Shell {
};
}
/**
* Compact description of where input is currently going, for the frame
* timing export: "which app was drawing" is usually the first thing you need
* to interpret an input-to-display latency, and it is not recoverable from
* the input event itself.
*/
describeInputTarget(): string {
const foreground = this.foregroundWindow()?.windowId ?? "none";
if (!this.screenOn) return `fg=${foreground} target=screen-off`;
if (this.activeVoiceLayer) return `fg=${foreground} target=voice`;
if (!this.stack.isAtBase()) return `fg=${foreground} target=shell-overlay`;
return `fg=${foreground} target=${this.focus}`;
}
/** Whether a window is the current input target (foreground + focus in-window). */
isWindowFocused(windowId: string): boolean {
return this.screenOn && this.focus === "window" && this.foregroundWindow()?.windowId === windowId;
+2
View File
@@ -3,6 +3,7 @@ import { windowIcon } from "./chrome-layer";
import { type IconName } from "../../graphics/icons";
import { toolRegistry, type ToolResult, type ToolSpec } from "../../assistant/tool-registry";
import { appViewportSize, type WindowHeightMode } from "./geometry";
import * as frameTimings from "../../native/frame-timings";
import { shell, type ShellWindow } from "./shell";
/**
@@ -293,6 +294,7 @@ export class WorkerAppHost {
},
drawIcon: windowIcon(spec.icon, spec.iconLetter, spec.iconGlyph),
handleInput: (event, frameId) => {
frameTimings.logFrame(frameId, `input posted to the ${this.options.appId} worker`);
this.post({
type: "input",
windowId: spec.windowId,