Skip to content

Commit

Permalink
fix: from flat hash map to unordered map
Browse files Browse the repository at this point in the history
Iterating with "for" range based on phmap::flat_hash_map is very expensive.
  • Loading branch information
dudantas committed Nov 7, 2024
1 parent 361c4a2 commit d0ef23f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/account/account.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ void Account::updatePremiumTime() {
}
}

std::tuple<phmap::flat_hash_map<std::string, uint64_t>, AccountErrors_t>
std::tuple<std::unordered_map<std::string, uint64_t>, AccountErrors_t>
Account::getAccountPlayers() const {
using enum AccountErrors_t;
auto valueToReturn = m_accLoaded ? Ok : NotInitialized;
Expand Down
2 changes: 1 addition & 1 deletion src/account/account.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class Account {

void updatePremiumTime();

std::tuple<phmap::flat_hash_map<std::string, uint64_t>, AccountErrors_t> getAccountPlayers() const;
std::tuple<std::unordered_map<std::string, uint64_t>, AccountErrors_t> getAccountPlayers() const;

// Old protocol compat
void setProtocolCompat(bool toggle);
Expand Down
3 changes: 1 addition & 2 deletions src/account/account_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#pragma once

#ifndef USE_PRECOMPILED_HEADERS
#include <parallel_hashmap/phmap.h>
#include <cstdint>
#endif

Expand All @@ -23,7 +22,7 @@ struct AccountInfo {
uint32_t premiumRemainingDays = 0;
time_t premiumLastDay = 0;
AccountType accountType = ACCOUNT_TYPE_NONE;
phmap::flat_hash_map<std::string, uint64_t> players;
std::unordered_map<std::string, uint64_t> players;
bool oldProtocol = false;
time_t sessionExpires = 0;
uint32_t premiumDaysPurchased = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/npcs/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ void Npc::onPlayerSellAllLoot(uint32_t playerId, uint16_t itemId, bool ignore, u
}
bool hasMore = false;
uint64_t toSellCount = 0;
phmap::flat_hash_map<uint16_t, uint16_t> toSell;
std::unordered_map<uint16_t, uint16_t> toSell;
for (ContainerIterator it = container->iterator(); it.hasNext(); it.advance()) {
if (toSellCount >= 500) {
hasMore = true;
Expand Down
22 changes: 11 additions & 11 deletions src/game/game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -509,10 +509,10 @@ class Game {
const std::map<uint16_t, std::map<uint8_t, uint64_t>> &getItemsPrice() const {
return itemsPriceMap;
}
const phmap::parallel_flat_hash_map<uint32_t, std::shared_ptr<Guild>> &getGuilds() const {
const std::unordered_map<uint32_t, std::shared_ptr<Guild>> &getGuilds() const {
return guilds;
}
const phmap::parallel_flat_hash_map<uint32_t, std::shared_ptr<Player>> &getPlayers() const {
const std::unordered_map<uint32_t, std::shared_ptr<Player>> &getPlayers() const {
return players;
}
const std::map<uint32_t, std::shared_ptr<Monster>> &getMonsters() const {
Expand Down Expand Up @@ -540,7 +540,7 @@ class Game {
void addGuild(const std::shared_ptr<Guild> &guild);
void removeGuild(uint32_t guildId);

phmap::flat_hash_map<std::shared_ptr<Tile>, std::weak_ptr<Container>> browseFields;
std::unordered_map<std::shared_ptr<Tile>, std::weak_ptr<Container>> browseFields;

void internalRemoveItems(const std::vector<std::shared_ptr<Item>> &itemVector, uint32_t amount, bool stackable);

Expand Down Expand Up @@ -814,15 +814,15 @@ class Game {
*/
ReturnValue collectRewardChestItems(const std::shared_ptr<Player> &player, uint32_t maxMoveItems = 0);

phmap::flat_hash_map<std::string, QueryHighscoreCacheEntry> queryCache;
phmap::flat_hash_map<std::string, HighscoreCacheEntry> highscoreCache;
std::unordered_map<std::string, QueryHighscoreCacheEntry> queryCache;
std::unordered_map<std::string, HighscoreCacheEntry> highscoreCache;

phmap::flat_hash_map<std::string, std::weak_ptr<Player>> m_uniqueLoginPlayerNames;
phmap::parallel_flat_hash_map<uint32_t, std::shared_ptr<Player>> players;
phmap::flat_hash_map<std::string, std::weak_ptr<Player>> mappedPlayerNames;
phmap::parallel_flat_hash_map<uint32_t, std::shared_ptr<Guild>> guilds;
phmap::flat_hash_map<uint16_t, std::shared_ptr<Item>> uniqueItems;
phmap::parallel_flat_hash_map<uint32_t, std::string> m_playerNameCache;
std::unordered_map<std::string, std::weak_ptr<Player>> m_uniqueLoginPlayerNames;
std::unordered_map<uint32_t, std::shared_ptr<Player>> players;
std::unordered_map<std::string, std::weak_ptr<Player>> mappedPlayerNames;
std::unordered_map<uint32_t, std::shared_ptr<Guild>> guilds;
std::unordered_map<uint16_t, std::shared_ptr<Item>> uniqueItems;
std::unordered_map<uint32_t, std::string> m_playerNameCache;

/* Items stored from the lua scripts positions
* For example: ActionFunctions::luaActionPosition
Expand Down

0 comments on commit d0ef23f

Please sign in to comment.