|
| 1 | +// Copyright (c) 2026 Alexander Medvednikov. All rights reserved. |
| 2 | +// Use of this source code is governed by an MIT license |
| 3 | +// that can be found in the LICENSE file. |
| 4 | + |
| 5 | +// Structure of Arrays (SoA) code generation. |
| 6 | +// |
| 7 | +// When a struct is annotated with @[soa], the compiler generates a companion |
| 8 | +// SoA container struct that stores each field in a separate contiguous array. |
| 9 | +// This layout provides significantly better cache performance for batch |
| 10 | +// operations that touch only a subset of fields (common in game math, ECS, |
| 11 | +// particle systems, physics simulations, etc.). |
| 12 | +// |
| 13 | +// Example: |
| 14 | +// @[soa] |
| 15 | +// struct Particle { |
| 16 | +// x f32 |
| 17 | +// y f32 |
| 18 | +// vx f32 |
| 19 | +// vy f32 |
| 20 | +// } |
| 21 | +// |
| 22 | +// Generates a companion type `Particle_SOA` with: |
| 23 | +// struct Particle_SOA { |
| 24 | +// int len; |
| 25 | +// int cap; |
| 26 | +// f32* x; |
| 27 | +// f32* y; |
| 28 | +// f32* vx; |
| 29 | +// f32* vy; |
| 30 | +// }; |
| 31 | +// |
| 32 | +// And helper functions: |
| 33 | +// Particle_SOA Particle_SOA_new(int len, int cap); |
| 34 | +// void Particle_SOA_push(Particle_SOA* soa, Particle val); |
| 35 | +// Particle Particle_SOA_get(Particle_SOA soa, int i); |
| 36 | +// void Particle_SOA_set(Particle_SOA* soa, int i, Particle val); |
| 37 | +// void Particle_SOA_free(Particle_SOA* soa); |
| 38 | +module cleanc |
| 39 | + |
| 40 | +import v2.types |
| 41 | + |
| 42 | +// gen_soa_companion generates the SoA container struct and helper functions |
| 43 | +// for a struct annotated with @[soa]. |
| 44 | +fn (mut g Gen) gen_soa_companion(name string, s types.Struct) { |
| 45 | + soa_name := '${name}_SOA' |
| 46 | + |
| 47 | + // --- SoA container struct --- |
| 48 | + g.sb.writeln('// SoA (Structure of Arrays) companion for ${name}') |
| 49 | + g.sb.writeln('typedef struct {') |
| 50 | + g.sb.writeln('\tint len;') |
| 51 | + g.sb.writeln('\tint cap;') |
| 52 | + for field in s.fields { |
| 53 | + c_type := g.types_type_to_c(field.typ) |
| 54 | + fname := escape_c_keyword(field.name) |
| 55 | + g.sb.writeln('\t${c_type}* ${fname};') |
| 56 | + } |
| 57 | + g.sb.writeln('} ${soa_name};') |
| 58 | + g.sb.writeln('') |
| 59 | + |
| 60 | + // --- new: allocate SoA container --- |
| 61 | + g.sb.writeln('static inline ${soa_name} ${soa_name}_new(int len, int cap) {') |
| 62 | + g.sb.writeln('\tif (cap < len) cap = len;') |
| 63 | + g.sb.writeln('\t${soa_name} soa;') |
| 64 | + g.sb.writeln('\tsoa.len = len;') |
| 65 | + g.sb.writeln('\tsoa.cap = cap;') |
| 66 | + for field in s.fields { |
| 67 | + c_type := g.types_type_to_c(field.typ) |
| 68 | + fname := escape_c_keyword(field.name) |
| 69 | + g.sb.writeln('\tsoa.${fname} = (${c_type}*)calloc(cap, sizeof(${c_type}));') |
| 70 | + } |
| 71 | + g.sb.writeln('\treturn soa;') |
| 72 | + g.sb.writeln('}') |
| 73 | + g.sb.writeln('') |
| 74 | + |
| 75 | + // --- get: retrieve element at index as original struct --- |
| 76 | + g.sb.writeln('static inline ${name} ${soa_name}_get(${soa_name} soa, int i) {') |
| 77 | + g.sb.writeln('\treturn (${name}){') |
| 78 | + for i, field in s.fields { |
| 79 | + comma := if i < s.fields.len - 1 { ',' } else { '' } |
| 80 | + fname := escape_c_keyword(field.name) |
| 81 | + g.sb.writeln('\t\t.${fname} = soa.${fname}[i]${comma}') |
| 82 | + } |
| 83 | + g.sb.writeln('\t};') |
| 84 | + g.sb.writeln('}') |
| 85 | + g.sb.writeln('') |
| 86 | + |
| 87 | + // --- set: set element at index from original struct --- |
| 88 | + g.sb.writeln('static inline void ${soa_name}_set(${soa_name}* soa, int i, ${name} val) {') |
| 89 | + for field in s.fields { |
| 90 | + fname := escape_c_keyword(field.name) |
| 91 | + g.sb.writeln('\tsoa->${fname}[i] = val.${fname};') |
| 92 | + } |
| 93 | + g.sb.writeln('}') |
| 94 | + g.sb.writeln('') |
| 95 | + |
| 96 | + // --- push: append element, growing capacity if needed --- |
| 97 | + g.sb.writeln('static inline void ${soa_name}_push(${soa_name}* soa, ${name} val) {') |
| 98 | + g.sb.writeln('\tif (soa->len >= soa->cap) {') |
| 99 | + g.sb.writeln('\t\tint new_cap = soa->cap < 8 ? 8 : soa->cap * 2;') |
| 100 | + for field in s.fields { |
| 101 | + c_type := g.types_type_to_c(field.typ) |
| 102 | + fname := escape_c_keyword(field.name) |
| 103 | + g.sb.writeln('\t\tsoa->${fname} = (${c_type}*)realloc(soa->${fname}, new_cap * sizeof(${c_type}));') |
| 104 | + } |
| 105 | + g.sb.writeln('\t\tsoa->cap = new_cap;') |
| 106 | + g.sb.writeln('\t}') |
| 107 | + for field in s.fields { |
| 108 | + fname := escape_c_keyword(field.name) |
| 109 | + g.sb.writeln('\tsoa->${fname}[soa->len] = val.${fname};') |
| 110 | + } |
| 111 | + g.sb.writeln('\tsoa->len++;') |
| 112 | + g.sb.writeln('}') |
| 113 | + g.sb.writeln('') |
| 114 | + |
| 115 | + // --- pop: remove and return last element --- |
| 116 | + g.sb.writeln('static inline ${name} ${soa_name}_pop(${soa_name}* soa) {') |
| 117 | + g.sb.writeln('\tif (soa->len == 0) return (${name}){0};') |
| 118 | + g.sb.writeln('\tsoa->len--;') |
| 119 | + g.sb.writeln('\treturn (${name}){') |
| 120 | + for i, field in s.fields { |
| 121 | + comma := if i < s.fields.len - 1 { ',' } else { '' } |
| 122 | + fname := escape_c_keyword(field.name) |
| 123 | + g.sb.writeln('\t\t.${fname} = soa->${fname}[soa->len]${comma}') |
| 124 | + } |
| 125 | + g.sb.writeln('\t};') |
| 126 | + g.sb.writeln('}') |
| 127 | + g.sb.writeln('') |
| 128 | + |
| 129 | + // --- free: deallocate all field arrays --- |
| 130 | + g.sb.writeln('static inline void ${soa_name}_free(${soa_name}* soa) {') |
| 131 | + for field in s.fields { |
| 132 | + fname := escape_c_keyword(field.name) |
| 133 | + g.sb.writeln('\tfree(soa->${fname});') |
| 134 | + } |
| 135 | + g.sb.writeln('\tsoa->len = 0;') |
| 136 | + g.sb.writeln('\tsoa->cap = 0;') |
| 137 | + g.sb.writeln('}') |
| 138 | + g.sb.writeln('') |
| 139 | +} |
0 commit comments