【发布时间】:2020-11-28 16:34:36
【问题描述】:
当我偶然发现这种奇怪的性能下降时,我正在玩一个简单的“游戏”来测试面向数据设计的不同方面。
我有这个结构来存储游戏船的数据:
constexpr int MAX_ENEMY_SHIPS = 4000000;
struct Ships
{
int32_t count;
v2 pos[MAX_ENEMY_SHIPS];
ShipMovement movements[MAX_ENEMY_SHIPS];
ShipDrawing drawings[MAX_ENEMY_SHIPS];
//ShipOtherData other[MAX_ENEMY_SHIPS];
void Add(Ship ship)
{
pos[count] = ship.pos;
movements[count] = { ship.dir, ship.speed };
drawings[count] = { ship.size, ship.color };
//other[count] = { ship.a, ship.b, ship.c, ship.d };
count++;
}
};
然后我有一个更新运动数据的功能:
void MoveShips(v2* positions, ShipMovement* movements, int count)
{
ScopeBenchmark bench("Move Ships");
for(int i = 0; i < count; ++i)
{
positions[i] = positions[i] + (movements[i].dir * movements[i].speed);
}
}
我的理解是,由于 MoveShips 函数仅使用位置和运动数组,因此 Ships 结构中的额外内存不会影响其性能。但是,当我取消注释 Ships 结构上的注释行时,性能会下降很多。使用当前的 MAX_ENEMY_SHIPS 值,我的计算机中 MoveShips 函数的持续时间从 10-11 毫秒变为 200-210 毫秒。
这里我举一个最小的、可重现的例子:
#include <stdlib.h>
#include <stdio.h>
#include <chrono>
#include <string>
class ScopeBenchmark
{
public:
ScopeBenchmark(std::string text)
: text(text)
{
begin = std::chrono::steady_clock::now();
}
~ScopeBenchmark()
{
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
printf("%s: %lli\n", text.data(), std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count());
}
private:
std::string text;
std::chrono::steady_clock::time_point begin;
};
constexpr int32_t Color(uint8_t r, uint8_t g, uint8_t b)
{
return (r << 16) | (g << 8) | b;
}
struct v2
{
float x;
float y;
};
inline v2 operator+(v2 a, v2 b)
{
v2 result;
result.x = a.x + b.x;
result.y = a.y + b.y;
return result;
}
inline v2 operator*(v2 a, float b)
{
v2 result;
result.x = a.x * b;
result.y = a.y * b;
return result;
}
//----------------------------------------------------------------------
struct Ship
{
v2 pos;
v2 size;
v2 dir;
float speed;
int32_t color;
v2 a;
v2 b;
v2 c;
v2 d;
};
struct ShipMovement
{
v2 dir;
float speed;
};
struct ShipDrawing
{
v2 size;
int32_t color;
};
struct ShipOtherData
{
v2 a;
v2 b;
v2 c;
v2 d;
};
constexpr int MAX_ENEMY_SHIPS = 4000000;
struct Ships
{
int32_t count;
v2 pos[MAX_ENEMY_SHIPS];
ShipMovement movements[MAX_ENEMY_SHIPS];
ShipDrawing drawings[MAX_ENEMY_SHIPS];
//ShipOtherData other[MAX_ENEMY_SHIPS];
void Add(Ship ship)
{
pos[count] = ship.pos;
movements[count] = { ship.dir, ship.speed };
drawings[count] = { ship.size, ship.color };
//other[count] = { ship.a, ship.b, ship.c, ship.d };
count++;
}
};
void MoveShips(v2* positions, ShipMovement* movements, int count)
{
ScopeBenchmark bench("Move Ships");
for(int i = 0; i < count; ++i)
{
positions[i] = positions[i] + (movements[i].dir * movements[i].speed);
}
}
struct Game
{
int32_t playerShipIndex;
Ships ships;
};
void InitGame(void* gameMemory)
{
Game* game = (Game*)gameMemory;
Ship ship;
ship.pos = { 0.0f, 0.0f };
ship.size = { 100.0f, 100.0f };
ship.speed = 1.0f;
ship.color = Color(64, 192, 32);
game->ships.Add(ship);
game->playerShipIndex = 0;
ship.speed *= 0.5f;
ship.dir.x = -1.0f;
ship.size = { 50.0f, 50.0f };
ship.color = Color(192, 64, 32);
for(int i = 0; i < MAX_ENEMY_SHIPS; i++)
{
ship.pos = { 500.0f, 350.0f };
game->ships.Add(ship);
}
}
int main()
{
Game* game = (Game*)malloc(sizeof(Game));
memset(game, 0, sizeof(Game));
InitGame(game);
while (true)
{
MoveShips(game->ships.pos, game->ships.movements, game->ships.count);
}
}
我使用 Visual Studio 编译器,并使用以下命令编译文件:
cl.exe /O2 /GL src/Game.cpp
那么,我的问题是:为什么在添加未使用的内存时,MoveShips 功能的性能下降如此之快?
【问题讨论】:
-
这只是意味着分配一个大对象比分配一个小对象需要更多时间。
-
嗯,它没有被使用,但仍然被分配。您要求 400 万个 8 字节的“东西”。使用或未使用,即每艘船 3200 万字节。对于一艘船,可行。对于一百或一千艘船,您开始谈论一些真实的记忆。以这种方式分配内存会使其远离其他往往会减慢速度的东西。然后,您必须将 3200 万次 Ships 的数量与系统上的可用内存进行比较。我猜你是在强迫你的系统进行大量交换,从而影响性能。
-
@Frank Merrow 为什么是 8 字节的东西?更有可能是 64 字节的......
-
这很奇怪。当我在 MS Visual Studio 2017 上运行此代码作为发布版本时,有时我总是得到输出 11,有时我每次总是得到大约 500 的输出。无论是 11 还是 500 在每个程序运行中始终相同,但在程序的单独调用中可能会有所不同,即使我不重新编译也是如此。因此,这种极端差异与问题中提到的行是否被注释掉无关(在我的测试中,它们总是没有被注释掉)。
-
使用
std::vector。
标签: c++ optimization data-oriented-design