Skip to content

Cluster SoA span APIs stride by sizeof(T) instead of the layout's component size — silent cross-column corruption #816

Closed

Description

@nockawa

Summary

ClusterRef's span/ref accessors index a cluster SoA column with sizeof(T), while the column is laid out with ArchetypeClusterInfo.ComponentSize(slot) (= DBComponentDefinition.ComponentStorageSize). Those two disagree whenever a component struct carries trailing padding the schema cannot see — e.g. [StructLayout(Sequential, Size = 8)] on a struct with a single 4-byte [Field].

When they disagree, every cluster-span access is mis-strided, silently — no exception, no Release assert:

  • Reads alias the wrong slot, then run off the end of the column into the next component's column.
  • Writes stamp the padding bytes over the neighbouring slot, and past the halfway point write into the adjacent column.

Where

  • src/Typhon.Engine/Ecs/public/ClusterRef.cs:115,148,160,168,246 — GetSpan / GetReadOnlySpan / Get / GetReadOnly / WriteSpatial, all sizeof(T).
  • src/Typhon.Engine/Schema/public/DBComponentDefinition.cs:316 — ComponentStorageSize = lastField.Offset + lastField.Size; trailing padding is invisible to it.
  • src/Typhon.Engine/Ecs/public/EntityRef.cs:173,193,252,257 — the per-entity path uses ComponentSize(slot) and is correct.

That divergence is the diagnostic tell: the flat/EntityMap view reads right while the cluster view reads wrong, over the same bytes.

The invariant is already known — EntityAccessor.ECS.cs:583 asserts sizeof(T) == size — but only in Debug, and only on the staging path.

Evidence

Found while investigating #815 (SpaceBattle demo). Ship: ClusterSize 21, Vitals column 4 B/slot, sizeof(Vitals) 8.

with the padding padding removed
spawn + WriteTickFence, cluster reads returning 0 2375/5000 (47.5%) 0/5000
8-worker 30k-tick run, mis-strided reads peak 100% 0% (0 of 595k at tick 125, 0 at all 41 samples)
damage settlement stall tick 4125 none

FinalizeSpawns is correct throughout — it writes with the layout stride. Nothing concurrent is involved; it reproduces single-threaded.

Why no test caught it

Vitals in the SpaceBattle demo is the only ECS component in the repo with an explicit StructLayout(Size = …). Every engine test component has sizeof(T) == ComponentStorageSize, so the divergent stride never had a chance to show.

Suggested fix

  1. Reject the mismatch at archetype registration. A component whose CLR size exceeds its storage size can never be safely spanned; a loud throw naming the component beats a Release-silent stride.
  2. Optionally back it with a CheckConfig.Require in the ClusterRef accessors.
  3. A compile-time diagnostic in src/Typhon.Analyzers/ (alongside TYPHON009) would catch it before it ever runs.

Area note

Filed as Schema because the invariant (sizeof(T) vs ComponentStorageSize) and the natural place to enforce it are schema-side; the symptom surfaces in the ECS cluster accessors.

Activity

  1. self-assigned this
    on Aug 15, 2026
  2. added theissue type on Aug 15, 2026
  3. updated Priority P1-High, Product Engine, Estimate S and Area Schema on Aug 15, 2026
  4. moved this to Todo in Typhonon Aug 15, 2026
  5. added 2 commits that reference this issue on Aug 15, 2026
  6. moved this from Todo to Done in Typhonon Sep 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Fields

Priority

P1-High

Estimate

S

Area

Schema

Start date

None yet

Target date

None yet

Bug status

None yet

Product

Engine

Projects

  • Status
    Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions