[Blog post] Why and when you should use SoA

maik klein via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Sun Mar 27 10:48:47 PDT 2016


On Sunday, 27 March 2016 at 16:18:18 UTC, ZombineDev wrote:
> On Saturday, 26 March 2016 at 20:55:17 UTC, maik klein wrote:
>> [snip]
>>
>> Thanks, yes that is simpler.
>>
>> But I am not sure that I want to have pluggable containers in 
>> SOA, mostly because every field would have overhead from the 
>> container.
>>
>> For example array has size, length etc as overhead, but it is 
>> also not that much and probably won't matter anyway.
>>
>> But I also thought about it, maybe sometimes I want to use a 
>> map instead of an array for some fields. So I need to have a 
>> way of telling which field should get which container.
>>
>> Maybe something like this:
>>
>> SOA!(Foo, Array, HashMap, DList);
>>
>> The current implementation is mostly for experimentation.
>
> Never mind. Anything with memory representation different from 
> an array would ruin cache locality. My thinking was that using 
> a container defined somewhere else would simplify the code.
>
> I tried a couple approaches and came up with the following, 
> which I think this is the most efficient in terms of space 
> overhead and number of allocations (but still generic), 
> implementation that is possible:
> http://dpaste.dzfl.pl/3de1e18756f8
>
> It took me a couple of tries, but overall I'm satisfied my 
> code, although is it's more low-level and more meta-heavy than 
> yours.

I also thought about doing it this way but I wasn't sure that it 
would be better overall.

I am not sure that one big buffer is better than several smaller 
ones overall.

I mean it is definitely more space efficient because you only 
have one pointer and reallocation is one big reallocation instead 
of smaller ones.

But it seems to me that smaller reallocations might be cheaper 
because you should have a higher chance of growing without 
reallocating.

Then again your approach will have no fragmented memory at all 
which might also be a good thing.

I just have not enough knowledge to know exactly what is better. 
Maybe we could maintain our implementations side by side and 
benchmark them for certain scenarios.

A lot of functionality is still missing in my implementation.


More information about the Digitalmars-d-announce mailing list