I'm doing a Monte-Carlo simulation of a computer game which is basically the following:
void simulate(Game game)
{
for (int i = 0; i < some_stuff)
{
Game game_copy = game;
// change copy
simulate(std::move(game_copy));
}
}
Now, Game
is full of vector<>
s that are very expensive to copy.
What I found is that the above version is the fastest version in terms of avoiding copying, despite trying:
- Taking the argument as
Game&
,const Game&
andGame&&
- Passing the argument with and without
std::move
Can someone explain it to me? I don't understand how changing the argument type from pass-by-value to pass-by-reference suddenly reintroduces all those copying operations that I've managed to avoid. What's going on here? Is it the optimizing compiler that's at work?