Skip to content

feat: Make schemasEqualStructurally & schemasEqualNominally callable at comptime - #3221

Merged
iwoplaza merged 1 commit into
feat/schemas-equal-structurally-nominallyfrom
feat/schemas-equal-comptime
Oct 21, 2026
Merged

iwoplaza merged 1 commit into
feat/schemas-equal-structurally-nominallyfrom
feat/schemas-equal-comptime

Conversation

@iwoplaza

Copy link
Copy Markdown
Collaborator

Stacked on #3209.

Wraps schemasEqualStructurally and schemasEqualNominally in tgpu.comptime, so they can be called inside 'use gpu' functions to branch on schema equality during shader generation (unreached branches are pruned). They behave the same as before when called from JS.

const f = () => {
  'use gpu';
  if (d.schemasEqualStructurally(A, B)) {
    return 1;
  }
  return 0;
};
// fn f() -> i32 { return 1; }

Added a resolution test to both test files.

@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

pkg.pr.new

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

https://pkg.pr.new/software-mansion/TypeGPU/typegpu@8231b2f989ea2515794791fb8543854b63e631f5
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/react@8231b2f989ea2515794791fb8543854b63e631f5

benchmark
view benchmark

commit
view commit

@github-actions

github-actions Bot commented Oct 19, 2026 •

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.53, 1.07, 2.25, 3.52, 4.13, 6.76, 12.53, 13.74]
  line [0.49, 1.03, 2.26, 3.35, 3.54, 6.50, 13.20, 13.67]
  line [0.50, 1.04, 2.17, 3.41, 4.06, 7.01, 14.08, 13.48]
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.19, 0.31, 0.42, 0.48, 0.62, 0.69, 0.78, 0.88]
  line [0.18, 0.31, 0.38, 0.46, 0.60, 0.62, 0.72, 0.81]
  line [0.17, 0.28, 0.39, 0.41, 0.59, 0.64, 0.77, 0.85]
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.47, 1.22, 2.26, 4.04, 6.83, 14.34, 31.35, 62.54]
  line [0.50, 1.16, 2.01, 4.01, 7.14, 15.35, 30.30, 60.13]
  line [0.60, 1.43, 2.19, 3.82, 7.13, 14.04, 31.09, 63.76]
Loading

@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

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

🟢 Decreased ➖ Unchanged 🔴 Increased (max 791.59%) ❔ Unknown
0 347 6 2

import * as ... in PR vs import * as ... in target (did bundle size increase?):

Test tsdown
d_deepEqual.ts 22.79 kB ($${\color{red}+791.6\%}$$)
d_schemasEqualNominally.ts 22.79 kB
d_schemasEqualStructurally.ts 22.79 kB

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

@iwoplaza
iwoplaza force-pushed the feat/schemas-equal-comptime branch from a12f19f to f78b76b Compare October 19, 2026 11:39
@iwoplaza
iwoplaza added this pull request to stack #3222 October 19, 2026 11:39
@iwoplaza
iwoplaza force-pushed the feat/schemas-equal-comptime branch from f78b76b to 315facb Compare October 20, 2026 12:08
@iwoplaza
iwoplaza marked this pull request as ready for review October 20, 2026 12:09
@iwoplaza
iwoplaza requested review from aleksanderkatan, cieplypolar, reczkok and vende11s and a balanced review from Copilot October 20, 2026 12:09
@iwoplaza iwoplaza mentioned this pull request Oct 20, 2026

Copilot AI 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.

🟢 Approval recommended

The focused wrappers preserve comparison logic, and regression tests cover shader resolution with no identified blocking issues.

No open issues 🎉

What changed in this PR

Enables schema equality checks during shader generation while preserving JavaScript behavior.

Changes:

  • Wraps both equality helpers in named comptime functions.
  • Adds shader-resolution tests demonstrating true and false branch pruning.
File Description
packages/​typegpu/​tests/​data/​schemasEqualStructurally.test.ts Tests structural equality and true-branch pruning.
packages/​typegpu/​tests/​data/​schemasEqualNominally.test.ts Tests nominal inequality and false-branch pruning.
packages/​typegpu/​src/​data/​schemasEqual.ts Makes both helpers callable at compile time.

Review effort: Balanced

@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.

✅ No new issues found.

Reviewed changes

  • schemasEqualStructurally / schemasEqualNominally become comptime wrappers — both public exports in packages/typegpu/src/data/schemasEqual.ts are now comptime((a, b): boolean => schemasEqual(a, b, <structural>)).$name(...); the private recursive schemasEqual is unchanged.
  • Added resolution tests — each public test file gains it('can be called at comptime in a shader function'), asserting branch pruning (return 1 for the structural case, return 0 for the nominal case).

The wrapper preserves JS behavior: comptime's returned implementation forwards plain calls to the inner function, so existing callers (core/slot/accessor.ts, typegpu-react/src/core/helper-hooks.ts) are unaffected. The pattern matches existing usages in data/matrix.ts, data/array.ts, and data/disarray.ts. I verified both new test files pass, npx tsc --noEmit in packages/typegpu is clean, and a scratch probe confirmed comptime evaluation preserves object identity for schemasEqualNominally(A, A) (→ true) and recurses correctly into nested structs — cases the added tests don't cover but which work as intended.

( ◦°^°◦) 🏴☠️

Arr! I read the whole diff end to end, ran both new test files plus a scratch probe for same-identity and nested-schema comptime equality, and typechecked the package — no leaks in the hull. Ship it!

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@iwoplaza
iwoplaza force-pushed the feat/schemas-equal-comptime branch from 315facb to 0365f5b Compare October 20, 2026 12:57
@iwoplaza
iwoplaza force-pushed the feat/schemas-equal-comptime branch 2 times, most recently from 5e17ca7 to 2be759d Compare October 21, 2026 10:51
@iwoplaza
iwoplaza force-pushed the feat/schemas-equal-comptime branch from 2be759d to fed373c Compare October 21, 2026 11:37
@iwoplaza
iwoplaza force-pushed the feat/schemas-equal-comptime branch from fed373c to 8231b2f Compare October 21, 2026 11:43
@iwoplaza
iwoplaza merged commit 9ab8a56 into main Oct 21, 2026
9 checks passed
@iwoplaza
iwoplaza deleted the feat/schemas-equal-comptime branch October 21, 2026 11:53
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.

5 participants