From 199aa8ef4b2a112591eeb30c4a1baadfcc1e53e2 Mon Sep 17 00:00:00 2001 From: eust-w Date: Thu, 25 Jun 2026 13:36:51 +0800 Subject: [PATCH] Optimize compose restore path --- docs/product-readiness.md | 49 ++++++++++++++----------- scripts/verify-real-backend-editing.mjs | 11 ++++++ server/headless_motrix_bridge.py | 41 ++++++++++++++++++--- src/main.ts | 37 ++++++++++++++----- src/preview/UniversalPreviewer.ts | 10 +++-- 5 files changed, 108 insertions(+), 40 deletions(-) diff --git a/docs/product-readiness.md b/docs/product-readiness.md index 2e859b2..962b91d 100644 --- a/docs/product-readiness.md +++ b/docs/product-readiness.md @@ -651,36 +651,39 @@ Bundle evidence: Control evidence: -- Cold compose endpoint: `451ms`, `4/4` composed object bodies synced. -- Repeated compose cache: `222ms`, `cached=true`, same compose key. +- Cold compose endpoint: `271ms`, `4/4` composed object bodies synced, with + `resetApplied=true` when requested by the Scene Builder restore path. +- Repeated compose cache: `55ms`, `cached=true`, same compose key, and + `resetApplied=true`, so the frontend no longer needs a second `/control + reset` request after a cached compose restore. - JSON transport compression: `/compose` and `/manifest` wire payloads are about `11 KiB` after gzip instead of about `257 KiB` uncompressed. - Active state endpoint: full `/state` returned `602` bodies; active - `/state?bodies=active` returned `34` bodies in `62ms`, filtered `568` fixed + `/state?bodies=active` returned `34` bodies in `49ms`, filtered `568` fixed bodies, and retained `4/4` composed dynamic bodies. -- Scene Builder UI compose: representative run `3544ms` end to end with real +- Scene Builder UI compose: representative run `3601ms` end to end with real backend diagnostics. -- Share-link restore: representative full browser restore `20138ms`. -- Config import restore: representative full browser restore `18889ms`. -- Composed object body edit: `47ms`. -- Fixed inventory edit rejection: `59ms`. -- Dynamic composed object drop: `0.48012m`. +- Share-link restore: representative full browser restore `15053ms`. +- Config import restore: representative full browser restore `13213ms`. +- Composed object body edit: `56ms`. +- Fixed inventory edit rejection: `56ms`. +- Dynamic composed object drop: `0.47846m`. - Static shelf anchor stability: `max drift 0.00000m` after a 2500 ms resume window. -- Resume backend state: `55ms paused=false` -- Resume visible UI state: `46ms` -- Pause backend state: `258ms paused=true` -- Pause visible UI state: `44ms` -- Desktop acceptance wait: `1243ms` total, product telemetry `0ms` -- Mobile acceptance wait: `277ms` total, product telemetry `0ms` +- Resume backend state: `63ms paused=false` +- Resume visible UI state: `53ms` +- Pause backend state: `237ms paused=true` +- Pause visible UI state: `51ms` +- Desktop acceptance wait: product telemetry `1ms` +- Mobile acceptance wait: product telemetry `0ms` - Runtime diagnostics panel: `state=http://127.0.0.1:18090/state`, `control=http://127.0.0.1:18090/control`, - `latency=响应 44 ms / 确认 44 ms / 可见 44 ms`, - `acceptanceLatency=0 ms / 11-0-0`, + `latency=响应 50 ms / 确认 50 ms / 可见 51 ms`, + `acceptanceLatency=1 ms / 11-0-0`, `binding=635/636; 4/568`, `builderPhysics=已添加物体 4: 后端动态 4 / 仅视觉 0`, - `renderPerformance=large-scene; target 45 fps; actual 0.7 fps; dpr 1.00; buffer preserved false; readback renders 1; shadows off; skipped 1096; hover 32ms; frozen 135 prefrozen 59 spinSkip 627; instanced 568/16/16 matrices 568 cull 16 static 16 elidedLocal 568 virtualRoots 568; manual roots 68 updates 67; primitive geometries 7; mesh cache 51 sources 24/51 hit/miss retained 51g/17m; bounds cache 636 fast 627 scratch 695 source 51 hoverProxy 2 cached 2/fallback 0 camera traversals 0; scene batches 4/84 suppressed 826 yields 26; state payload 34/602 reduced 1 incremental 2; ui cache physics 25/3 builder 28/1 text 103/435 selector 492/46 diagnostics 20/3; pick targets 88/636; state apply 0/skip 67; unchanged frames 1; released 0g/0m/0t`. + `renderPerformance=large-scene; target 45 fps; actual 0.8 fps; stats refresh 4000ms 7/0; dpr 1.00; buffer preserved false; readback renders 1; shadows off; skipped 891; hover 32ms; frozen 135 prefrozen 59 spinSkip 627; instanced 568/16/16 matrices 568 cull 16 static 16 elidedLocal 568 virtualRoots 568; manual roots 68 updates 67; primitive geometries 7; mesh cache 51 sources 24/51 hit/miss retained 51g/17m; bounds cache 636 fast 627 scratch 695 source 51 hoverProxy 2 cached 2/fallback 0 camera traversals 0; scene batches 4/84 suppressed 1806 yields 26; state payload 34/602 reduced 1 incremental 2; poll 50/2000/2000ms; pick targets 88/636; state apply 0/skip 67; unchanged frames 1; released 0g/0m/0t`. - Evidence screenshot artifacts: `builder page; share page; import page; visual-only boundary canvas; desktop canvas; debug PNG; mobile canvas` - Evidence export artifacts: @@ -777,16 +780,18 @@ Current performance boundaries: first load, Scene Builder compose, share restore, and config import restore still include browser asset creation and scene graph setup time. - The real backend control path is not the observed bottleneck in the current - gate: resume visible UI confirmation is `46ms`, pause visible UI confirmation - is `44ms`, and the backend physics step sample during pause verification - reports `0.51ms` mean and `0.81ms` p95 step time. + gate: resume visible UI confirmation is `53ms`, pause visible UI confirmation + is `51ms`, and repeated compose cache with reset is `55ms`. - The remaining high-impact optimization area is asset-level rendering and static-scene packaging: the current virtual-root instancing path reduces per-item scene construction (`virtualRoots=568`, `manualRoots=68`), but - share/config restore still spends `18-20s` in full browser page load, compose + share/config restore still spends `13-15s` in full browser page load, compose restore, asset setup, and screenshots. The next tier is material reduction, LODs, static asset manifests, and eventually moving repeated fixed inventory into a backend static-state channel. +- Runtime stats no longer perform repeated WebGL pixel readback. Pixel proof is + sampled once after manifest load and screenshot/debug export, while ordinary + stats refresh reads the cached value. ## Handoff Criteria diff --git a/scripts/verify-real-backend-editing.mjs b/scripts/verify-real-backend-editing.mjs index ba111c6..0f2e2ef 100644 --- a/scripts/verify-real-backend-editing.mjs +++ b/scripts/verify-real-backend-editing.mjs @@ -205,6 +205,7 @@ try { robot: "g1", robotPose: expectedPhysicalRobotPose, paused: true, + reset: true, objects: composeObjects, }; @@ -221,6 +222,11 @@ try { `${composeResult.elapsedMs}ms objectCount=${composeResult.json?.objectCount}`, )); checks.push(check("physical-compose-latency", composeResult.elapsedMs <= maxComposeLatencyMs, `${composeResult.elapsedMs}ms <= ${maxComposeLatencyMs}ms`, "warn")); + checks.push(check( + "physical-compose-reset-applied", + composeResult.json?.resetApplied === true, + `resetApplied=${composeResult.json?.resetApplied}`, + )); checks.push(check("physical-compose-manifest", inlineComposeObjects.length > 0, `${inlineComposeObjects.length} inline objects`)); composeCompression = compressedJsonDetail(composeResult); checks.push(check( @@ -249,6 +255,11 @@ try { `${composeCacheResult.elapsedMs}ms <= ${maxControlLatencyMs}ms`, "warn", )); + checks.push(check( + "physical-compose-cache-reset-applied", + composeCacheResult.json?.resetApplied === true, + `resetApplied=${composeCacheResult.json?.resetApplied}`, + )); composeCacheCompression = compressedJsonDetail(composeCacheResult); checks.push(check( "physical-compose-cache-json-gzip", diff --git a/server/headless_motrix_bridge.py b/server/headless_motrix_bridge.py index 9394db2..41263f7 100644 --- a/server/headless_motrix_bridge.py +++ b/server/headless_motrix_bridge.py @@ -1335,7 +1335,14 @@ def main() -> int: active_compose_key: str | None = None - def scene_response(active_runtime: MotrixRuntime, active_manifest: dict[str, Any], *, cached: bool = False, compose_key: str | None = None) -> dict[str, Any]: + def scene_response( + active_runtime: MotrixRuntime, + active_manifest: dict[str, Any], + *, + cached: bool = False, + compose_key: str | None = None, + reset_applied: bool = False, + ) -> dict[str, Any]: return { "ok": True, "scene": active_runtime.scene.as_posix(), @@ -1348,10 +1355,17 @@ def main() -> int: "policy": active_runtime.policy_name, "cached": cached, "composeKey": compose_key, + "resetApplied": reset_applied, "manifest": active_manifest, } - def replace_scene(scene: Path, *, paused: bool = True, compose_key: str | None = None) -> dict[str, Any]: + def replace_scene( + scene: Path, + *, + paused: bool = True, + compose_key: str | None = None, + reset_applied: bool = False, + ) -> dict[str, Any]: nonlocal runtime, manifest, asset_files, thread, active_compose_key next_runtime, next_manifest, next_asset_files, next_thread = create_bundle(scene, paused=paused) with state_lock: @@ -1363,7 +1377,7 @@ def main() -> int: active_compose_key = compose_key previous_runtime.running = False next_thread.start() - return scene_response(next_runtime, next_manifest, cached=False, compose_key=compose_key) + return scene_response(next_runtime, next_manifest, cached=False, compose_key=compose_key, reset_applied=reset_applied) class Handler(BaseHTTPRequestHandler): def log_message(self, fmt: str, *values: Any) -> None: @@ -1445,16 +1459,31 @@ def main() -> int: compose_request = normalize_compose_request(payload) compose_key = compose_request_key(compose_request) paused = bool(payload.get("paused", True)) + reset_requested = bool(payload.get("reset", False)) cached_response: dict[str, Any] | None = None with state_lock: if active_compose_key == compose_key: - runtime.paused = paused - cached_response = scene_response(runtime, manifest, cached=True, compose_key=compose_key) + with runtime.lock: + if reset_requested: + runtime.reset_locked() + runtime.paused = paused + cached_response = scene_response( + runtime, + manifest, + cached=True, + compose_key=compose_key, + reset_applied=reset_requested, + ) if cached_response is not None: self.send_json(200, cached_response) return scene_path = compose_physical_scene(payload, args.compose_dir, compose_request) - self.send_json(200, replace_scene(scene_path, paused=paused, compose_key=compose_key)) + self.send_json(200, replace_scene( + scene_path, + paused=paused, + compose_key=compose_key, + reset_applied=reset_requested, + )) return self.send_json(404, {"ok": False, "error": "not found"}) except Exception as error: # noqa: BLE001 diff --git a/src/main.ts b/src/main.ts index e776a94..8482329 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1666,7 +1666,7 @@ async function loadResolvedManifest(manifest: SceneManifest): Promise { addActivity("activity.manifestLoaded", { count: manifest.objects.length }, "ok"); pendingCameraFit = true; await stateConnect; - renderStats({ forceDiagnostics: true }); + renderStats({ forceDiagnostics: true, samplePixels: true }); scheduleStatsRefresh(); } @@ -1712,8 +1712,8 @@ function resolveControlUrl(manifest: SceneManifest): string | undefined { return url.href; } -function renderStats(options: { forceDiagnostics?: boolean } = {}): void { - const stats = previewer.getStats(); +function renderStats(options: { forceDiagnostics?: boolean; samplePixels?: boolean } = {}): void { + const stats = previewer.getStats({ samplePixels: options.samplePixels ?? false }); const physicsProfile = objectPhysicsProfile(); const builderPhysics = builderPhysicsProfile(); const health = healthSummary(stats); @@ -3641,9 +3641,10 @@ async function buildAndLoadBuilderScene(savedStatusKey: MessageKey = "status.wor const hasCustomUploadedProps = builderProps.some((item) => isCustomUploadedPropId(item.presetId)); saveBuilderDraft(savedStatusKey); setText("#builder-status", t("status.builder.composing")); - const manifest = useBackendCompose + const composedScene = useBackendCompose ? await composePhysicalScene(scenePreset, robotPreset) - : await createBuilderManifest(scenePreset, robotPreset, builderRobotPose); + : undefined; + const manifest = composedScene?.manifest ?? await createBuilderManifest(scenePreset, robotPreset, builderRobotPose); if (!useBackendCompose) { manifestInput.value = `builder://${scenePreset.id}+${robotPreset.id}`; } @@ -3687,7 +3688,8 @@ async function buildAndLoadBuilderScene(savedStatusKey: MessageKey = "status.wor } }, }); - if (controlUrl && (robotPreset.id !== "none" || scenePreset.physical)) { + const composeAlreadyInitializedRuntime = composedScene?.resetApplied === true || composedScene?.cached === false; + if (controlUrl && (robotPreset.id !== "none" || scenePreset.physical) && !composeAlreadyInitializedRuntime) { await sendControl("reset"); } const modeKey: MessageKey = useBackendCompose @@ -3708,9 +3710,17 @@ interface ComposeSceneResponse { error?: string; manifest?: SceneManifest; manifestUrl?: string; + cached?: boolean; + resetApplied?: boolean; } -async function composePhysicalScene(scenePreset: ScenePreset, robotPreset: RobotPreset): Promise { +interface ComposeSceneResult { + manifest: SceneManifest; + cached?: boolean; + resetApplied?: boolean; +} + +async function composePhysicalScene(scenePreset: ScenePreset, robotPreset: RobotPreset): Promise { if (!scenePreset.composeUrl) { throw new Error("physical compose endpoint is unavailable"); } @@ -3721,6 +3731,7 @@ async function composePhysicalScene(scenePreset: ScenePreset, robotPreset: Robot scene: scenePreset.id, robot: robotPreset.id, robotPose: builderRobotPose, + reset: true, stockPhysics: "fixed", objects: builderProps.map((item) => ({ id: item.id, @@ -3737,9 +3748,17 @@ async function composePhysicalScene(scenePreset: ScenePreset, robotPreset: Robot const manifestUrl = new URL(payload.manifestUrl ?? "/manifest", response.url).href; manifestInput.value = manifestUrl; if (payload.manifest) { - return resolveManifestUrls(payload.manifest, response.url); + return { + manifest: resolveManifestUrls(payload.manifest, response.url), + cached: payload.cached, + resetApplied: payload.resetApplied, + }; } - return loadPresetManifest(manifestUrl); + return { + manifest: await loadPresetManifest(manifestUrl), + cached: payload.cached, + resetApplied: payload.resetApplied, + }; } async function createBuilderManifest(scenePreset: ScenePreset, robotPreset: RobotPreset, robotPose: Vec3 = [0, 0, 0]): Promise { diff --git a/src/preview/UniversalPreviewer.ts b/src/preview/UniversalPreviewer.ts index ea97896..79d031d 100644 --- a/src/preview/UniversalPreviewer.ts +++ b/src/preview/UniversalPreviewer.ts @@ -38,6 +38,10 @@ interface PreviewerOptions { hoverLabel?: HTMLElement; } +interface RendererStatsOptions { + samplePixels?: boolean; +} + interface BoundObject { manifest: PreviewObject; root: THREE.Object3D; @@ -537,7 +541,7 @@ export class UniversalPreviewer { this.refreshEditablePickTargets(); } - getStats(): RendererStats { + getStats(options: RendererStatsOptions = {}): RendererStats { return { manifestName: this.manifest?.name ?? "none", objectCount: this.manifest?.objects.length ?? 0, @@ -607,7 +611,7 @@ export class UniversalPreviewer { lastBodyCount: this.lastFrame?.bodies ? this.mergedBodyStateCount : 0, paused: this.lastFrame?.paused, running: this.lastFrame?.running, - nonBackgroundPixelSamples: this.cachedCanvasPixelSamples(), + nonBackgroundPixelSamples: options.samplePixels ? this.cachedCanvasPixelSamples() : this.lastPixelSamples, }; } @@ -675,7 +679,7 @@ export class UniversalPreviewer { return this.lastPixelSamples; } const now = performance.now(); - if (now - this.lastPixelSampleAt < 2000) { + if (this.lastPixelSampleAt > 0 && now - this.lastPixelSampleAt < 2000) { return this.lastPixelSamples; } this.lastPixelSamples = this.sampleCanvasPixels();