Speed up cold boot: build-time gzip_static, HF Hub CDN asset routing, and parallel prefetch

#13
by Am1nadav - opened

Summary

Cold loads of the Space currently transfer ~32 MB uncompressed through the single container proxy in a multi-stage sequential waterfall. This PR speeds up cold boot across three layers while keeping everything strictly inside the Space and Hugging Face infrastructure (no external CDNs):

  1. Build-time gzip -9 pre-compression + gzip_static on; (Dockerfile)
    nginxinc/nginx-unprivileged:alpine only compresses text/html by default, sending all .wasm, .js, .glb, .stl, .json, and .xml files uncompressed. Pre-compressing assets during docker build and enabling gzip_static on; reduces transfer sizes with zero runtime CPU cost on the container:

    • mujoco.wasm: 10.12 MB -> 2.52 MB (-75%)
    • ort-wasm-simd-threaded.wasm: 13.48 MB -> 3.50 MB (-74%)
    • JS chunks (App.js, index.js, mujoco.js, ort.js): 1.55 MB -> 0.45 MB (-71%)
    • microduck.glb: 1.28 MB -> 0.80 MB (-37%)
    • Bundle total: 25.15 MB -> 6.48 MB (18.67 MB saved on cold load).
  2. Route public static assets through Hugging Face Hub CDN (app/src/game/signed.js)
    When running on the public production host (pollen-robotics-microduck-simulator.hf.space without ?__sign=), relative ./robot/, ./policies/, and ./assets/ paths resolve through the Space repo's own Hub CDN endpoint (/resolve/main/app/public/... -> cdn.hf.co, which includes CORS headers on both the 302 redirect and edge response). This offloads static asset fetches from the single container replica (~4.6x higher throughput in benchmarks). Local dev (localhost), /play/, forked Spaces, private Spaces (?__sign=), and ?cdn=0 retain same-origin resolution.

  3. Collapse the serial boot waterfall (app/src/game/game.js, app/src/game/duck.js)

    • Prefetch loadGlbGeometries() (microduck.glb) and the 7 initial ONNX policy buffers at the start of boot() in parallel with @mujoco/mujoco and onnxruntime-web/wasm instead of waiting for mujoco.wasm compilation and MjVFS setup to finish first.
    • Deduplicate concurrent loadKinematics() fetches between MenuDuck and boot().
    • Added unit tests in app/test/signed.test.js (12/12 passing).
Am1nadav changed pull request status to open
tfrere changed pull request status to merged

Sign up or log in to comment