Re:C Is Not Suited to SIMD
by Gibbon1 on Monday January 27, 2025 @04:45PM
(#42846017)
The C language spec isn't what important here it's the ABI doesn't know the difference.
[ Parent
| on HN ]
if using an int* over an int[] changes the memory layout of a struct, that necessarily implies a difference in the ABI.
As an example, C++'s std::array can be implemented as a struct containing a fixed-size C-style array. This can be passed-by-value. This means that returning a std::array can be more performant than returning a std::vector (which might be implemented as an int* that is reallocated when you add too many elements), because a std::array is returned on the stack while a std::vector is returned on the heap.
I was bit by this once when returning a std::unordered_map because I was doing a heap-allocation in a performance-critical section.
The memory layout is the ABI not the language spec.
The language spec for C constrains the memory layout of a struct. From 6.7.3.2.7 of the draft spec:
> a structure is a type consisting of a sequence of members, whose storage is allocated in an ordered sequence, (emphasis added)