Replace Deque with std::deque

Profiling indicated that a little time (< 10% of `ApplyBlockActions`'s
total execution time) is spent on `Deque` when a large chunk of block
is being removed. Thus even *if* this commit slows down things, the
performance impact would be insignificant.
This commit is contained in:
yvt 2019-07-24 18:07:42 +09:00
parent d1ba2a729b
commit 3b15a205ff
4 changed files with 47 additions and 165 deletions

View File

@ -180,7 +180,6 @@
E82E679818EA7972004DBA18 /* ClientUIHelper.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8F74CFC1845C8D50085AA54 /* ClientUIHelper.cpp */; };
E82E679918EA7972004DBA18 /* Clipboard.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8F74CFE184C753F0085AA54 /* Clipboard.cpp */; };
E82E679A18EA7972004DBA18 /* Math.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8CF03CC178EE95F000683D4 /* Math.cpp */; };
E82E679B18EA7972004DBA18 /* Deque.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E88318DF17925F2E002ABE6D /* Deque.cpp */; };
E82E679C18EA7972004DBA18 /* Stopwatch.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E88318E21792698D002ABE6D /* Stopwatch.cpp */; };
E82E679D18EA7972004DBA18 /* Debug.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E834F5541794BBD4004EBE88 /* Debug.cpp */; };
E82E679E18EA7972004DBA18 /* Settings.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E8E0AFB3179BF25B00C6B5A9 /* Settings.cpp */; };
@ -559,8 +558,6 @@
E88318DA179256E5002ABE6D /* Player.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Player.h; sourceTree = "<group>"; };
E88318DC179257F0002ABE6D /* GameMapWrapper.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GameMapWrapper.cpp; sourceTree = "<group>"; };
E88318DD179257F0002ABE6D /* GameMapWrapper.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GameMapWrapper.h; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
E88318DF17925F2E002ABE6D /* Deque.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Deque.cpp; sourceTree = "<group>"; };
E88318E017925F2E002ABE6D /* Deque.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Deque.h; sourceTree = "<group>"; };
E88318E21792698D002ABE6D /* Stopwatch.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Stopwatch.cpp; sourceTree = "<group>"; };
E88318E31792698D002ABE6D /* Stopwatch.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Stopwatch.h; sourceTree = "<group>"; };
E88318E517928EAC002ABE6D /* PhysicsConstants.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PhysicsConstants.h; sourceTree = "<group>"; };
@ -1606,8 +1603,6 @@
E8B6B6E617E40AF300E35523 /* ScriptBindings */,
E8CF03CC178EE95F000683D4 /* Math.cpp */,
E8CF03CD178EE95F000683D4 /* Math.h */,
E88318DF17925F2E002ABE6D /* Deque.cpp */,
E88318E017925F2E002ABE6D /* Deque.h */,
E88318E21792698D002ABE6D /* Stopwatch.cpp */,
E88318E31792698D002ABE6D /* Stopwatch.h */,
E834F5541794BBD4004EBE88 /* Debug.cpp */,
@ -2189,7 +2184,6 @@
E82E679918EA7972004DBA18 /* Clipboard.cpp in Sources */,
E82E679A18EA7972004DBA18 /* Math.cpp in Sources */,
E89A5F1E1DF8732200857F65 /* ShellApi.cpp in Sources */,
E82E679B18EA7972004DBA18 /* Deque.cpp in Sources */,
E82E679C18EA7972004DBA18 /* Stopwatch.cpp in Sources */,
E82E679D18EA7972004DBA18 /* Debug.cpp in Sources */,
E82E679E18EA7972004DBA18 /* Settings.cpp in Sources */,

View File

@ -20,14 +20,13 @@
#include <cstring>
#include <deque>
#include <queue>
#include <set>
#include <vector>
#include "GameMap.h"
#include "GameMapWrapper.h"
#include <Core/Debug.h>
#include <Core/Debug.h>
#include <Core/Deque.h>
#include <Core/Stopwatch.h>
namespace spades {
@ -58,44 +57,44 @@ namespace spades {
for (int y = 0; y < height; y++)
SetLink(x, y, depth - 1, Root);
Deque<CellPos> queue(width * height * 2);
std::deque<CellPos> queue(width * height * 2);
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
if (m.IsSolid(x, y, depth - 2)) {
SetLink(x, y, depth - 2, PositiveZ);
queue.Push(CellPos(x, y, depth - 2));
queue.push_back(CellPos(x, y, depth - 2));
}
while (!queue.IsEmpty()) {
CellPos p = queue.Front();
queue.Shift();
while (!queue.empty()) {
CellPos p = queue.front();
queue.pop_front();
int x = p.x, y = p.y, z = p.z;
if (p.x > 0 && m.IsSolid(x - 1, y, z) && GetLink(x - 1, y, z) == Invalid) {
SetLink(x - 1, y, z, PositiveX);
queue.Push(CellPos(x - 1, y, z));
queue.push_back(CellPos(x - 1, y, z));
}
if (p.x < width - 1 && m.IsSolid(x + 1, y, z) && GetLink(x + 1, y, z) == Invalid) {
SetLink(x + 1, y, z, NegativeX);
queue.Push(CellPos(x + 1, y, z));
queue.push_back(CellPos(x + 1, y, z));
}
if (p.y > 0 && m.IsSolid(x, y - 1, z) && GetLink(x, y - 1, z) == Invalid) {
SetLink(x, y - 1, z, PositiveY);
queue.Push(CellPos(x, y - 1, z));
queue.push_back(CellPos(x, y - 1, z));
}
if (p.y < height - 1 && m.IsSolid(x, y + 1, z) && GetLink(x, y + 1, z) == Invalid) {
SetLink(x, y + 1, z, NegativeY);
queue.Push(CellPos(x, y + 1, z));
queue.push_back(CellPos(x, y + 1, z));
}
if (p.z > 0 && m.IsSolid(x, y, z - 1) && GetLink(x, y, z - 1) == Invalid) {
SetLink(x, y, z - 1, PositiveZ);
queue.Push(CellPos(x, y, z - 1));
queue.push_back(CellPos(x, y, z - 1));
}
if (p.z < depth - 1 && m.IsSolid(x, y, z + 1) && GetLink(x, y, z + 1) == Invalid) {
SetLink(x, y, z + 1, NegativeZ);
queue.Push(CellPos(x, y, z + 1));
queue.push_back(CellPos(x, y, z + 1));
}
}
@ -149,11 +148,11 @@ namespace spades {
return;
// if there's invalid block around this block,
// rebuild tree
Deque<CellPos> queue(1024);
queue.Push(CellPos(x, y, z));
while (!queue.IsEmpty()) {
CellPos p = queue.Front();
queue.Shift();
std::deque<CellPos> queue{1024};
queue.push_back(CellPos(x, y, z));
while (!queue.empty()) {
CellPos p = queue.front();
queue.pop_front();
int x = p.x, y = p.y, z = p.z;
SPAssert(m.IsSolid(x, y, z));
@ -163,32 +162,32 @@ namespace spades {
if (p.x > 0 && m.IsSolid(x - 1, y, z) && GetLink(x - 1, y, z) == Invalid &&
thisLink != NegativeX) {
SetLink(x - 1, y, z, PositiveX);
queue.Push(CellPos(x - 1, y, z));
queue.push_back(CellPos(x - 1, y, z));
}
if (p.x < width - 1 && m.IsSolid(x + 1, y, z) && GetLink(x + 1, y, z) == Invalid &&
thisLink != PositiveX) {
SetLink(x + 1, y, z, NegativeX);
queue.Push(CellPos(x + 1, y, z));
queue.push_back(CellPos(x + 1, y, z));
}
if (p.y > 0 && m.IsSolid(x, y - 1, z) && GetLink(x, y - 1, z) == Invalid &&
thisLink != NegativeY) {
SetLink(x, y - 1, z, PositiveY);
queue.Push(CellPos(x, y - 1, z));
queue.push_back(CellPos(x, y - 1, z));
}
if (p.y < height - 1 && m.IsSolid(x, y + 1, z) && GetLink(x, y + 1, z) == Invalid &&
thisLink != PositiveY) {
SetLink(x, y + 1, z, NegativeY);
queue.Push(CellPos(x, y + 1, z));
queue.push_back(CellPos(x, y + 1, z));
}
if (p.z > 0 && m.IsSolid(x, y, z - 1) && GetLink(x, y, z - 1) == Invalid &&
thisLink != NegativeZ) {
SetLink(x, y, z - 1, PositiveZ);
queue.Push(CellPos(x, y, z - 1));
queue.push_back(CellPos(x, y, z - 1));
}
if (p.z < depth - 1 && m.IsSolid(x, y, z + 1) && GetLink(x, y, z + 1) == Invalid &&
thisLink != PositiveZ) {
SetLink(x, y, z + 1, NegativeZ);
queue.Push(CellPos(x, y, z + 1));
queue.push_back(CellPos(x, y, z + 1));
}
}
}
@ -207,7 +206,7 @@ namespace spades {
// solid, but unlinked cells
std::vector<CellPos> unlinkedCells;
Deque<CellPos> queue(1024);
std::deque<CellPos> queue{1024};
// unlink children
for (size_t i = 0; i < cells.size(); i++) {
@ -223,11 +222,11 @@ namespace spades {
SPAssert(GetLink(pos.x, pos.y, pos.z) != Root);
SetLink(pos.x, pos.y, pos.z, Invalid);
queue.Push(pos);
queue.push_back(pos);
while (!queue.IsEmpty()) {
pos = queue.Front();
queue.Shift();
while (!queue.empty()) {
pos = queue.front();
queue.pop_front();
if (m.IsSolid(pos.x, pos.y, pos.z))
unlinkedCells.push_back(pos);
@ -237,32 +236,32 @@ namespace spades {
if (x > 0 && EqualTwoCond(GetLink(x - 1, y, z), PositiveX, Invalid,
m.IsSolid(x - 1, y, z))) {
SetLink(x - 1, y, z, Marked);
queue.Push(CellPos(x - 1, y, z));
queue.push_back(CellPos(x - 1, y, z));
}
if (x < width - 1 && EqualTwoCond(GetLink(x + 1, y, z), NegativeX, Invalid,
m.IsSolid(x + 1, y, z))) {
SetLink(x + 1, y, z, Marked);
queue.Push(CellPos(x + 1, y, z));
queue.push_back(CellPos(x + 1, y, z));
}
if (y > 0 && EqualTwoCond(GetLink(x, y - 1, z), PositiveY, Invalid,
m.IsSolid(x, y - 1, z))) {
SetLink(x, y - 1, z, Marked);
queue.Push(CellPos(x, y - 1, z));
queue.push_back(CellPos(x, y - 1, z));
}
if (y < height - 1 && EqualTwoCond(GetLink(x, y + 1, z), NegativeY, Invalid,
m.IsSolid(x, y + 1, z))) {
SetLink(x, y + 1, z, Marked);
queue.Push(CellPos(x, y + 1, z));
queue.push_back(CellPos(x, y + 1, z));
}
if (z > 0 && EqualTwoCond(GetLink(x, y, z - 1), PositiveZ, Invalid,
m.IsSolid(x, y, z - 1))) {
SetLink(x, y, z - 1, Marked);
queue.Push(CellPos(x, y, z - 1));
queue.push_back(CellPos(x, y, z - 1));
}
if (z < depth - 1 && EqualTwoCond(GetLink(x, y, z + 1), NegativeZ, Invalid,
m.IsSolid(x, y, z + 1))) {
SetLink(x, y, z + 1, Marked);
queue.Push(CellPos(x, y, z + 1));
queue.push_back(CellPos(x, y, z + 1));
}
}
}
@ -274,7 +273,7 @@ namespace spades {
SetLink(pos.x, pos.y, pos.z, Invalid);
}
SPAssert(queue.IsEmpty());
SPAssert(queue.empty());
// start relinking
for (size_t i = 0; i < unlinkedCells.size(); i++) {
@ -303,13 +302,13 @@ namespace spades {
if (newLink != Invalid) {
SetLink(x, y, z, newLink);
queue.Push(pos);
queue.push_back(pos);
}
}
while (!queue.IsEmpty()) {
CellPos p = queue.Front();
queue.Shift();
while (!queue.empty()) {
CellPos p = queue.front();
queue.pop_front();
int x = p.x, y = p.y, z = p.z;
LinkType thisLink = GetLink(x, y, z);
@ -317,32 +316,32 @@ namespace spades {
if (p.x > 0 && m.IsSolid(x - 1, y, z) && GetLink(x - 1, y, z) == Invalid &&
thisLink != NegativeX) {
SetLink(x - 1, y, z, PositiveX);
queue.Push(CellPos(x - 1, y, z));
queue.push_back(CellPos(x - 1, y, z));
}
if (p.x < width - 1 && m.IsSolid(x + 1, y, z) && GetLink(x + 1, y, z) == Invalid &&
thisLink != PositiveX) {
SetLink(x + 1, y, z, NegativeX);
queue.Push(CellPos(x + 1, y, z));
queue.push_back(CellPos(x + 1, y, z));
}
if (p.y > 0 && m.IsSolid(x, y - 1, z) && GetLink(x, y - 1, z) == Invalid &&
thisLink != NegativeY) {
SetLink(x, y - 1, z, PositiveY);
queue.Push(CellPos(x, y - 1, z));
queue.push_back(CellPos(x, y - 1, z));
}
if (p.y < height - 1 && m.IsSolid(x, y + 1, z) && GetLink(x, y + 1, z) == Invalid &&
thisLink != PositiveY) {
SetLink(x, y + 1, z, NegativeY);
queue.Push(CellPos(x, y + 1, z));
queue.push_back(CellPos(x, y + 1, z));
}
if (p.z > 0 && m.IsSolid(x, y, z - 1) && GetLink(x, y, z - 1) == Invalid &&
thisLink != NegativeZ) {
SetLink(x, y, z - 1, PositiveZ);
queue.Push(CellPos(x, y, z - 1));
queue.push_back(CellPos(x, y, z - 1));
}
if (p.z < depth - 1 && m.IsSolid(x, y, z + 1) && GetLink(x, y, z + 1) == Invalid &&
thisLink != PositiveZ) {
SetLink(x, y, z + 1, NegativeZ);
queue.Push(CellPos(x, y, z + 1));
queue.push_back(CellPos(x, y, z + 1));
}
}
@ -360,5 +359,5 @@ namespace spades {
return floatingBlocks;
}
}
}
} // namespace client
} // namespace spades

View File

@ -1,21 +0,0 @@
/*
Copyright (c) 2013 yvt
This file is part of OpenSpades.
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Deque.h"

View File

@ -1,90 +0,0 @@
/*
Copyright (c) 2013 yvt
This file is part of OpenSpades.
OpenSpades is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenSpades is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstdio>
#include <cstdlib>
#include <Core/Debug.h>
namespace spades {
/** Deque implementation. NPOT is not fully supported. */
template <typename T> class Deque {
T *ptr;
size_t length;
size_t startPos;
size_t capacity;
public:
Deque(size_t cap) {
ptr = (T *)malloc(sizeof(T) * cap);
startPos = 0;
length = 0;
capacity = cap;
}
~Deque() { free(ptr); }
void Reserve(size_t newCap) {
if (newCap <= capacity)
return;
T *newPtr = (T *)malloc(sizeof(T) * newCap);
size_t pos = startPos;
for (size_t i = 0; i < length; i++) {
newPtr[i] = ptr[pos++];
if (pos == capacity)
pos = 0;
}
free(ptr);
ptr = newPtr;
startPos = 0;
capacity = newCap;
}
void Push(const T &e) {
if (length + 1 > capacity) {
size_t newCap = capacity;
while (newCap < length + 1)
newCap <<= 1;
Reserve(newCap);
}
size_t pos = startPos + length;
if (pos >= capacity)
pos -= capacity;
ptr[pos] = e;
length++;
}
T &Front() { return ptr[startPos]; }
void Shift() {
SPAssert(length > 0);
startPos++;
if (startPos == capacity)
startPos = 0;
length--;
}
size_t GetLength() const { return length; }
bool IsEmpty() const { return length == 0; }
};
}