feat: Mutability tracking#2390
feat: Mutability tracking#2390aleksanderkatan wants to merge 33 commits intorefactor/naming-cleanupfrom
Conversation
|
pkg.pr.new packages benchmark commit |
📊 Bundle Size Comparison
👀 Notable resultsStatic test results:No major changes. Dynamic test results:
📋 All resultsClick to reveal the results table (350 entries).
If you wish to run a comparison for other, slower bundlers, run the 'Tree-shake test' from the GitHub Actions menu. |
d7926fa to
5b7b6eb
Compare
…at/mutability-tracking
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.87, 1.71, 3.63, 5.57, 6.71, 8.67, 18.89, 21.99]
line [0.82, 1.60, 3.52, 4.97, 5.73, 9.44, 18.40, 18.51]
line [0.85, 1.63, 3.51, 5.07, 5.89, 9.27, 18.29, 20.82]
---
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.25, 0.45, 0.62, 0.77, 1.00, 1.04, 1.29, 1.40]
line [0.26, 0.51, 0.62, 0.76, 1.08, 1.02, 1.23, 1.36]
line [0.28, 0.49, 0.68, 0.78, 1.04, 1.09, 1.26, 1.40]
---
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.82, 1.84, 2.94, 6.09, 11.52, 22.70, 49.45, 103.04]
line [0.75, 1.68, 3.56, 5.21, 10.48, 20.80, 46.71, 94.39]
line [0.70, 1.67, 2.83, 5.14, 10.21, 20.90, 48.19, 96.24]
|
5b7b6eb to
2afcfb8
Compare
2afcfb8 to
d5d4be5
Compare
| if (targetType === snippet.dataType) { | ||
| return snippet; | ||
| } | ||
|
|
||
| return snip( | ||
| snippet.value, | ||
| targetType, | ||
| // if it was a ref, then it's still a ref | ||
| /* origin */ snippet.origin, | ||
| ); |
There was a problem hiding this comment.
| return snippet; |
There was a problem hiding this comment.
I know that I wrote this fragment of code, but now I think this check in unnecessary.
There was a problem hiding this comment.
I don't understand, what can be simplified? Action 'none' does not mean return as-is, but it also replaces the dataType with targetType
There was a problem hiding this comment.
Pull request overview
Implements function-scope mutability tracking so the WGSL generator can emit let for locals that are never mutated, while still emitting var when mutation is detected (closing #2356).
Changes:
- Add per-function placeholder + “modified variables” tracking to defer choosing
letvsvaruntil after the function body is generated. - Mark variables as modified on assignment/update operations and in select reference-taking cases (
ref,unroll, implicit pointer creation). - Update a large set of inline-snapshot tests to reflect
var→letwhere no mutation occurs.
Reviewed changes
Copilot reviewed 78 out of 78 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/typegpu/src/types.ts | Extends FunctionScopeLayer with placeholder + modified-variable tracking. |
| packages/typegpu/src/resolutionCtx.ts | Initializes new function-scope tracking structures when pushing a function scope. |
| packages/typegpu/src/tgsl/wgslGenerator.ts | Core implementation: emit placeholder decls, track mutations, and replace placeholders with let/var at function end. |
| packages/typegpu/src/tgsl/conversion.ts | Minor optimization: preserve snippet when no-op conversion targets the same type. |
| packages/typegpu/tests/vector.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/unroll.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/tgslFn.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/tgsl/wgslGenerator.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/tgsl/typeInference.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/tgsl/operatorOverloads.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/tgsl/infixOperators.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/tgsl/consoleLog.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/tgsl/codeGen.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/struct.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/std/texture/textureLoad.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/std/texture/textureGather.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/std/range.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/std/numeric/bitShift.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/std/matrix/translate.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/std/matrix/scale.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/std/matrix/rotate.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/std/boolean/not.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/slot.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/renderPipeline.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/pipeline-resolution.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/lazy.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/indent.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/computePipeline.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/array.test.ts | Snapshot updates for var → let. |
| packages/typegpu/tests/accessor.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/xor-dev-runner.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/xor-dev-centrifuge-2.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/wgsl-resolution.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/vaporrave.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/uniformity.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/two-boxes.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/tgsl-parsing-test.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/stencil.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/stable-fluid.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/square.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/smoky-triangle.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/slime-mold.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/slime-mold-3d.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/simple-shadow.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/ray-marching.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/probability.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/point-light-shadow.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/phong-reflection.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/perlin-noise.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/oklab.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/log-test.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/liquid-glass.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/lines-combinations.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/jump-flood-voronoi.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/jump-flood-distance.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/image-tuning.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/gravity.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/global-wind-map.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/game-of-life.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/function-visualizer.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/fluid-double-buffering.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/disco.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/cubemap-reflection.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/confetti.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/clouds.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/circles.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/chroma-keying.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/caustics.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/camera-thresholding.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/box-raytracing.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/boids.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/blur.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/ascii-filter.test.ts | Snapshot updates for var → let. |
| apps/typegpu-docs/tests/individual-example-tests/3d-fish.test.ts | Snapshot updates for var → let. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
We should probably test atomics as well, atomic load takes address of its argument edit: nvm, you cannot declare an atomic in a function |
d5d4be5 to
8517cba
Compare
…at/mutability-tracking
38fa09b to
6a935e5
Compare
Closes #2356
We could keep and propagate info about unmodified consts (WGSL
const) to handle cases like the below, but I think it is outside the scope of this PRI don't really like how this is implemented, but I see no better way. Maybe we could mark at implicit pointer dereference in
convertToCommonTypeto avoid some of the ref/unroll calls, but this requires better origin tracking.