Skip to content

Commit

Permalink
perf: std::forward_list to std::vector (#2731)
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah committed Jul 11, 2024
1 parent 284897f commit 4920394
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 65 deletions.
29 changes: 16 additions & 13 deletions src/creatures/combat/combat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ CombatDamage Combat::getCombatDamage(std::shared_ptr<Creature> creature, std::sh
return damage;
}

void Combat::getCombatArea(const Position &centerPos, const Position &targetPos, const std::unique_ptr<AreaCombat> &area, std::forward_list<std::shared_ptr<Tile>> &list) {
void Combat::getCombatArea(const Position &centerPos, const Position &targetPos, const std::unique_ptr<AreaCombat> &area, std::vector<std::shared_ptr<Tile>> &list) {
if (targetPos.z >= MAP_MAX_LAYERS) {
return;
}

if (area) {
area->getList(centerPos, targetPos, list);
} else {
list.push_front(g_game().map.getOrCreateTile(targetPos));
list.emplace_back(g_game().map.getOrCreateTile(targetPos));
}
}

Expand Down Expand Up @@ -1119,7 +1119,7 @@ bool Combat::doCombat(std::shared_ptr<Creature> caster, const Position &position
}

void Combat::CombatFunc(std::shared_ptr<Creature> caster, const Position &origin, const Position &pos, const std::unique_ptr<AreaCombat> &area, const CombatParams &params, CombatFunction func, CombatDamage* data) {
std::forward_list<std::shared_ptr<Tile>> tileList;
std::vector<std::shared_ptr<Tile>> tileList;

if (caster) {
getCombatArea(caster->getPosition(), pos, area, tileList);
Expand Down Expand Up @@ -1827,26 +1827,29 @@ AreaCombat::AreaCombat(const AreaCombat &rhs) {
}
}

void AreaCombat::getList(const Position &centerPos, const Position &targetPos, std::forward_list<std::shared_ptr<Tile>> &list) const {
void AreaCombat::getList(const Position &centerPos, const Position &targetPos, std::vector<std::shared_ptr<Tile>> &list) const {
const std::unique_ptr<MatrixArea> &area = getArea(centerPos, targetPos);
if (!area) {
return;
}

uint32_t centerY, centerX;
uint32_t centerY;
uint32_t centerX;
area->getCenter(centerY, centerX);

const uint32_t rows = area->getRows();
const uint32_t cols = area->getCols();
list.reserve(rows * cols);

Position tmpPos(targetPos.x - centerX, targetPos.y - centerY, targetPos.z);
uint32_t cols = area->getCols();
for (uint32_t y = 0, rows = area->getRows(); y < rows; ++y) {
for (uint32_t x = 0; x < cols; ++x) {
if (area->getValue(y, x) != 0 && g_game().isSightClear(targetPos, tmpPos, true)) {
list.push_front(g_game().map.getOrCreateTile(tmpPos));
for (uint32_t y = 0; y < rows; ++y, ++tmpPos.y, tmpPos.x -= cols) {
for (uint32_t x = 0; x < cols; ++x, ++tmpPos.x) {
if (area->getValue(y, x) != 0) {
if (g_game().isSightClear(targetPos, tmpPos, true)) {
list.emplace_back(g_game().map.getOrCreateTile(tmpPos));
}
}
tmpPos.x++;
}
tmpPos.x -= cols;
tmpPos.y++;
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/creatures/combat/combat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class ChainPickerCallback final : public CallBack {
};

struct CombatParams {
std::forward_list<std::shared_ptr<Condition>> conditionList;
std::vector<std::shared_ptr<Condition>> conditionList;

std::unique_ptr<ValueCallback> valueCallback;
std::unique_ptr<TileCallback> tileCallback;
Expand Down Expand Up @@ -218,7 +218,7 @@ class AreaCombat {
// non-assignable
AreaCombat &operator=(const AreaCombat &) = delete;

void getList(const Position &centerPos, const Position &targetPos, std::forward_list<std::shared_ptr<Tile>> &list) const;
void getList(const Position &centerPos, const Position &targetPos, std::vector<std::shared_ptr<Tile>> &list) const;

void setupArea(const std::list<uint32_t> &list, uint32_t rows);
void setupArea(int32_t length, int32_t spread);
Expand Down Expand Up @@ -290,7 +290,7 @@ class Combat {
static void doCombatDispel(std::shared_ptr<Creature> caster, std::shared_ptr<Creature> target, const CombatParams &params);
static void doCombatDispel(std::shared_ptr<Creature> caster, const Position &position, const std::unique_ptr<AreaCombat> &area, const CombatParams &params);

static void getCombatArea(const Position &centerPos, const Position &targetPos, const std::unique_ptr<AreaCombat> &area, std::forward_list<std::shared_ptr<Tile>> &list);
static void getCombatArea(const Position &centerPos, const Position &targetPos, const std::unique_ptr<AreaCombat> &area, std::vector<std::shared_ptr<Tile>> &list);

static bool isInPvpZone(std::shared_ptr<Creature> attacker, std::shared_ptr<Creature> target);
static bool isProtected(std::shared_ptr<Player> attacker, std::shared_ptr<Player> target);
Expand Down Expand Up @@ -320,7 +320,7 @@ class Combat {
return area != nullptr;
}
void addCondition(const std::shared_ptr<Condition> condition) {
params.conditionList.emplace_front(condition);
params.conditionList.emplace_back(condition);
}
void setPlayerCombatValues(formulaType_t formulaType, double mina, double minb, double maxa, double maxb);
void postCombatEffects(std::shared_ptr<Creature> caster, const Position &origin, const Position &pos) const {
Expand Down
3 changes: 1 addition & 2 deletions src/creatures/monsters/spawns/spawn_monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ bool SpawnsMonster::loadFromXML(const std::string &filemonstername) {
continue;
}

spawnMonsterList.emplace_front(centerPos, radius);
SpawnMonster &spawnMonster = spawnMonsterList.front();
SpawnMonster &spawnMonster = spawnMonsterList.emplace_back(centerPos, radius);

for (auto childMonsterNode : spawnMonsterNode.children()) {
if (strcasecmp(childMonsterNode.name(), "monster") == 0) {
Expand Down
13 changes: 7 additions & 6 deletions src/creatures/monsters/spawns/spawn_monster.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class SpawnMonster {
centerPos(initPos), radius(initRadius) { }
~SpawnMonster();

SpawnMonster(SpawnMonster &&) { }
SpawnMonster &operator=(SpawnMonster &&) {
return *this;
}

// non-copyable
SpawnMonster(const SpawnMonster &) = delete;
SpawnMonster &operator=(const SpawnMonster &) = delete;
Expand Down Expand Up @@ -83,23 +88,19 @@ class SpawnsMonster {
bool loadFromXML(const std::string &filemonstername);
void startup();
void clear();
SpawnMonster &addSpawnMonster(const Position &pos, int32_t radius) {
spawnMonsterList.emplace_front(pos, radius);
return spawnMonsterList.front();
}

bool isStarted() const {
return started;
}
bool isLoaded() const {
return loaded;
}
std::forward_list<SpawnMonster> &getspawnMonsterList() {
std::vector<SpawnMonster> &getspawnMonsterList() {
return spawnMonsterList;
}

private:
std::forward_list<SpawnMonster> spawnMonsterList;
std::vector<SpawnMonster> spawnMonsterList;
std::string filemonstername;
bool loaded = false;
bool started = false;
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/npcs/spawns/spawn_npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ bool SpawnsNpc::loadFromXml(const std::string &fileNpcName) {
continue;
}

const auto &spawnNpc = spawnNpcList.emplace_front(std::make_shared<SpawnNpc>(centerPos, radius));
const auto &spawnNpc = spawnNpcList.emplace_back(std::make_shared<SpawnNpc>(centerPos, radius));

for (auto childNode : spawnNode.children()) {
if (strcasecmp(childNode.name(), "npc") == 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/creatures/npcs/spawns/spawn_npc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ class SpawnsNpc {
return fileName = std::move(setName);
}

std::forward_list<std::shared_ptr<SpawnNpc>> &getSpawnNpcList() {
std::vector<std::shared_ptr<SpawnNpc>> &getSpawnNpcList() {
return spawnNpcList;
}

private:
std::forward_list<std::shared_ptr<SpawnNpc>> spawnNpcList;
std::vector<std::shared_ptr<SpawnNpc>> spawnNpcList;
std::string fileName;
bool loaded = false;
bool started = false;
Expand Down
20 changes: 11 additions & 9 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5289,12 +5289,12 @@ double Player::getLostPercent() const {

void Player::learnInstantSpell(const std::string &spellName) {
if (!hasLearnedInstantSpell(spellName)) {
learnedInstantSpellList.push_front(spellName);
learnedInstantSpellList.emplace_back(spellName);
}
}

void Player::forgetInstantSpell(const std::string &spellName) {
learnedInstantSpellList.remove(spellName);
std::erase(learnedInstantSpellList, spellName);
}

bool Player::hasLearnedInstantSpell(const std::string &spellName) const {
Expand Down Expand Up @@ -5700,12 +5700,12 @@ bool Player::addPartyInvitation(std::shared_ptr<Party> newParty) {
return false;
}

invitePartyList.push_front(newParty);
invitePartyList.emplace_back(newParty);
return true;
}

void Player::removePartyInvitation(std::shared_ptr<Party> remParty) {
invitePartyList.remove(remParty);
std::erase(invitePartyList, remParty);
}

void Player::clearPartyInvitations() {
Expand Down Expand Up @@ -6124,15 +6124,15 @@ bool Player::hasModalWindowOpen(uint32_t modalWindowId) const {
}

void Player::onModalWindowHandled(uint32_t modalWindowId) {
modalWindows.remove(modalWindowId);
std::erase(modalWindows, modalWindowId);
}

void Player::sendModalWindow(const ModalWindow &modalWindow) {
if (!client) {
return;
}

modalWindows.push_front(modalWindow.id);
modalWindows.emplace_back(modalWindow.id);
client->sendModalWindow(modalWindow);
}

Expand Down Expand Up @@ -6251,8 +6251,10 @@ size_t Player::getMaxDepotItems() const {
return g_configManager().getNumber(FREE_DEPOT_LIMIT, __FUNCTION__);
}

std::forward_list<std::shared_ptr<Condition>> Player::getMuteConditions() const {
std::forward_list<std::shared_ptr<Condition>> muteConditions;
std::vector<std::shared_ptr<Condition>> Player::getMuteConditions() const {
std::vector<std::shared_ptr<Condition>> muteConditions;
muteConditions.reserve(conditions.size());

for (const std::shared_ptr<Condition> &condition : conditions) {
if (condition->getTicks() <= 0) {
continue;
Expand All @@ -6263,7 +6265,7 @@ std::forward_list<std::shared_ptr<Condition>> Player::getMuteConditions() const
continue;
}

muteConditions.push_front(condition);
muteConditions.emplace_back(condition);
}
return muteConditions;
}
Expand Down
10 changes: 5 additions & 5 deletions src/creatures/players/player.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2664,7 +2664,7 @@ class Player final : public Creature, public Cylinder, public Bankable {
static uint32_t playerFirstID;
static uint32_t playerLastID;

std::forward_list<std::shared_ptr<Condition>> getMuteConditions() const;
std::vector<std::shared_ptr<Condition>> getMuteConditions() const;

void checkTradeState(std::shared_ptr<Item> item);
bool hasCapacity(std::shared_ptr<Item> item, uint32_t count) const;
Expand Down Expand Up @@ -2760,11 +2760,11 @@ class Player final : public Creature, public Cylinder, public Bankable {

GuildWarVector guildWarVector;

std::forward_list<std::shared_ptr<Party>> invitePartyList;
std::forward_list<uint32_t> modalWindows;
std::forward_list<std::string> learnedInstantSpellList;
std::vector<std::shared_ptr<Party>> invitePartyList;
std::vector<uint32_t> modalWindows;
std::vector<std::string> learnedInstantSpellList;
// TODO: This variable is only temporarily used when logging in, get rid of it somehow.
std::forward_list<std::shared_ptr<Condition>> storedConditionList;
std::vector<std::shared_ptr<Condition>> storedConditionList;

std::unordered_set<std::shared_ptr<MonsterType>> m_bestiaryMonsterTracker;
std::unordered_set<std::shared_ptr<MonsterType>> m_bosstiaryMonsterTracker;
Expand Down
4 changes: 2 additions & 2 deletions src/io/functions/iologindata_load_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ void IOLoginDataLoad::loadPlayerConditions(std::shared_ptr<Player> player, DBRes
auto condition = Condition::createCondition(propStream);
while (condition) {
if (condition->unserialize(propStream)) {
player->storedConditionList.push_front(condition);
player->storedConditionList.emplace_back(condition);
}
condition = Condition::createCondition(propStream);
}
Expand Down Expand Up @@ -465,7 +465,7 @@ void IOLoginDataLoad::loadPlayerInstantSpellList(std::shared_ptr<Player> player,
query << "SELECT `player_id`, `name` FROM `player_spells` WHERE `player_id` = " << player->getGUID();
if ((result = db.storeQuery(query.str()))) {
do {
player->learnedInstantSpellList.emplace_front(result->getString("name"));
player->learnedInstantSpellList.emplace_back(result->getString("name"));
} while (result->next());
}
}
Expand Down
25 changes: 13 additions & 12 deletions src/io/iologindata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,14 @@ bool IOLoginData::hasBiddedOnHouse(uint32_t guid) {
return db.storeQuery(query.str()).get() != nullptr;
}

std::forward_list<VIPEntry> IOLoginData::getVIPEntries(uint32_t accountId) {
std::forward_list<VIPEntry> entries;

std::vector<VIPEntry> IOLoginData::getVIPEntries(uint32_t accountId) {
std::string query = fmt::format("SELECT `player_id`, (SELECT `name` FROM `players` WHERE `id` = `player_id`) AS `name`, `description`, `icon`, `notify` FROM `account_viplist` WHERE `account_id` = {}", accountId);
std::vector<VIPEntry> entries;

DBResult_ptr result = Database::getInstance().storeQuery(query);
if (result) {
if (const auto &result = Database::getInstance().storeQuery(query)) {
entries.reserve(result->countResults());
do {
entries.emplace_front(
entries.emplace_back(
result->getNumber<uint32_t>("player_id"),
result->getString("name"),
result->getString("description"),
Expand All @@ -366,6 +365,7 @@ std::forward_list<VIPEntry> IOLoginData::getVIPEntries(uint32_t accountId) {
);
} while (result->next());
}

return entries;
}

Expand All @@ -388,15 +388,16 @@ void IOLoginData::removeVIPEntry(uint32_t accountId, uint32_t guid) {
g_database().executeQuery(query);
}

std::forward_list<VIPGroupEntry> IOLoginData::getVIPGroupEntries(uint32_t accountId, uint32_t guid) {
std::forward_list<VIPGroupEntry> entries;

std::vector<VIPGroupEntry> IOLoginData::getVIPGroupEntries(uint32_t accountId, uint32_t guid) {
std::string query = fmt::format("SELECT `id`, `name`, `customizable` FROM `account_vipgroups` WHERE `account_id` = {}", accountId);

DBResult_ptr result = g_database().storeQuery(query);
if (result) {
std::vector<VIPGroupEntry> entries;

if (const auto &result = g_database().storeQuery(query)) {
entries.reserve(result->countResults());

do {
entries.emplace_front(
entries.emplace_back(
result->getNumber<uint8_t>("id"),
result->getString("name"),
result->getNumber<uint8_t>("customizable") == 0 ? false : true
Expand Down
4 changes: 2 additions & 2 deletions src/io/iologindata.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ class IOLoginData {
static void increaseBankBalance(uint32_t guid, uint64_t bankBalance);
static bool hasBiddedOnHouse(uint32_t guid);

static std::forward_list<VIPEntry> getVIPEntries(uint32_t accountId);
static std::vector<VIPEntry> getVIPEntries(uint32_t accountId);
static void addVIPEntry(uint32_t accountId, uint32_t guid, const std::string &description, uint32_t icon, bool notify);
static void editVIPEntry(uint32_t accountId, uint32_t guid, const std::string &description, uint32_t icon, bool notify);
static void removeVIPEntry(uint32_t accountId, uint32_t guid);

static std::forward_list<VIPGroupEntry> getVIPGroupEntries(uint32_t accountId, uint32_t guid);
static std::vector<VIPGroupEntry> getVIPGroupEntries(uint32_t accountId, uint32_t guid);
static void addVIPGroupEntry(uint8_t groupId, uint32_t accountId, const std::string &groupName, bool customizable);
static void editVIPGroupEntry(uint8_t groupId, uint32_t accountId, const std::string &groupName, bool customizable);
static void removeVIPGroupEntry(uint8_t groupId, uint32_t accountId);
Expand Down
8 changes: 5 additions & 3 deletions src/io/iomapserialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,19 +241,21 @@ void IOMapSerialize::saveTile(PropWriteStream &stream, std::shared_ptr<Tile> til
return;
}

std::forward_list<std::shared_ptr<Item>> items;
std::vector<std::shared_ptr<Item>> items;
items.reserve(32);

uint16_t count = 0;
for (auto &item : *tileItems) {
if (item->getID() == ITEM_BATHTUB_FILLED_NOTMOVABLE) {
std::shared_ptr<Item> tub = Item::CreateItem(ITEM_BATHTUB_FILLED);
items.push_front(tub);
items.emplace_back(tub);
++count;
continue;
} else if (!item->isSavedToHouses()) {
continue;
}

items.push_front(item);
items.emplace_back(item);
++count;
}

Expand Down
4 changes: 2 additions & 2 deletions src/lua/functions/core/game/global_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ int GlobalFunctions::luaDoAreaCombatCondition(lua_State* L) {
if (area || areaId == 0) {
CombatParams params;
params.impactEffect = getNumber<uint16_t>(L, 5);
params.conditionList.emplace_front(condition);
params.conditionList.emplace_back(condition);
Combat::doCombatCondition(creature, getPosition(L, 2), area, params);
pushBoolean(L, true);
} else {
Expand Down Expand Up @@ -479,7 +479,7 @@ int GlobalFunctions::luaDoTargetCombatCondition(lua_State* L) {

CombatParams params;
params.impactEffect = getNumber<uint16_t>(L, 4);
params.conditionList.emplace_front(condition->clone());
params.conditionList.emplace_back(condition->clone());
Combat::doCombatCondition(creature, target, params);
pushBoolean(L, true);
return 1;
Expand Down
Loading

0 comments on commit 4920394

Please sign in to comment.