How we added WebGPU without affecting performance (Web Engines Hackfest '24)

The Nugget

  • Adding WebGPU to Deno without impacting startup performance involved using lazy-loaded modules, which are loaded only when first accessed, effectively managing increased snapshot sizes and startup times.

Make it stick

  • 🚀 Lazy-loading modules: Only load the modules when they are needed, reducing initial load time.
  • 🧩 Micro-optimizations add up: A 0.5-millisecond improvement in startup time was crucial despite seeming minor.
  • 💡 Single-use efficiency: Users of WebGPU, mainly for intensive applications, can tolerate a one-time startup cost.
  • 🛠️ Shared code base challenge: Managing multiple repositories so both Deno and Mozilla could iterate independently.

Key insights

Development History of WebGPU

  • The WebGPU API, a successor to WebGL, provides a lower-level, more efficient API.
  • Originating from a 2017 proposal by Apple, it was later shaped by merging proposals from Mozilla (Obsidian) and Google (NXT).
  • WGPU is Mozilla's implementation of the WebGPU specification, created by Dimitri Mal Khronos.

Internal Structure of Deno

  • Deno's layered architecture starts with Rust bindings to V8 (rusty V8) and adds utility functions (Deno Core), extensions (fetch, crypto, WebGPU), and the runtime that integrates all components.
  • WebGPU's JavaScript part is significant (205 kB), impacting startup time and complexity.

Challenges in Integration

  • WebGPU integration faced increased snapshot size and startup time. The initial implementation increased startup time from 14 milliseconds to 19 milliseconds.
  • Due to community feedback and performance concerns, the WebGPU API was temporarily removed for over six months in 2023.

Solutions for Performance Optimization

  • Addressed startup time issue by weakly linking framework dependencies like Metal, reducing startup time by approximately 0.5 milliseconds.
  • Created lazy-loaded modules: Modules are loaded only upon first access, spreading the load and minimizing startup impact.
  • Even though lazy-loading increased memory usage and compile time slightly, it was a justified trade-off.

Future Directions

  • Ongoing effort to further optimize startup time by refining weak linking of frameworks.
  • Running conformance tests for WebGPU and supporting additional APIs like offscreen canvas and canvas rendering context in Deno.
  • Collaboration potential with Servo for improved performance.

Key quotes

  • "The main issue was due to size increase and increase of the JS snapshot size, leading to increased startup time."
  • "We created a lazy loader... it loads the module once and stores the value for subsequent calls."
  • "For WebGPU, making a one-time small initialization trade-off is viable since its user base is very niche."
  • "Patch dependencies to weakly link frameworks saved 0.5 milliseconds of startup time; every bit counts."
This summary contains AI-generated information and may have important inaccuracies or omissions.