Skip to content

fix: Respect custom alignment of nested loose schemas - #3260

Open
cieplypolar wants to merge 4 commits into
mainfrom
fix/loose-schemas-attributes
Open

cieplypolar wants to merge 4 commits into
mainfrom
fix/loose-schemas-attributes

Conversation

@cieplypolar

@cieplypolar cieplypolar commented Oct 22, 2026 •

Copy link
Copy Markdown
Collaborator

Changes:

  • Fix alignmentOf for unstruct and disarray. Now, it recurses into the schema.
  • offsetsForProps no longer adds trailing padding to unstruct.
  • Fallback writers and readers don't align structs to their alignment inside unstructs.

Copilot AI balanced review requested due to automatic review settings October 22, 2026 12:44

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@github-actions

github-actions Bot commented Oct 22, 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 [1.06, 2.20, 4.75, 7.75, 8.14, 13.36, 25.58, 28.91]
  line [1.03, 2.06, 4.82, 7.71, 8.80, 12.60, 27.33, 28.52]
  line [1.09, 2.15, 4.79, 7.61, 8.88, 14.49, 29.28, 30.27]
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.32, 0.60, 0.73, 0.86, 1.22, 1.28, 1.48, 1.66]
  line [0.31, 0.49, 0.69, 0.86, 1.13, 1.21, 1.46, 1.63]
  line [0.33, 0.53, 0.75, 0.89, 1.20, 1.31, 1.53, 1.69]
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 [1.02, 2.37, 4.62, 8.87, 15.52, 28.19, 61.07, 123.61]
  line [0.83, 2.30, 4.51, 8.00, 14.25, 28.45, 60.35, 121.98]
  line [0.87, 2.24, 3.96, 8.12, 14.74, 28.24, 59.91, 124.60]
Loading

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

Caution

This PR does not compile. tsc fails with two TS2322 errors in packages/typegpu/src/data/dataIO.ts, which breaks the typegpu build and every CI job that depends on it (build-and-test on built packages, test:types/test-misc, docs, treeshake, publish). This needs to be fixed before merge.

( ◦°^°◦) 🏴☠️ Avast, me hearties! I read the whole diff, ran the core test suite (2969 tests, all passin'), typechecked the core package, and spied the build barrrking at a type error in the new array writer — she'll never make port with that readonly unknown[] keel.

Reviewed changes

  • alignmentOf recursion — unstruct/disarray now delegate to customAlignmentOf instead of peeking at the first property's raw custom alignment, so custom alignment propagates through nesting.
  • offsetsForProps trailing padding — uses sizeOf(struct) directly, dropping spurious trailing padding for unstruct (no change for WgslStruct, whose size is already rounded).
  • Fallback reader/writer rewrite — struct/array/unstruct/disarray are now placed by offsetsForProps offsets and roundUp(sizeOf(elementType), alignmentOf(schema)) stride, and alignIO was removed from the decorated/loose-decorated paths, aligning the fallback with the already-offsets-based compiled writer.
  • getPatchInstructions stride — derives disarray/array element stride from alignmentOf(arrSchema) rather than alignmentOf(elementType).
  • Tests — added nested-alignment coverage in disarray.test.ts, internal/arrayBufferIO.noEval.test.ts, internal/partialIo.test.ts, and unstruct.test.ts.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

Comment thread packages/typegpu/src/data/dataIO.ts Outdated
@cieplypolar
cieplypolar added this pull request to stack #3262 October 22, 2026 12:59
@github-actions

github-actions Bot commented Oct 22, 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/tgpu-gen@fabf065857f9fd548f374cc3db99cfa2c90c1aae
https://pkg.pr.new/software-mansion/TypeGPU/typegpu@fabf065857f9fd548f374cc3db99cfa2c90c1aae
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/cli@fabf065857f9fd548f374cc3db99cfa2c90c1aae
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/gl@fabf065857f9fd548f374cc3db99cfa2c90c1aae

benchmark
view benchmark

commit
view commit

@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

This run re-reviewed the one commit added since the prior pullfrog review (f7ddd896, type-only). The TS2322 build break is resolved and no new issues were introduced.

  • Typed the shared fallback writer params — writeStructLike and writeArrayLike now declare InferInput<wgsl.WgslStruct | Unstruct> / InferInput<wgsl.WgslArray | Disarray> instead of Record<string, unknown> / readonly unknown[], so the satisfies CompleteDataWriters assignment accepts the typed-array members of InferInput. Verified locally: pnpm --filter typegpu test:types and tsc -p tsconfig.build.json --noEmit both exit 0.

Prior pullfrog review feedback (the writeArrayLike parameter type) is addressed; the corresponding thread is resolved.

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

@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

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

🟢 Decreased (max -0.23%) ➖ Unchanged 🔴 Increased (max 0.51%) ❔ Unknown
34 320 1 0

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

Test tsdown
d_alignmentOf.ts 30.01 kB ($${\color{red}+0.5\%}$$)

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

@cieplypolar
cieplypolar force-pushed the fix/loose-schemas-attributes branch 2 times, most recently from 2fc983f to 346e20f Compare October 26, 2026 11:32
});

it('reads wgsl structs packed into an unstruct', () => {
const s = d.unstruct({ a: d.u32, s: d.struct({ v: d.vec3u, w: d.u32 }) });

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Could you convince me as to why structs should pack tightly instead of aligning based on the alignment of their natural alignment? Is it because vectors don't either?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

First, to clarify, structs only relax their starting offsets. Any internal or trailing padding is preserved.
Second, the docs state: Loose schemas are not subject to alignment restrictions. To me, it feels intuitive that unstruct drops starting offset restriction but preserves struct's inner layout.
This is also consistent with vectors and arrays.

Btw, thanks for this comment. While reading it, I realized that I haven't updated vertex layout calculations.

@iwoplaza iwoplaza left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The implementation looks very solid! I am mostly wondering about the behavior, but I am easily persuaded 😆

@cieplypolar
cieplypolar force-pushed the fix/loose-schemas-attributes branch from 0a01cb7 to fabf065 Compare October 27, 2026 18:08

This branch has not been deployed

No deployments
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