forked from Blendman974/faceclaw
Performance optimizations
This commit is contained in:
1 parent
e49c699062
commit
f6035ea9ec
18 files changed
+715
-158
No files matched your search
+41
-11
@@ -3,13 +3,17 @@
|
||||
* instrumentation; see FrameTimings.java). Frame ID 0 means "no frame" and is
|
||||
* ignored by every method, so callers never need to null-check.
|
||||
*
|
||||
* Because the JS side is single-threaded, this module also tracks a "current
|
||||
* frame" so that leaf drawing/IO code can record spans without threading a
|
||||
* frame ID through every call signature.
|
||||
* Frames form a tree: pass the causing frame's ID as `parentFrameId` when one
|
||||
* frame fans out into several (an input event that repaints both the focused
|
||||
* app and the shell chrome), and the export nests them under one timeline
|
||||
* instead of showing unrelated frames.
|
||||
*
|
||||
* Because each JS context is single-threaded, this module also tracks a
|
||||
* "current frame" so that leaf drawing/IO code can record spans without
|
||||
* threading a frame ID through every call signature.
|
||||
*/
|
||||
|
||||
declare const com: any;
|
||||
declare const global: any;
|
||||
|
||||
let javaInstance: any = null;
|
||||
let javaUnavailable = false;
|
||||
@@ -20,12 +24,11 @@ let currentFrameId = 0;
|
||||
|
||||
function getJava(): any {
|
||||
if (javaInstance === null && !javaUnavailable) {
|
||||
// Deliberately not gated on global.isAndroid: this runs in app workers
|
||||
// too, and a worker that had not yet imported the core globals would
|
||||
// silently drop every frame it renders.
|
||||
try {
|
||||
if (typeof com !== "undefined" && global.isAndroid) {
|
||||
javaInstance = com.faceclaw.app.FrameTimings.getInstance();
|
||||
} else {
|
||||
javaUnavailable = true;
|
||||
}
|
||||
javaInstance = com.faceclaw.app.FrameTimings.getInstance();
|
||||
} catch {
|
||||
javaUnavailable = true;
|
||||
}
|
||||
@@ -33,10 +36,23 @@ function getJava(): any {
|
||||
return javaInstance;
|
||||
}
|
||||
|
||||
export function startFrame(reason: string): number {
|
||||
/** Begin a frame; parentFrameId links it under the frame that caused it. */
|
||||
export function startFrame(reason: string, parentFrameId = 0): number {
|
||||
const java = getJava();
|
||||
if (!java) return fallbackNextFrameId--;
|
||||
return Number(java.startFrame(reason));
|
||||
// Guarded: requestRender-style callbacks can be invoked with an unrelated
|
||||
// argument, and a NaN would blow up the int marshalling.
|
||||
const parent = Number.isFinite(parentFrameId) ? Math.max(0, Math.round(parentFrameId)) : 0;
|
||||
return Number(java.startFrame(reason, parent));
|
||||
}
|
||||
|
||||
/**
|
||||
* Append a fact to the frame's label, e.g. which app was in the foreground
|
||||
* when the input arrived. Shows in the frame's header line in the export.
|
||||
*/
|
||||
export function annotateFrame(frameId: number, text: string): void {
|
||||
if (frameId <= 0 || !text) return;
|
||||
getJava()?.annotate(frameId, text);
|
||||
}
|
||||
|
||||
export function logFrame(frameId: number, message: string): void {
|
||||
@@ -68,6 +84,20 @@ export function span<T>(frameId: number, name: string, fn: () => T): T {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Span around an awaited step. Most multi-second gaps in the timing export are
|
||||
* awaits (an app's async input handler, a worker round trip, the Java call
|
||||
* queue), so instrument those with this rather than leaving a bare jump.
|
||||
*/
|
||||
export async function spanAsync<T>(frameId: number, name: string, fn: () => Promise<T>): Promise<T> {
|
||||
spanStart(frameId, name);
|
||||
try {
|
||||
return await fn();
|
||||
} finally {
|
||||
spanEnd(frameId, name);
|
||||
}
|
||||
}
|
||||
|
||||
/** Set the current frame while running fn, for leaf code using the *Current helpers. */
|
||||
export function runWithFrame<T>(frameId: number, fn: () => T): T {
|
||||
const previous = currentFrameId;
|
||||
|
||||
Reference in new issue
Block a user