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
@@ -111,6 +111,8 @@ export class FaceclawCommunicatorBridge {
|
||||
private readonly communicator: any;
|
||||
private readonly listenerProxy: any;
|
||||
private javaCallQueue: Promise<void> = Promise.resolve();
|
||||
/** Calls waiting on javaCallQueue; 0 means enqueueJavaCall's fast path is safe. */
|
||||
private queuedJavaCalls = 0;
|
||||
private readonly frameMetricWaiters = new Set<(metrics: FrameMetrics) => void>();
|
||||
// Recent frame-finished outcomes from the Java side, so waitForFrameFinished
|
||||
// does not race against finishes that land before the wait starts.
|
||||
@@ -227,10 +229,30 @@ export class FaceclawCommunicatorBridge {
|
||||
}, 0);
|
||||
}
|
||||
|
||||
private enqueueJavaCall<T>(operation: () => T): Promise<T> {
|
||||
/**
|
||||
* Run a Java call, ordered against every other call made through here.
|
||||
*
|
||||
* inlineWhenIdle runs the call on the spot when nothing is queued ahead of
|
||||
* it. Ordering still holds — the fast path only fires when no earlier call
|
||||
* is waiting — and it skips a task-queue hop that measured ~18ms on device
|
||||
* (the gap between "span submit start" and the Java side's own log line in
|
||||
* the frame timings), which is pure latency on a call the caller is already
|
||||
* awaiting. Off by default: a call that might block for a while should keep
|
||||
* yielding to the main looper first.
|
||||
*/
|
||||
private enqueueJavaCall<T>(operation: () => T, inlineWhenIdle = false): Promise<T> {
|
||||
if (inlineWhenIdle && this.queuedJavaCalls === 0) {
|
||||
try {
|
||||
return Promise.resolve(operation());
|
||||
} catch (error) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
}
|
||||
this.queuedJavaCalls++;
|
||||
const run = () =>
|
||||
new Promise<T>((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
this.queuedJavaCalls--;
|
||||
try {
|
||||
resolve(operation());
|
||||
} catch (error) {
|
||||
@@ -526,6 +548,8 @@ export class FaceclawCommunicatorBridge {
|
||||
// ArrayBuffer, which NativeScript marshals to a ByteBuffer without the
|
||||
// ~150ms per-element copy a byte[] parameter would need.
|
||||
const snapshot = new Uint8Array(pixels8bpp);
|
||||
// inlineWhenIdle: this is the frame path, the Java side of it measures
|
||||
// ~5ms (composite + pack), and the caller awaits it either way.
|
||||
await this.enqueueJavaCall(() => {
|
||||
this.communicator.submitSurfaceFrame(
|
||||
snapshot.buffer,
|
||||
@@ -539,7 +563,7 @@ export class FaceclawCommunicatorBridge {
|
||||
Math.round(nonNegativeNumber(frameId)),
|
||||
glyphs,
|
||||
);
|
||||
});
|
||||
}, true);
|
||||
}
|
||||
|
||||
async disconnect(): Promise<void> {
|
||||
|
||||
+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