Repository navigation
Description
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, allsizeof(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 usesComponentSize(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
- 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.
- Optionally back it with a
CheckConfig.Requirein theClusterRefaccessors. - 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
#815 #816 Cluster SoA span APIs strided by the field extent instead o…
#819 Reflection schema paths recorded marshalled field offsets while …