Which struct design has better spatial locality?
-
28-09-2019 - |
Pergunta
struct{
Vector3* centers;
float* radii;
float* colors;
unsigned int size;
}Spheres;
versus
struct Sphere{
Vector3 center;
float radius;
float color;
};
struct{
struct Sphere* spheres;
unsigned int size;
}Spheres;
usage example
void spheres_process(){
int i;
for(i = 0; i < Spheres.size; ++i){
// do something with this sphere
}
}
I'm thinking that the second case has better spatial locality since all data is interleaved and should be loaded into the cache at the same time. In both cases I will be processing all spheres at the same time. Any input?
Solução
May I suggest trying both with your dataset under cachegrind or some other cache profiler? That might be more effective than theorising on spatial locality etc. Depending on the access patterns of your code you might come up with some surprising results.
Outras dicas
We lack important details, the target architecture to start with.
Both ways can achieve the same spatial locality.
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow