Skip to content

fix: Lazy buffer views in bitcast to allow for polyfill - #2769

Open
iwoplaza wants to merge 1 commit into
mainfrom
fix/lazy-float16array
Open

fix: Lazy buffer views in bitcast to allow for polyfill#2769
iwoplaza wants to merge 1 commit into
mainfrom
fix/lazy-float16array

Conversation

@iwoplaza

Copy link
Copy Markdown
Collaborator

No description provided.

Copilot AI review requested due to automatic review settings July 29, 2026 08:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 bufViews typed arrays with getters that allocate on first access.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@github-actions

Copy link
Copy Markdown

pkg.pr.new

packages
Ready to be installed by your favorite package manager ⬇️

https://pkg.pr.new/software-mansion/TypeGPU/eslint-plugin-typegpu@df7067aedbba2558f15ada4c338483ed861bb1d2
https://pkg.pr.new/software-mansion/TypeGPU/tgpu-gen@df7067aedbba2558f15ada4c338483ed861bb1d2
https://pkg.pr.new/software-mansion/TypeGPU/tinyest-for-wgsl@df7067aedbba2558f15ada4c338483ed861bb1d2
https://pkg.pr.new/software-mansion/TypeGPU/typegpu@df7067aedbba2558f15ada4c338483ed861bb1d2
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/cli@df7067aedbba2558f15ada4c338483ed861bb1d2
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/color@df7067aedbba2558f15ada4c338483ed861bb1d2
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/gl@df7067aedbba2558f15ada4c338483ed861bb1d2
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/noise@df7067aedbba2558f15ada4c338483ed861bb1d2
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/radiance-cascades@df7067aedbba2558f15ada4c338483ed861bb1d2
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/react@df7067aedbba2558f15ada4c338483ed861bb1d2
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/sdf@df7067aedbba2558f15ada4c338483ed861bb1d2
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/three@df7067aedbba2558f15ada4c338483ed861bb1d2
https://pkg.pr.new/software-mansion/TypeGPU/unplugin-typegpu@df7067aedbba2558f15ada4c338483ed861bb1d2

benchmark
view benchmark

commit
view commit

@github-actions

Copy link
Copy Markdown

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]
Loading
---
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]
Loading
---
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]
Loading

@github-actions

Copy link
Copy Markdown

Bundle size comparison (import * as ... in PR vs import * as ... in target):

🟢 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 ($${\color{green}-3.2\%}$$)
tgpu_initFromDevice.ts 258.52 kB ($${\color{green}-3.4\%}$$)
tgpu_resolve.ts 164.59 kB ($${\color{green}-38.5\%}$$)
tgpu_resolveWithContext.ts 164.53 kB ($${\color{green}-38.5\%}$$)
tgpu_bindGroupLayout.ts 68.97 kB ($${\color{green}-74.2\%}$$)
tgpu_mutableAccessor.ts 65.97 kB ($${\color{green}-75.3\%}$$)
tgpu_accessor.ts 65.96 kB ($${\color{green}-75.3\%}$$)
tgpu_privateVar.ts 65.31 kB ($${\color{green}-75.6\%}$$)
tgpu_workgroupVar.ts 65.30 kB ($${\color{green}-75.6\%}$$)
tgpu_const.ts 64.55 kB ($${\color{green}-75.9\%}$$)
tgpu_fn.ts 38.14 kB ($${\color{green}-85.7\%}$$)
tgpu_fragmentFn.ts 38.14 kB ($${\color{green}-85.7\%}$$)
tgpu_vertexFn.ts 37.96 kB ($${\color{green}-85.8\%}$$)
tgpu_computeFn.ts 37.66 kB ($${\color{green}-85.9\%}$$)
tgpu_vertexLayout.ts 26.79 kB ($${\color{green}-90.0\%}$$)
tgpu_comptime.ts 14.48 kB ($${\color{green}-94.6\%}$$)
tgpu_unroll.ts 1.66 kB ($${\color{green}-99.4\%}$$)
tgpu_slot.ts 1.54 kB ($${\color{green}-99.4\%}$$)
tgpu_lazy.ts 1.19 kB ($${\color{green}-99.6\%}$$)

If you wish to run a comparison for other, slower bundlers, run the 'Tree-shake test' from the GitHub Actions menu.

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 bufViews in bitcast.ts — replaces 4 eagerly-allocated TypedArray views (f32, u32, i32, f16) with getter-backed lazy initialization. Each view is still allocated at most once per module lifetime via ??= in bufViewCache.

✅ No new issues found.

Pullfrog  | View workflow run | Using DeepSeek Pro (free via Pullfrog for OSS) (Kimi K2 not used — the program covers this model; add its provider key to run your pick) | 𝕏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants