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
This commit is contained in:
Loic Blot 2016-10-05 00:13:10 +02:00 committed by Ner'zhul
parent d4c76258e3
commit 5f084cd98d
5 changed files with 38 additions and 48 deletions

@ -829,20 +829,18 @@ void push_tool_capabilities(lua_State *L,
// Create groupcaps table // Create groupcaps table
lua_newtable(L); lua_newtable(L);
// For each groupcap // For each groupcap
for(std::map<std::string, ToolGroupCap>::const_iterator for (ToolGCMap::const_iterator i = toolcap.groupcaps.begin();
i = toolcap.groupcaps.begin(); i != toolcap.groupcaps.end(); i++){ i != toolcap.groupcaps.end(); i++) {
// Create groupcap table // Create groupcap table
lua_newtable(L); lua_newtable(L);
const std::string &name = i->first; const std::string &name = i->first;
const ToolGroupCap &groupcap = i->second; const ToolGroupCap &groupcap = i->second;
// Create subtable "times" // Create subtable "times"
lua_newtable(L); lua_newtable(L);
for(std::map<int, float>::const_iterator for (UNORDERED_MAP<int, float>::const_iterator
i = groupcap.times.begin(); i != groupcap.times.end(); i++){ i = groupcap.times.begin(); i != groupcap.times.end(); i++) {
int rating = i->first; lua_pushinteger(L, i->first);
float time = i->second; lua_pushnumber(L, i->second);
lua_pushinteger(L, rating);
lua_pushnumber(L, time);
lua_settable(L, -3); lua_settable(L, -3);
} }
// Set subtable "times" // Set subtable "times"
@ -858,8 +856,8 @@ void push_tool_capabilities(lua_State *L,
//Create damage_groups table //Create damage_groups table
lua_newtable(L); lua_newtable(L);
// For each damage group // For each damage group
for(std::map<std::string, s16>::const_iterator for (DamageGroup::const_iterator i = toolcap.damageGroups.begin();
i = toolcap.damageGroups.begin(); i != toolcap.damageGroups.end(); i++){ i != toolcap.damageGroups.end(); i++) {
// Create damage group table // Create damage group table
lua_pushinteger(L, i->second); lua_pushinteger(L, i->second);
lua_setfield(L, -2, i->first.c_str()); lua_setfield(L, -2, i->first.c_str());

@ -24,7 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/string.h" #include "util/string.h"
#include "threading/mutex.h" #include "threading/mutex.h"
#include <string> #include <string>
#include <map> #include "util/cpp11_container.h"
#include <list> #include <list>
#include <set> #include <set>
@ -45,7 +45,7 @@ typedef std::vector<
> >
> SettingsCallbackList; > SettingsCallbackList;
typedef std::map<std::string, SettingsCallbackList> SettingsCallbackMap; typedef UNORDERED_MAP<std::string, SettingsCallbackList> SettingsCallbackMap;
enum ValueType { enum ValueType {
VALUETYPE_STRING, VALUETYPE_STRING,

@ -41,9 +41,9 @@ with this program; ifnot, write to the Free Software Foundation, Inc.,
#include "log.h" #include "log.h"
#include "util/numeric.h" // myrand() #include "util/numeric.h" // myrand()
#include "porting.h" #include "porting.h"
#include <map>
#include <vector> #include <vector>
#include <fstream> #include <fstream>
#include "util/cpp11_container.h"
#define BUFFER_SIZE 30000 #define BUFFER_SIZE 30000
@ -271,8 +271,8 @@ private:
ALCdevice *m_device; ALCdevice *m_device;
ALCcontext *m_context; ALCcontext *m_context;
int m_next_id; int m_next_id;
std::map<std::string, std::vector<SoundBuffer*> > m_buffers; UNORDERED_MAP<std::string, std::vector<SoundBuffer*> > m_buffers;
std::map<int, PlayingSound*> m_sounds_playing; UNORDERED_MAP<int, PlayingSound*> m_sounds_playing;
v3f m_listener_pos; v3f m_listener_pos;
public: public:
bool m_is_initialized; bool m_is_initialized;
@ -337,7 +337,7 @@ public:
alcCloseDevice(m_device); alcCloseDevice(m_device);
m_device = NULL; m_device = NULL;
for (std::map<std::string, std::vector<SoundBuffer*> >::iterator i = m_buffers.begin(); for (UNORDERED_MAP<std::string, std::vector<SoundBuffer*> >::iterator i = m_buffers.begin();
i != m_buffers.end(); ++i) { i != m_buffers.end(); ++i) {
for (std::vector<SoundBuffer*>::iterator iter = (*i).second.begin(); for (std::vector<SoundBuffer*>::iterator iter = (*i).second.begin();
iter != (*i).second.end(); ++iter) { iter != (*i).second.end(); ++iter) {
@ -351,7 +351,7 @@ public:
void addBuffer(const std::string &name, SoundBuffer *buf) void addBuffer(const std::string &name, SoundBuffer *buf)
{ {
std::map<std::string, std::vector<SoundBuffer*> >::iterator i = UNORDERED_MAP<std::string, std::vector<SoundBuffer*> >::iterator i =
m_buffers.find(name); m_buffers.find(name);
if(i != m_buffers.end()){ if(i != m_buffers.end()){
i->second.push_back(buf); i->second.push_back(buf);
@ -365,7 +365,7 @@ public:
SoundBuffer* getBuffer(const std::string &name) SoundBuffer* getBuffer(const std::string &name)
{ {
std::map<std::string, std::vector<SoundBuffer*> >::iterator i = UNORDERED_MAP<std::string, std::vector<SoundBuffer*> >::iterator i =
m_buffers.find(name); m_buffers.find(name);
if(i == m_buffers.end()) if(i == m_buffers.end())
return NULL; return NULL;
@ -443,8 +443,7 @@ public:
void deleteSound(int id) void deleteSound(int id)
{ {
std::map<int, PlayingSound*>::iterator i = UNORDERED_MAP<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
m_sounds_playing.find(id);
if(i == m_sounds_playing.end()) if(i == m_sounds_playing.end())
return; return;
PlayingSound *sound = i->second; PlayingSound *sound = i->second;
@ -484,10 +483,8 @@ public:
<<m_sounds_playing.size()<<" playing sounds, " <<m_sounds_playing.size()<<" playing sounds, "
<<m_buffers.size()<<" sound names loaded"<<std::endl; <<m_buffers.size()<<" sound names loaded"<<std::endl;
std::set<int> del_list; std::set<int> del_list;
for(std::map<int, PlayingSound*>::iterator for(UNORDERED_MAP<int, PlayingSound*>::iterator i = m_sounds_playing.begin();
i = m_sounds_playing.begin(); i != m_sounds_playing.end(); ++i) {
i != m_sounds_playing.end(); ++i)
{
int id = i->first; int id = i->first;
PlayingSound *sound = i->second; PlayingSound *sound = i->second;
// If not playing, remove it // If not playing, remove it
@ -583,9 +580,8 @@ public:
} }
void updateSoundPosition(int id, v3f pos) void updateSoundPosition(int id, v3f pos)
{ {
std::map<int, PlayingSound*>::iterator i = UNORDERED_MAP<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
m_sounds_playing.find(id); if (i == m_sounds_playing.end())
if(i == m_sounds_playing.end())
return; return;
PlayingSound *sound = i->second; PlayingSound *sound = i->second;

@ -34,24 +34,23 @@ void ToolCapabilities::serialize(std::ostream &os, u16 protocol_version) const
writeF1000(os, full_punch_interval); writeF1000(os, full_punch_interval);
writeS16(os, max_drop_level); writeS16(os, max_drop_level);
writeU32(os, groupcaps.size()); writeU32(os, groupcaps.size());
for(std::map<std::string, ToolGroupCap>::const_iterator for (ToolGCMap::const_iterator i = groupcaps.begin(); i != groupcaps.end(); ++i) {
i = groupcaps.begin(); i != groupcaps.end(); ++i){
const std::string *name = &i->first; const std::string *name = &i->first;
const ToolGroupCap *cap = &i->second; const ToolGroupCap *cap = &i->second;
os<<serializeString(*name); os<<serializeString(*name);
writeS16(os, cap->uses); writeS16(os, cap->uses);
writeS16(os, cap->maxlevel); writeS16(os, cap->maxlevel);
writeU32(os, cap->times.size()); writeU32(os, cap->times.size());
for(std::map<int, float>::const_iterator for (UNORDERED_MAP<int, float>::const_iterator
i = cap->times.begin(); i != cap->times.end(); ++i){ i = cap->times.begin(); i != cap->times.end(); ++i) {
writeS16(os, i->first); writeS16(os, i->first);
writeF1000(os, i->second); writeF1000(os, i->second);
} }
} }
if(protocol_version > 17){ if(protocol_version > 17){
writeU32(os, damageGroups.size()); writeU32(os, damageGroups.size());
for(std::map<std::string, s16>::const_iterator for (DamageGroup::const_iterator i = damageGroups.begin();
i = damageGroups.begin(); i != damageGroups.end(); ++i){ i != damageGroups.end(); ++i) {
os<<serializeString(i->first); os<<serializeString(i->first);
writeS16(os, i->second); writeS16(os, i->second);
} }
@ -106,7 +105,7 @@ DigParams getDigParams(const ItemGroupList &groups,
default: default:
break; break;
} }
// Values to be returned (with a bit of conversion) // Values to be returned (with a bit of conversion)
bool result_diggable = false; bool result_diggable = false;
float result_time = 0.0; float result_time = 0.0;
@ -115,8 +114,8 @@ DigParams getDigParams(const ItemGroupList &groups,
int level = itemgroup_get(groups, "level"); int level = itemgroup_get(groups, "level");
//infostream<<"level="<<level<<std::endl; //infostream<<"level="<<level<<std::endl;
for(std::map<std::string, ToolGroupCap>::const_iterator for (ToolGCMap::const_iterator i = tp->groupcaps.begin();
i = tp->groupcaps.begin(); i != tp->groupcaps.end(); ++i){ i != tp->groupcaps.end(); ++i) {
const std::string &name = i->first; const std::string &name = i->first;
//infostream<<"group="<<name<<std::endl; //infostream<<"group="<<name<<std::endl;
const ToolGroupCap &cap = i->second; const ToolGroupCap &cap = i->second;
@ -163,8 +162,8 @@ HitParams getHitParams(const ItemGroupList &armor_groups,
s16 damage = 0; s16 damage = 0;
float full_punch_interval = tp->full_punch_interval; float full_punch_interval = tp->full_punch_interval;
for(std::map<std::string, s16>::const_iterator for (DamageGroup::const_iterator i = tp->damageGroups.begin();
i = tp->damageGroups.begin(); i != tp->damageGroups.end(); ++i){ i != tp->damageGroups.end(); ++i) {
s16 armor = itemgroup_get(armor_groups, i->first); s16 armor = itemgroup_get(armor_groups, i->first);
damage += i->second * rangelim(time_from_last_punch / full_punch_interval, 0.0, 1.0) damage += i->second * rangelim(time_from_last_punch / full_punch_interval, 0.0, 1.0)
* armor / 100.0; * armor / 100.0;
@ -197,7 +196,7 @@ PunchDamageResult getPunchDamage(
do_hit = false; do_hit = false;
} }
} }
PunchDamageResult result; PunchDamageResult result;
if(do_hit) if(do_hit)
{ {

@ -23,12 +23,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irrlichttypes.h" #include "irrlichttypes.h"
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <map> #include "util/cpp11_container.h"
#include "itemgroup.h" #include "itemgroup.h"
struct ToolGroupCap struct ToolGroupCap
{ {
std::map<int, float> times; UNORDERED_MAP<int, float> times;
int maxlevel; int maxlevel;
int uses; int uses;
@ -39,8 +39,8 @@ struct ToolGroupCap
bool getTime(int rating, float *time) const bool getTime(int rating, float *time) const
{ {
std::map<int, float>::const_iterator i = times.find(rating); UNORDERED_MAP<int, float>::const_iterator i = times.find(rating);
if(i == times.end()){ if (i == times.end()) {
*time = 0; *time = 0;
return false; return false;
} }
@ -50,22 +50,19 @@ struct ToolGroupCap
}; };
// CLANG SUCKS DONKEY BALLS typedef UNORDERED_MAP<std::string, struct ToolGroupCap> ToolGCMap;
typedef std::map<std::string, struct ToolGroupCap> ToolGCMap; typedef UNORDERED_MAP<std::string, s16> DamageGroup;
typedef std::map<std::string, s16> DamageGroup;
struct ToolCapabilities struct ToolCapabilities
{ {
float full_punch_interval; float full_punch_interval;
int max_drop_level; int max_drop_level;
// CLANG SUCKS DONKEY BALLS
ToolGCMap groupcaps; ToolGCMap groupcaps;
DamageGroup damageGroups; DamageGroup damageGroups;
ToolCapabilities( ToolCapabilities(
float full_punch_interval_=1.4, float full_punch_interval_=1.4,
int max_drop_level_=1, int max_drop_level_=1,
// CLANG SUCKS DONKEY BALLS
ToolGCMap groupcaps_=ToolGCMap(), ToolGCMap groupcaps_=ToolGCMap(),
DamageGroup damageGroups_=DamageGroup() DamageGroup damageGroups_=DamageGroup()
): ):