perf(viewer): request the renderer's WebGPU device at the file pick

Draft
LO
18d ago
+741-104

Status: draft. Not timed until cold loads are neutral (see "Cold concern").

What

On a fresh page the viewport mounts during a load's interface reset, and its renderer only asked for a WebGPU device then. This PR starts the request at the file pick:

  • prerequestRendererDevice() (packages/renderer/src/device-request.ts) requests the adapter. It returns a promise that resolves once requestDevice is in flight, or after 50 ms at most.
  • loadFile awaits that promise once, after it owns its load session and before the reset. Without that turn the adapter's promise could only resolve after the reset's long task, so requestDevice went out late. Measured below; the first version of this PR had exactly that problem.
  • The next WebGPUDevice.init() adopts the device.
  • A device that is lost or fails before then is replaced by a fresh request, and a device no renderer claims within 15 s is destroyed. Nothing is requested at idle.

Measured (untimed, Pop real GPU, ABBA x5, every open quiet-gated at load < 6 and CPU pressure < 5; base 70ecd5a09c vs this PR b99da9edf0)

Medians in ms from the file input's change event (base / this PR):

cellrequestDevice sentreset endsdevice readyfirst frameinteractive
Holter snapshot reopen116 / 15107 / 110266 / 148506 / 4321834 / 1723
Holter first reopen114 / 15105 / 111268 / 176515 / 4241838 / 1733
advanced_model snapshot reopen121 / 13111 / 116230 / 128409 / 310854 / 764
advanced_model first reopen117 / 12107 / 110273 / 157445 / 327893 / 785
BWK snapshot reopen115 / 14106 / 113402 / 399612 / 6081590 / 1572
BWK first reopen115 / 13107 / 113401 / 401612 / 6111582 / 1584
Holter cold123 / 14114 / 117292 / 1271040 / 10424677 / 4617
BWK cold114 / 13106 / 118390 / 1271360 / 13723952 / 3910
advanced_model cold119 / 16111 / 116234 / 124562 / 6171809 / 1841
FZK cold119 / 16111 / 117233 / 126336 / 364574 / 617
  • The device is ready 3-15 ms after the reset ends, 100-260 ms earlier than base. The yield costs about 3-12 ms before the reset starts.
  • Holter and advanced_model reopens gain about 75-120 ms to first frame and about 90-110 ms to interactive.
  • BWK reopens do not move: the device is ready at about 400 ms on both sides. That is being investigated.

Cold concern (blocks timing)

advanced_model cold is +55 ms to first frame. FZK cold is +28 ms to first frame and +43 ms to interactive. Base and PR ranges do not overlap. Next step: sourcemapped traces of the cold path, then hold renderer setup back to base's point on a cold path (or gate the request by cache size tier if the request itself costs). The itemisation is repeated afterwards.

Tests

  • packages/renderer/src/device-request.test.ts:

    • init adopts the early device;
    • a lost or failed early device is replaced, and a lost one that throws on destroy still lets init proceed;
    • an unclaimed device is destroyed after the hold, and a failure frees the slot;
    • the promise resolves only once requestDevice is in flight;
    • one early request at a time, none without WebGPU.

    The renderer suite passes (24/24 in the device tests).

  • apps/viewer/src/hooks/useIfcLoader.earlyDevice.test.tsx: the real hook issues requestAdapter, then requestDevice, then the reset, in that exact order (fails without the await); a renderer initialised afterwards adopts that device; nothing early when a renderer is already mounted.

šŸ¤– Generated with Claude Code