implement @[soa] struct attribute for Structure of Arrays transformation#26738
Conversation
…nsformation Adds `@[soa]` struct attribute support to the v2 compiler for Structure of Arrays memory layout transformation. Generates a companion `_SOA` C type with separate contiguous arrays per field, plus static inline `_new`, `_get`, `_set`, `_push`, `_pop`, `_free` helper functions for zero-overhead abstraction. The checker validates that `@[soa]` structs only contain primitive numeric types and cannot be used with unions or embedded structs. https://claude.ai/code/session_015Evy1ypK5SZ8Mkiu6UX14E
8fcd38a to
bc1b3d4
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8fcd38a622
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
|
|
||
| // --- pop: remove and return last element --- | ||
| g.sb.writeln('static inline ${name} ${soa_name}_pop(${soa_name}* soa) {') | ||
| g.sb.writeln('\tsoa->len--;') |
There was a problem hiding this comment.
Guard pop against empty SoA containers
${soa_name}_pop decrements len unconditionally, so calling pop on an empty SoA sets len to -1 and then reads soa->field[-1], which is undefined behavior and can crash or corrupt memory. This helper should check len == 0 (matching array pop semantics) before decrementing.
Useful? React with 👍 / 👎.
| g.sb.writeln('\tint cap;') | ||
| for field in s.fields { | ||
| c_type := g.types_type_to_c(field.typ) | ||
| g.sb.writeln('\t${c_type}* ${field.name};') |
There was a problem hiding this comment.
Escape C keywords when emitting SoA field identifiers
The SoA generator emits raw field.name for C members, but other struct codegen paths use escape_c_keyword to handle escaped V identifiers like @type. For @[soa] structs with keyword-like field names, this produces invalid C in the companion type/functions and breaks compilation for those inputs.
Useful? React with 👍 / 👎.
Return zero-initialized struct instead of decrementing into negative len and reading out-of-bounds memory. https://claude.ai/code/session_015Evy1ypK5SZ8Mkiu6UX14E
Use escape_c_keyword(field.name) consistently for all emitted C member accesses, matching what struct.v and expr.v already do. Prevents invalid C when V field names collide with C keywords. https://claude.ai/code/session_015Evy1ypK5SZ8Mkiu6UX14E
Add @[soa] attribute support to both v1 and v2 compilers. When a struct
is annotated with @[soa], the compiler generates a companion _SOA
container struct that stores each field in a separate contiguous array,
providing better cache performance for batch operations common in game
math, ECS, particle systems, and physics simulations.
Generated companion type includes: new, get, set, push, pop, free
helper functions. Direct field array access (soa.x[i]) enables
SIMD-friendly iteration patterns.
https://claude.ai/code/session_015Evy1ypK5SZ8Mkiu6UX14E