【问题标题】:Which struct design has better spatial locality?哪种结构设计具有更好的空间局部性?
【发布时间】:2010-11-27 19:37:48
【问题描述】:
struct{
    Vector3* centers;
    float*  radii;
    float*  colors;
    unsigned int size;
}Spheres;

struct Sphere{
    Vector3 center;
    float  radius;
    float  color;
};

struct{
    struct Sphere* spheres;
    unsigned int size;
}Spheres;

使用示例

void spheres_process(){
    int i;
    for(i = 0; i < Spheres.size; ++i){
         // do something with this sphere
    }
}

我认为第二种情况具有更好的空间局部性,因为所有数据都是交错的,应该同时加载到缓存中。在这两种情况下,我将同时处理所有球体。有什么意见吗?

【问题讨论】:

    标签: c caching data-oriented-design


    【解决方案1】:

    我可以建议您在 cachegrind 或其他一些缓存分析器下尝试使用您的数据集吗?这可能比空间局部性等理论更有效。根据代码的访问模式,您可能会得出一些令人惊讶的结果。

    【讨论】:

      【解决方案2】:

      我们缺乏重要的细节,即目标架构。

      两种方式都可以达到相同的空间局部性。

      【讨论】:

      • 目标架构是 x86,我使用的是 GCC 4.5.0。你还想要什么其他细节?
      猜你喜欢
      • 2011-03-14
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 2011-11-26
      • 2021-03-30
      • 2011-02-20
      • 2010-09-29
      相关资源
      最近更新 更多