SOA causing stack overflow? Max size?

Hi,

I am working on a small odin/raylib program as a way to learn and I am getting a warning I don’t understand. Specifically, the warning is about a potential stack overflow when trying to create a SOA with “component” arrays bigger than ~256 KB. The warning persists after trying a few different memory allocation techniques to see if I could ensure the SOAwasn’t stack allocated (eg. arena allocator >1 MB in size) . The code below shows a minimal recreation that creates the warning.

package main

import "core:fmt"

vec3 :: [3]f32
n :: 32 * 1024

test :: struct {
	x: vec3,
}

main :: proc() {
	example_soa:= new(#soa[n]test)
	fmt.println(size_of(example_soa^))
}

When I increase the array size further, the exe crashes on launch (full code, not the above). Possibly the stack overflowing?

Is a SOA always stack allocated? Any help in resolving the stack overflow or insight into the issue would be greatly appreciated! Thanks very much.

1 Like

This problem only appears when you use the soa, not when you use it as an array of structures. This probably happens because, internally, one element of the aos is a struct that takes 12 bytes, while one element of the soa is an array that takes 393,216 bytes.

To take the example further, if the struct included additional fields y and z, then one aos element becomes 36 bytes, while one soa element remains 393,216 bytes.

It is interesting that the warning points to the struct declaration (line 9) and not the #soa implementation (line 13).

So the problem here is that the warning is in the wrong place.

1 Like

I was hoping to learn how to have a bigger soa.

The answer appears to be to directly create a structure of arrays. Those can be huge, and don’t require any behind-the-scenes shuffling.