【发布时间】:2013-10-02 09:58:35
【问题描述】:
我正在考虑使用 map 将自定义 ID 映射到 vector 索引,例如;
struct Mesh
{
GLsizei mIndices;
GLuint mVBO;
GLuint mIndexBuffer;
GLuint mVAO;
size_t vertexDataSize;
size_t normalDataSize;
};
typedef uint32_t MeshID;
std::map<MeshID, uint16_t> gMeshIDIndexMap;
std::vector<Mesh> gMeshes;
std::vector<MeshID> drawCmds = {1, 1, 2, 3, 4, 5, 8, ,8 ,8, 9, 10, ...}; //sorted
std::for_each(drawCmds.begin(), drawCmds.end(), [](MeshID& id)
{
Mesh& mesh = gMeshes[gMeshIDIndexMap.at(id)];
glBindVertexArray(mesh.mVAO);
glDrawElements(GL_TRIANGLES, mesh.mIndexBuffer, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
....
}
由于 std::map 不将其元素存储在连续内存中,因此在每次新迭代时gMeshIDIndexMap.at(id) 都必须将一个全新的缓存行加载到缓存中。这会破坏缓存并导致大量缓存未命中,对吗?我可以做些什么来改进这一点并尽可能减少缓存未命中?
【问题讨论】:
-
根据地图的大小和使用方式,
boost::container::flat_map可能是一个不错的选择。 -
关于你如何使用地图?
-
@KaiserJohann 很公平,我把它写在一个答案中。
标签: c++ caching memory-management c++11