Skip to content

implement @[soa] struct attribute for Structure of Arrays transformation#26738

Merged
medvednikov merged 3 commits into
masterfrom
claude/implement-soa-struct-b9zpH
Mar 16, 2026
Merged

implement @[soa] struct attribute for Structure of Arrays transformation#26738
medvednikov merged 3 commits into
masterfrom
claude/implement-soa-struct-b9zpH

Conversation

@medvednikov

Copy link
Copy Markdown
Member

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

…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
@medvednikov medvednikov force-pushed the claude/implement-soa-struct-b9zpH branch from 8fcd38a to bc1b3d4 Compare March 16, 2026 13:33

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 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".

Comment thread vlib/v2/gen/cleanc/soa.v

// --- pop: remove and return last element ---
g.sb.writeln('static inline ${name} ${soa_name}_pop(${soa_name}* soa) {')
g.sb.writeln('\tsoa->len--;')

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge 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 👍 / 👎.

Comment thread vlib/v2/gen/cleanc/soa.v Outdated
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};')

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

claude added 2 commits March 16, 2026 22:49
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
@medvednikov medvednikov merged commit ffe6640 into master Mar 16, 2026
118 of 155 checks passed
@medvednikov medvednikov deleted the claude/implement-soa-struct-b9zpH branch March 23, 2026 20:19
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.

2 participants