Skip to content

Commit

Permalink
[Enhancement] Replace all std::unordered with phmap::flat_hash (#420)
Browse files Browse the repository at this point in the history
Parallel HashMap is faster and more optimized
Documentation: https://greg7mdp.github.io/parallel-hashmap/

This fixes a crash on ubuntu 22.04 when imbuement duration end
  • Loading branch information
beats-dh committed Jul 28, 2022
1 parent 26b1b40 commit 5a6da6c
Show file tree
Hide file tree
Showing 24 changed files with 56 additions and 52 deletions.
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ if (MSVC)
${Boost_INCLUDE_DIRS}
${PUGIXML_INCLUDE_DIR}
${CURL_INCLUDE_DIRS}
${PARALLEL_HASHMAP_INCLUDE_DIRS}
${Protobuf_INCLUDE_DIRS}
${SPDLOG_INCLUDE_DIR}
${GMP_INCLUDE_DIR}
Expand Down Expand Up @@ -335,6 +336,7 @@ else()
PRIVATE
${CMAKE_SOURCE_DIR}/src
${LUAJIT_INCLUDE_DIRS}
${PARALLEL_HASHMAP_INCLUDE_DIRS}
${Protobuf_INCLUDE_DIRS}
${GMP_INCLUDE_DIRS}
)
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/monsters/monster.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Creature;
class Game;
class Spawn;

using CreatureHashSet = std::unordered_set<Creature*>;
using CreatureHashSet = phmap::flat_hash_set<Creature*>;
using CreatureList = std::list<Creature*>;

class Monster final : public Creature
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/monsters/monsters.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class MonsterType
bool targetPreferMaster = false;

Faction_t faction = FACTION_DEFAULT;
std::unordered_set<Faction_t> enemyFactions;
phmap::flat_hash_set<Faction_t> enemyFactions;

bool canPushItems = false;
bool canPushCreatures = false;
Expand Down
8 changes: 4 additions & 4 deletions src/creatures/players/grouping/groups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "utils/pugicast.h"
#include "utils/tools.h"

const std::unordered_map<std::string, PlayerFlags> ParsePlayerFlagMap = {
const phmap::flat_hash_map<std::string, PlayerFlags> ParsePlayerFlagMap = {
{"cannotusecombat", PlayerFlag_CannotUseCombat},
{"cannotattackplayer", PlayerFlag_CannotAttackPlayer},
{"cannotattackmonster", PlayerFlag_CannotAttackMonster},
Expand Down Expand Up @@ -65,9 +65,9 @@ const std::unordered_map<std::string, PlayerFlags> ParsePlayerFlagMap = {
{"isalwayspremium", PlayerFlag_IsAlwaysPremium}
};

const std::unordered_map<std::string, PlayerCustomFlags> ParsePlayerCustomFlagMap = {
{"canmapclickteleport", PlayerCustomFlag_CanMapClickTeleport},
{"ignoredbynpcs", PlayerCustomFlag_IgnoredByNpcs}
const phmap::flat_hash_map<std::string, PlayerCustomFlags> ParsePlayerCustomFlagMap = {
{"canmapclickteleport", PlayerCustomFlag_CanMapClickTeleport},
{"ignoredbynpcs", PlayerCustomFlag_IgnoredByNpcs}
};

bool Groups::load()
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5456,7 +5456,7 @@ uint16_t Player::getHelpers() const
uint16_t helpers;

if (guild && party) {
std::unordered_set<Player*> helperSet;
phmap::flat_hash_set<Player*> helperSet;

const auto& guildMembers = guild->getMembersOnline();
helperSet.insert(guildMembers.begin(), guildMembers.end());
Expand Down
4 changes: 2 additions & 2 deletions src/creatures/players/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -2154,9 +2154,9 @@ class Player final : public Creature, public Cylinder
void internalAddThing(Thing* thing) override;
void internalAddThing(uint32_t index, Thing* thing) override;

std::unordered_set<uint32_t> attackedSet;
phmap::flat_hash_set<uint32_t> attackedSet;

std::unordered_set<uint32_t> VIPList;
phmap::flat_hash_set<uint32_t> VIPList;

std::map<uint8_t, OpenContainer> openContainers;
std::map<uint32_t, DepotLocker*> depotLockerMap;
Expand Down
18 changes: 9 additions & 9 deletions src/game/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ class Game
void sendOfflineTrainingDialog(Player* player);

const std::map<uint16_t, uint32_t>& getItemsPrice() const { return itemsPriceMap; }
const std::unordered_map<uint32_t, Player*>& getPlayers() const { return players; }
const phmap::flat_hash_map<uint32_t, Player*>& getPlayers() const { return players; }
const std::map<uint32_t, Npc*>& getNpcs() const { return npcs; }

const std::vector<ItemClassification*>& getItemsClassifications() const { return itemsClassifications; }
Expand All @@ -490,7 +490,7 @@ class Game
void removeGuild(uint32_t guildId);
void decreaseBrowseFieldRef(const Position& pos);

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

void internalRemoveItems(std::vector<Item*> itemList, uint32_t amount, bool stackable);

Expand All @@ -514,7 +514,7 @@ class Game
GameStore gameStore;
Canary::protobuf::appearances::Appearances appearances;

std::unordered_set<Tile*> getTilesToClean() const {
phmap::flat_hash_set<Tile*> getTilesToClean() const {
return tilesToClean;
}
void addTileToClean(Tile* tile) {
Expand Down Expand Up @@ -586,11 +586,11 @@ class Game
bool playerSpeakTo(Player* player, SpeakClasses type, const std::string& receiver, const std::string& text);
void playerSpeakToNpc(Player* player, const std::string& text);

std::unordered_map<uint32_t, Player*> players;
std::unordered_map<uint32_t, uint8_t> playersActiveImbuements;
std::unordered_map<std::string, Player*> mappedPlayerNames;
std::unordered_map<uint32_t, Guild*> guilds;
std::unordered_map<uint16_t, Item*> uniqueItems;
phmap::flat_hash_map<uint32_t, Player*> players;
phmap::flat_hash_map<uint32_t, uint8_t> playersActiveImbuements;
phmap::flat_hash_map<std::string, Player*> mappedPlayerNames;
phmap::flat_hash_map<uint32_t, Guild*> guilds;
phmap::flat_hash_map<uint16_t, Item*> uniqueItems;
std::map<uint32_t, uint32_t> stages;

/* Items stored from the lua scripts positions
Expand Down Expand Up @@ -626,7 +626,7 @@ class Game

std::map<uint32_t, BedItem*> bedSleepersMap;

std::unordered_set<Tile*> tilesToClean;
phmap::flat_hash_set<Tile*> tilesToClean;

ModalWindow offlineTrainingWindow { std::numeric_limits<uint32_t>::max(), "Choose a Skill", "Please choose a skill:" };

Expand Down
2 changes: 1 addition & 1 deletion src/game/scheduling/scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class Scheduler : public ThreadHolder<Scheduler>

uint32_t lastEventId {0};
std::priority_queue<SchedulerTask*, std::deque<SchedulerTask*>, TaskComparator> eventList;
std::unordered_set<uint32_t> eventIds;
phmap::flat_hash_set<uint32_t> eventIds;
};

constexpr auto g_scheduler = &Scheduler::getInstance;
Expand Down
12 changes: 6 additions & 6 deletions src/items/functions/item_parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

class ConditionDamage;

const std::unordered_map<std::string, ItemParseAttributes_t> ItemParseAttributesMap = {
const phmap::flat_hash_map<std::string, ItemParseAttributes_t> ItemParseAttributesMap = {
{"type", ITEM_PARSE_TYPE},
{"description", ITEM_PARSE_DESCRIPTION},
{"runespellname", ITEM_PARSE_RUNESPELLNAME},
Expand Down Expand Up @@ -149,7 +149,7 @@ const std::unordered_map<std::string, ItemParseAttributes_t> ItemParseAttributes
{"imbuementslot", ITEM_PARSE_IMBUEMENT},
};

const std::unordered_map<std::string, ItemTypes_t> ItemTypesMap = {
const phmap::flat_hash_map<std::string, ItemTypes_t> ItemTypesMap = {
{"key", ITEM_TYPE_KEY},
{"magicfield", ITEM_TYPE_MAGICFIELD},
{"container", ITEM_TYPE_CONTAINER},
Expand All @@ -169,7 +169,7 @@ const std::unordered_map<std::string, ItemTypes_t> ItemTypesMap = {
{"potion", ITEM_TYPE_POTION},
};

const std::unordered_map<std::string, TileFlags_t> TileStatesMap = {
const phmap::flat_hash_map<std::string, TileFlags_t> TileStatesMap = {
{"down", TILESTATE_FLOORCHANGE_DOWN},
{"north", TILESTATE_FLOORCHANGE_NORTH},
{"south", TILESTATE_FLOORCHANGE_SOUTH},
Expand All @@ -179,7 +179,7 @@ const std::unordered_map<std::string, TileFlags_t> TileStatesMap = {
{"eastalt", TILESTATE_FLOORCHANGE_EAST_ALT},
};

const std::unordered_map<std::string, Fluids_t> FluidTypesMap = {
const phmap::flat_hash_map<std::string, Fluids_t> FluidTypesMap = {
{"water", FLUID_WATER },
{"blood", FLUID_BLOOD},
{"beer", FLUID_BEER},
Expand All @@ -200,7 +200,7 @@ const std::unordered_map<std::string, Fluids_t> FluidTypesMap = {
{"ink", FLUID_INK },
};

const std::unordered_map<std::string, WeaponType_t> WeaponTypesMap = {
const phmap::flat_hash_map<std::string, WeaponType_t> WeaponTypesMap = {
{"sword", WEAPON_SWORD},
{"club", WEAPON_CLUB},
{"axe", WEAPON_AXE},
Expand All @@ -210,7 +210,7 @@ const std::unordered_map<std::string, WeaponType_t> WeaponTypesMap = {
{"ammunition", WEAPON_AMMO}
};

const std::unordered_map<std::string, ImbuementTypes_t> ImbuementsTypeMap = {
const phmap::flat_hash_map<std::string, ImbuementTypes_t> ImbuementsTypeMap = {
{"elemental damage", IMBUEMENT_ELEMENTAL_DAMAGE},
{"life leech", IMBUEMENT_LIFE_LEECH},
{"mana leech", IMBUEMENT_MANA_LEECH},
Expand Down
2 changes: 1 addition & 1 deletion src/items/item.h
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ class ItemAttributes
static double emptyDouble;
static bool emptyBool;

typedef std::unordered_map<std::string, CustomAttribute> CustomAttributeMap;
typedef phmap::flat_hash_map<std::string, CustomAttribute> CustomAttributeMap;

struct Attribute {
union {
Expand Down
2 changes: 1 addition & 1 deletion src/items/items.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void Items::clear()
nameToItems.clear();
}

using LootTypeNames = std::unordered_map<std::string, ItemTypes_t>;
using LootTypeNames = phmap::flat_hash_map<std::string, ItemTypes_t>;

LootTypeNames lootTypeNames = {
{"armor", ITEM_TYPE_ARMOR},
Expand Down
2 changes: 1 addition & 1 deletion src/items/tile.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class BedItem;

using CreatureVector = std::vector<Creature*>;
using ItemVector = std::vector<Item*>;
using SpectatorHashSet = std::unordered_set<Creature*>;
using SpectatorHashSet = phmap::flat_hash_set<Creature*>;

class TileItemVector : private ItemVector
{
Expand Down
10 changes: 5 additions & 5 deletions src/lua/scripts/lua_environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ class LuaEnvironment: public LuaScriptInterface {
private:
void executeTimerEvent(uint32_t eventIndex);

std::unordered_map < uint32_t,
phmap::flat_hash_map < uint32_t,
LuaTimerEventDesc > timerEvents;
std::unordered_map < uint32_t,
phmap::flat_hash_map < uint32_t,
Combat * > combatMap;
std::unordered_map < uint32_t,
phmap::flat_hash_map < uint32_t,
AreaCombat * > areaMap;

std::unordered_map < LuaScriptInterface * ,
phmap::flat_hash_map < LuaScriptInterface * ,
std::vector < uint32_t >> combatIdMap;
std::unordered_map < LuaScriptInterface * ,
phmap::flat_hash_map < LuaScriptInterface * ,
std::vector < uint32_t >> areaIdMap;

LuaScriptInterface * testInterface = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/lua/scripts/script_environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class ScriptEnvironment {
static std::multimap < ScriptEnvironment * , Item * > tempItems;

// local item map
std::unordered_map < uint32_t, Item * > localMap;
phmap::flat_hash_map < uint32_t, Item * > localMap;
uint32_t lastUID = std::numeric_limits < uint16_t > ::max();

// script file id
Expand Down
4 changes: 2 additions & 2 deletions src/map/house/house.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class AccessList

private:
std::string list;
std::unordered_set<uint32_t> playerList;
std::unordered_set<uint32_t> guildRankList;
phmap::flat_hash_set<uint32_t> playerList;
phmap::flat_hash_set<uint32_t> guildRankList;
bool allowEveryone = false;
};

Expand Down
2 changes: 1 addition & 1 deletion src/map/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class AStarNodes
private:
AStarNode nodes[MAX_NODES];
bool openNodes[MAX_NODES];
std::unordered_map<uint32_t, AStarNode*> nodeTable;
phmap::flat_hash_map<uint32_t, AStarNode*> nodeTable;
size_t curNode;
int_fast32_t closedNodes;
};
Expand Down
1 change: 1 addition & 0 deletions src/otpch.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,6 @@
#include <pugixml.hpp>

#include "spdlog/spdlog.h"
#include "parallel_hashmap/phmap.h"

#endif // SRC_OTPCH_H_
2 changes: 1 addition & 1 deletion src/server/network/connection/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ConnectionManager
private:
ConnectionManager() = default;

std::unordered_set<Connection_ptr> connections;
phmap::flat_hash_set<Connection_ptr> connections;
std::mutex connectionManagerLock;
};

Expand Down
4 changes: 2 additions & 2 deletions src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6712,7 +6712,7 @@ void ProtocolGame::openImbuementWindow(Item *item)
}

std::vector<Imbuement *> imbuements = g_imbuements().getImbuements(player, item);
std::unordered_map<uint16_t, uint16_t> needItems;
phmap::flat_hash_map<uint16_t, uint16_t> needItems;

msg.add<uint16_t>(imbuements.size());
for (const Imbuement *imbuement : imbuements)
Expand Down Expand Up @@ -7173,7 +7173,7 @@ void ProtocolGame::reloadCreature(const Creature *creature)

NetworkMessage msg;

std::unordered_set<uint32_t>::iterator it = std::find(knownCreatureSet.begin(), knownCreatureSet.end(), creature->getID());
phmap::flat_hash_set<uint32_t>::iterator it = std::find(knownCreatureSet.begin(), knownCreatureSet.end(), creature->getID());
if (it != knownCreatureSet.end())
{
msg.addByte(0x6B);
Expand Down
2 changes: 1 addition & 1 deletion src/server/network/protocol/protocolgame.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ class ProtocolGame final : public Protocol

friend class Player;

std::unordered_set<uint32_t> knownCreatureSet;
phmap::flat_hash_set<uint32_t> knownCreatureSet;
Player *player = nullptr;

uint32_t eventConnect = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class ServiceManager
private:
void die();

std::unordered_map<uint16_t, ServicePort_ptr> acceptors;
phmap::flat_hash_map<uint16_t, ServicePort_ptr> acceptors;

boost::asio::io_service io_service;
Signals signals{io_service};
Expand Down
16 changes: 8 additions & 8 deletions src/utils/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,19 +503,19 @@ Direction getDirectionTo(const Position& from, const Position& to)
return dir;
}

using MagicEffectNames = std::unordered_map<std::string, MagicEffectClasses>;
using ShootTypeNames = std::unordered_map<std::string, ShootType_t>;
using CombatTypeNames = std::unordered_map<CombatType_t, std::string, std::hash<int32_t>>;
using AmmoTypeNames = std::unordered_map<std::string, Ammo_t>;
using WeaponActionNames = std::unordered_map<std::string, WeaponAction_t>;
using SkullNames = std::unordered_map<std::string, Skulls_t>;
using ImbuementTypeNames = std::unordered_map<std::string, ImbuementTypes_t>;
using MagicEffectNames = phmap::flat_hash_map<std::string, MagicEffectClasses>;
using ShootTypeNames = phmap::flat_hash_map<std::string, ShootType_t>;
using CombatTypeNames = phmap::flat_hash_map<CombatType_t, std::string, std::hash<int32_t>>;
using AmmoTypeNames = phmap::flat_hash_map<std::string, Ammo_t>;
using WeaponActionNames = phmap::flat_hash_map<std::string, WeaponAction_t>;
using SkullNames = phmap::flat_hash_map<std::string, Skulls_t>;
using ImbuementTypeNames = phmap::flat_hash_map<std::string, ImbuementTypes_t>;

/**
* @Deprecated
* It will be dropped with monsters. Use RespawnPeriod_t instead.
*/
using SpawnTypeNames = std::unordered_map<std::string, SpawnType_t>;
using SpawnTypeNames = phmap::flat_hash_map<std::string, SpawnType_t>;

MagicEffectNames magicEffectNames = {
{"assassin", CONST_ME_ASSASSIN},
Expand Down
4 changes: 2 additions & 2 deletions src/utils/utils_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ enum item_t : uint16_t {
};

// A map which contains items that, when on creating, should be transformed to the default type.
const std::unordered_map<item_t, item_t> ItemTransformationMap = {
const phmap::flat_hash_map<item_t, item_t> ItemTransformationMap = {
{ITEM_SWORD_RING_ACTIVATED, ITEM_SWORD_RING},
{ITEM_CLUB_RING_ACTIVATED, ITEM_CLUB_RING},
{ITEM_DWARVEN_RING_ACTIVATED, ITEM_DWARVEN_RING},
Expand Down Expand Up @@ -753,7 +753,7 @@ enum Blessings_t : uint8_t {
HEARTH_OF_THE_MOUNTAIN = 8,
};

const std::unordered_map<Blessings_t, std::string> BlessingNames = {
const phmap::flat_hash_map<Blessings_t, std::string> BlessingNames = {
{TWIST_OF_FATE, "Twist of Fate"},
{WISDOM_OF_SOLITUDE, "The Wisdom of Solitude"},
{SPARK_OF_THE_PHOENIX, "The Spark of the Phoenix"},
Expand Down
1 change: 1 addition & 0 deletions vcpkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"curl",
"jsoncpp",
"protobuf",
"parallel-hashmap",
{
"name": "luajit",
"platform": "!arm"
Expand Down

0 comments on commit 5a6da6c

Please sign in to comment.