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.12, 2.18, 4.68, 6.87, 9.06, 13.71, 28.89, 25.47]
  line [1.12, 2.23, 4.63, 7.79, 9.56, 14.60, 28.03, 29.38]
  line [1.03, 2.12, 4.66, 7.27, 8.71, 13.99, 26.77, 25.79]
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.29, 0.54, 0.76, 0.93, 1.27, 1.32, 1.64, 1.73]
  line [0.37, 0.63, 0.82, 0.97, 1.32, 1.33, 1.66, 1.79]
  line [0.33, 0.60, 0.79, 0.92, 1.20, 1.31, 1.54, 1.67]
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.03, 2.57, 4.24, 7.81, 13.98, 29.96, 61.34, 123.90]
  line [0.96, 2.41, 4.39, 7.77, 14.60, 28.72, 63.10, 129.28]
  line [1.01, 2.31, 4.91, 7.84, 16.09, 28.77, 62.63, 125.85]
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@0a01cb70a8f5a7374f74ac467d0fea45bc2e1e41
https://pkg.pr.new/software-mansion/TypeGPU/typegpu@0a01cb70a8f5a7374f74ac467d0fea45bc2e1e41
https://pkg.pr.new/software-mansion/TypeGPU/@typegpu/gl@0a01cb70a8f5a7374f74ac467d0fea45bc2e1e41

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

Copy link
Copy Markdown

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

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

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

Test tsdown
d_alignmentOf.ts 29.10 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?

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

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.

4 participants