fix: Lazy buffer views in bitcast to allow for polyfill - #2769
Conversation
There was a problem hiding this comment.
Pull request overview
This PR adjusts the CPU-side bitcast implementation to instantiate typed-array buffer views lazily, preventing eager Float16Array construction so environments relying on a late-loaded polyfill don’t fail during module initialization.
Changes:
- Introduces a small cache object to hold lazily-created typed array views.
- Replaces eagerly-created
bufViewstyped arrays with getters that allocate on first access.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
pkg.pr.new packages benchmark commit |
Resolution Time Benchmark---
config:
themeVariables:
xyChart:
plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
title "Random Branching (🔴 PR | 🔵 main | 🟢 release)"
x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
y-axis "time (ms)"
line [0.72, 1.30, 2.60, 4.39, 4.83, 8.57, 15.45, 17.06]
line [0.68, 1.34, 2.56, 4.27, 4.92, 6.96, 14.06, 16.29]
line [0.66, 1.31, 2.85, 4.21, 5.54, 8.59, 14.32, 16.67]
---
config:
themeVariables:
xyChart:
plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
title "Linear Recursion (🔴 PR | 🔵 main | 🟢 release)"
x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
y-axis "time (ms)"
line [0.21, 0.45, 0.52, 0.65, 0.82, 0.88, 1.01, 1.10]
line [0.20, 0.35, 0.50, 0.55, 0.77, 0.82, 0.96, 1.08]
line [0.19, 0.41, 0.48, 0.62, 0.80, 0.85, 0.99, 1.07]
---
config:
themeVariables:
xyChart:
plotColorPalette: "#E63946, #3B82F6, #059669"
---
xychart
title "Full Tree (🔴 PR | 🔵 main | 🟢 release)"
x-axis "max depth" [1, 2, 3, 4, 5, 6, 7, 8]
y-axis "time (ms)"
line [0.67, 1.59, 2.58, 4.15, 7.96, 16.23, 36.43, 72.83]
line [0.67, 1.68, 2.60, 4.34, 7.88, 16.46, 37.14, 74.30]
line [0.65, 1.53, 3.07, 4.22, 8.09, 17.28, 37.19, 76.38]
|
Bundle size comparison (
|
| 🟢 Decreased | ➖ Unchanged | 🔴 Increased (max 0.29%) | ❔ Unknown |
|---|---|---|---|
| 0 | 296 | 26 | 0 |
import { ... } in PR vs import * as ... in PR (is the library tree-Shakeable?):
| Test | tsdown |
|---|---|
| tgpu_init.ts | 259.04 kB ( |
| tgpu_initFromDevice.ts | 258.52 kB ( |
| tgpu_resolve.ts | 164.59 kB ( |
| tgpu_resolveWithContext.ts | 164.53 kB ( |
| tgpu_bindGroupLayout.ts | 68.97 kB ( |
| tgpu_mutableAccessor.ts | 65.97 kB ( |
| tgpu_accessor.ts | 65.96 kB ( |
| tgpu_privateVar.ts | 65.31 kB ( |
| tgpu_workgroupVar.ts | 65.30 kB ( |
| tgpu_const.ts | 64.55 kB ( |
| tgpu_fn.ts | 38.14 kB ( |
| tgpu_fragmentFn.ts | 38.14 kB ( |
| tgpu_vertexFn.ts | 37.96 kB ( |
| tgpu_computeFn.ts | 37.66 kB ( |
| tgpu_vertexLayout.ts | 26.79 kB ( |
| tgpu_comptime.ts | 14.48 kB ( |
| tgpu_unroll.ts | 1.66 kB ( |
| tgpu_slot.ts | 1.54 kB ( |
| tgpu_lazy.ts | 1.19 kB ( |
If you wish to run a comparison for other, slower bundlers, run the 'Tree-shake test' from the GitHub Actions menu.
There was a problem hiding this comment.
Reviewed changes — converts eagerly-created TypedArray buffer views into lazy getters with ??= caching so that importing the module doesn't throw when Float16Array is unavailable at load time (e.g. before a polyfill initializes).
- Lazy
bufViewsinbitcast.ts— replaces 4 eagerly-allocatedTypedArrayviews (f32,u32,i32,f16) with getter-backed lazy initialization. Each view is still allocated at most once per module lifetime via??=inbufViewCache.
✅ No new issues found.
DeepSeek Pro (free via Pullfrog for OSS) (Kimi K2 not used — the program covers this model; add its provider key to run your pick) | 𝕏

No description provided.