【发布时间】:2012-06-03 22:10:11
【问题描述】:
我试图通过面向数据的设计来定位我的代码以尽可能高效地使用缓存,这是我第一次考虑这样的事情。我想出了一种方法来循环在屏幕上绘制精灵的相同指令,发送到函数的向量包括所有游戏实体的位置和精灵。
我的问题是条件语句是否从指令缓存中删除了绘图函数,从而破坏了我的计划?还是我在做什么通常是疯了?
struct position
{
position(int x_, int y_):x(x_), y(Y_)
int x,y;
};
vector<position> thePositions;
vector<sprite> theSprites;
vector<int> theNoOfEntities; //eg 3 things, 4 thingies, 36 dodahs
int noOfEntitesTotal;
//invoking the draw function
draw(&thePositions[0], &theSprites[0], &theNoOfEntities[0], noOfEntitesTotal)
void draw(position* thepos, sprite* thesp, int* theints, int totalsize)
{
for(int j=0;int i=0;i<totalsize;i++)
{
j+=i%size[j]?1:0;
thesp[j].draw(thepos[i]);
}
}
【问题讨论】:
-
为什么不存储
pair<sprite, position>并传递迭代器进行绘制。缓存局部性会更好,它会少很多模糊和存储无关。 -
数据缓存是否必须在每个函数调用中查找每一对?我的解决方案试图在缓存中维护所有数据和指令,并且没有缓存未命中,只是循环重复需要多少个周期。
-
条件语句与指令缓存关系不大。
-
为什么你认为条件语句可能和指令缓存有关系?
-
j+=i%size[j]?1:0;可能存在一些分支预测问题,但不太可能以任何明显的方式影响指令缓存。
标签: c++ cache-control data-oriented-design