From 5f084cd98d7b3326b51320455364337539710efd Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Wed, 5 Oct 2016 00:13:10 +0200 Subject: [PATCH] Make some maps unordered to improve performance * This permit to improve performance on C++11 builds * use some existing typedefs in tools maps * minor code style changes --- src/script/common/c_content.cpp | 18 ++++++++---------- src/settings.h | 4 ++-- src/sound_openal.cpp | 26 +++++++++++--------------- src/tool.cpp | 23 +++++++++++------------ src/tool.h | 15 ++++++--------- 5 files changed, 38 insertions(+), 48 deletions(-) diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp index 19873abc5..6fb9080bc 100644 --- a/src/script/common/c_content.cpp +++ b/src/script/common/c_content.cpp @@ -829,20 +829,18 @@ void push_tool_capabilities(lua_State *L, // Create groupcaps table lua_newtable(L); // For each groupcap - for(std::map::const_iterator - i = toolcap.groupcaps.begin(); i != toolcap.groupcaps.end(); i++){ + for (ToolGCMap::const_iterator i = toolcap.groupcaps.begin(); + i != toolcap.groupcaps.end(); i++) { // Create groupcap table lua_newtable(L); const std::string &name = i->first; const ToolGroupCap &groupcap = i->second; // Create subtable "times" lua_newtable(L); - for(std::map::const_iterator - i = groupcap.times.begin(); i != groupcap.times.end(); i++){ - int rating = i->first; - float time = i->second; - lua_pushinteger(L, rating); - lua_pushnumber(L, time); + for (UNORDERED_MAP::const_iterator + i = groupcap.times.begin(); i != groupcap.times.end(); i++) { + lua_pushinteger(L, i->first); + lua_pushnumber(L, i->second); lua_settable(L, -3); } // Set subtable "times" @@ -858,8 +856,8 @@ void push_tool_capabilities(lua_State *L, //Create damage_groups table lua_newtable(L); // For each damage group - for(std::map::const_iterator - i = toolcap.damageGroups.begin(); i != toolcap.damageGroups.end(); i++){ + for (DamageGroup::const_iterator i = toolcap.damageGroups.begin(); + i != toolcap.damageGroups.end(); i++) { // Create damage group table lua_pushinteger(L, i->second); lua_setfield(L, -2, i->first.c_str()); diff --git a/src/settings.h b/src/settings.h index 0af861a58..c6c044779 100644 --- a/src/settings.h +++ b/src/settings.h @@ -24,7 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/string.h" #include "threading/mutex.h" #include -#include +#include "util/cpp11_container.h" #include #include @@ -45,7 +45,7 @@ typedef std::vector< > > SettingsCallbackList; -typedef std::map SettingsCallbackMap; +typedef UNORDERED_MAP SettingsCallbackMap; enum ValueType { VALUETYPE_STRING, diff --git a/src/sound_openal.cpp b/src/sound_openal.cpp index 1832a0c77..317667f52 100644 --- a/src/sound_openal.cpp +++ b/src/sound_openal.cpp @@ -41,9 +41,9 @@ with this program; ifnot, write to the Free Software Foundation, Inc., #include "log.h" #include "util/numeric.h" // myrand() #include "porting.h" -#include #include #include +#include "util/cpp11_container.h" #define BUFFER_SIZE 30000 @@ -271,8 +271,8 @@ private: ALCdevice *m_device; ALCcontext *m_context; int m_next_id; - std::map > m_buffers; - std::map m_sounds_playing; + UNORDERED_MAP > m_buffers; + UNORDERED_MAP m_sounds_playing; v3f m_listener_pos; public: bool m_is_initialized; @@ -337,7 +337,7 @@ public: alcCloseDevice(m_device); m_device = NULL; - for (std::map >::iterator i = m_buffers.begin(); + for (UNORDERED_MAP >::iterator i = m_buffers.begin(); i != m_buffers.end(); ++i) { for (std::vector::iterator iter = (*i).second.begin(); iter != (*i).second.end(); ++iter) { @@ -351,7 +351,7 @@ public: void addBuffer(const std::string &name, SoundBuffer *buf) { - std::map >::iterator i = + UNORDERED_MAP >::iterator i = m_buffers.find(name); if(i != m_buffers.end()){ i->second.push_back(buf); @@ -365,7 +365,7 @@ public: SoundBuffer* getBuffer(const std::string &name) { - std::map >::iterator i = + UNORDERED_MAP >::iterator i = m_buffers.find(name); if(i == m_buffers.end()) return NULL; @@ -443,8 +443,7 @@ public: void deleteSound(int id) { - std::map::iterator i = - m_sounds_playing.find(id); + UNORDERED_MAP::iterator i = m_sounds_playing.find(id); if(i == m_sounds_playing.end()) return; PlayingSound *sound = i->second; @@ -484,10 +483,8 @@ public: < del_list; - for(std::map::iterator - i = m_sounds_playing.begin(); - i != m_sounds_playing.end(); ++i) - { + for(UNORDERED_MAP::iterator i = m_sounds_playing.begin(); + i != m_sounds_playing.end(); ++i) { int id = i->first; PlayingSound *sound = i->second; // If not playing, remove it @@ -583,9 +580,8 @@ public: } void updateSoundPosition(int id, v3f pos) { - std::map::iterator i = - m_sounds_playing.find(id); - if(i == m_sounds_playing.end()) + UNORDERED_MAP::iterator i = m_sounds_playing.find(id); + if (i == m_sounds_playing.end()) return; PlayingSound *sound = i->second; diff --git a/src/tool.cpp b/src/tool.cpp index 54b9f15f4..20b71fb31 100644 --- a/src/tool.cpp +++ b/src/tool.cpp @@ -34,24 +34,23 @@ void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const writeF1000(os, full_punch_interval); writeS16(os, max_drop_level); writeU32(os, groupcaps.size()); - for(std::map::const_iterator - i = groupcaps.begin(); i != groupcaps.end(); ++i){ + for (ToolGCMap::const_iterator i = groupcaps.begin(); i != groupcaps.end(); ++i) { const std::string *name = &i->first; const ToolGroupCap *cap = &i->second; os<uses); writeS16(os, cap->maxlevel); writeU32(os, cap->times.size()); - for(std::map::const_iterator - i = cap->times.begin(); i != cap->times.end(); ++i){ + for (UNORDERED_MAP::const_iterator + i = cap->times.begin(); i != cap->times.end(); ++i) { writeS16(os, i->first); writeF1000(os, i->second); } } if(protocol_version > 17){ writeU32(os, damageGroups.size()); - for(std::map::const_iterator - i = damageGroups.begin(); i != damageGroups.end(); ++i){ + for (DamageGroup::const_iterator i = damageGroups.begin(); + i != damageGroups.end(); ++i) { os<first); writeS16(os, i->second); } @@ -106,7 +105,7 @@ DigParams getDigParams(const ItemGroupList &groups, default: break; } - + // Values to be returned (with a bit of conversion) bool result_diggable = false; float result_time = 0.0; @@ -115,8 +114,8 @@ DigParams getDigParams(const ItemGroupList &groups, int level = itemgroup_get(groups, "level"); //infostream<<"level="<::const_iterator - i = tp->groupcaps.begin(); i != tp->groupcaps.end(); ++i){ + for (ToolGCMap::const_iterator i = tp->groupcaps.begin(); + i != tp->groupcaps.end(); ++i) { const std::string &name = i->first; //infostream<<"group="<second; @@ -163,8 +162,8 @@ HitParams getHitParams(const ItemGroupList &armor_groups, s16 damage = 0; float full_punch_interval = tp->full_punch_interval; - for(std::map::const_iterator - i = tp->damageGroups.begin(); i != tp->damageGroups.end(); ++i){ + for (DamageGroup::const_iterator i = tp->damageGroups.begin(); + i != tp->damageGroups.end(); ++i) { s16 armor = itemgroup_get(armor_groups, i->first); damage += i->second * rangelim(time_from_last_punch / full_punch_interval, 0.0, 1.0) * armor / 100.0; @@ -197,7 +196,7 @@ PunchDamageResult getPunchDamage( do_hit = false; } } - + PunchDamageResult result; if(do_hit) { diff --git a/src/tool.h b/src/tool.h index 509561a16..ebba5b749 100644 --- a/src/tool.h +++ b/src/tool.h @@ -23,12 +23,12 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "irrlichttypes.h" #include #include -#include +#include "util/cpp11_container.h" #include "itemgroup.h" struct ToolGroupCap { - std::map times; + UNORDERED_MAP times; int maxlevel; int uses; @@ -39,8 +39,8 @@ struct ToolGroupCap bool getTime(int rating, float *time) const { - std::map::const_iterator i = times.find(rating); - if(i == times.end()){ + UNORDERED_MAP::const_iterator i = times.find(rating); + if (i == times.end()) { *time = 0; return false; } @@ -50,22 +50,19 @@ struct ToolGroupCap }; -// CLANG SUCKS DONKEY BALLS -typedef std::map ToolGCMap; -typedef std::map DamageGroup; +typedef UNORDERED_MAP ToolGCMap; +typedef UNORDERED_MAP DamageGroup; struct ToolCapabilities { float full_punch_interval; int max_drop_level; - // CLANG SUCKS DONKEY BALLS ToolGCMap groupcaps; DamageGroup damageGroups; ToolCapabilities( float full_punch_interval_=1.4, int max_drop_level_=1, - // CLANG SUCKS DONKEY BALLS ToolGCMap groupcaps_=ToolGCMap(), DamageGroup damageGroups_=DamageGroup() ):