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
lua_newtable(L);
// For each groupcap
for(std::map<std::string, ToolGroupCap>::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<int, float>::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<int, float>::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<std::string, s16>::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());

@ -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 <string>
#include <map>
#include "util/cpp11_container.h"
#include <list>
#include <set>
@ -45,7 +45,7 @@ typedef std::vector<
>
> SettingsCallbackList;
typedef std::map<std::string, SettingsCallbackList> SettingsCallbackMap;
typedef UNORDERED_MAP<std::string, SettingsCallbackList> SettingsCallbackMap;
enum ValueType {
VALUETYPE_STRING,

@ -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 <map>
#include <vector>
#include <fstream>
#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<std::string, std::vector<SoundBuffer*> > m_buffers;
std::map<int, PlayingSound*> m_sounds_playing;
UNORDERED_MAP<std::string, std::vector<SoundBuffer*> > m_buffers;
UNORDERED_MAP<int, PlayingSound*> 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<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) {
for (std::vector<SoundBuffer*>::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<std::string, std::vector<SoundBuffer*> >::iterator i =
UNORDERED_MAP<std::string, std::vector<SoundBuffer*> >::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<std::string, std::vector<SoundBuffer*> >::iterator i =
UNORDERED_MAP<std::string, std::vector<SoundBuffer*> >::iterator i =
m_buffers.find(name);
if(i == m_buffers.end())
return NULL;
@ -443,8 +443,7 @@ public:
void deleteSound(int id)
{
std::map<int, PlayingSound*>::iterator i =
m_sounds_playing.find(id);
UNORDERED_MAP<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
if(i == m_sounds_playing.end())
return;
PlayingSound *sound = i->second;
@ -484,10 +483,8 @@ public:
<<m_sounds_playing.size()<<" playing sounds, "
<<m_buffers.size()<<" sound names loaded"<<std::endl;
std::set<int> del_list;
for(std::map<int, PlayingSound*>::iterator
i = m_sounds_playing.begin();
i != m_sounds_playing.end(); ++i)
{
for(UNORDERED_MAP<int, PlayingSound*>::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<int, PlayingSound*>::iterator i =
m_sounds_playing.find(id);
if(i == m_sounds_playing.end())
UNORDERED_MAP<int, PlayingSound*>::iterator i = m_sounds_playing.find(id);
if (i == m_sounds_playing.end())
return;
PlayingSound *sound = i->second;

@ -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<std::string, ToolGroupCap>::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<<serializeString(*name);
writeS16(os, cap->uses);
writeS16(os, cap->maxlevel);
writeU32(os, cap->times.size());
for(std::map<int, float>::const_iterator
i = cap->times.begin(); i != cap->times.end(); ++i){
for (UNORDERED_MAP<int, float>::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<std::string, s16>::const_iterator
i = damageGroups.begin(); i != damageGroups.end(); ++i){
for (DamageGroup::const_iterator i = damageGroups.begin();
i != damageGroups.end(); ++i) {
os<<serializeString(i->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="<<level<<std::endl;
for(std::map<std::string, ToolGroupCap>::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="<<name<<std::endl;
const ToolGroupCap &cap = i->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<std::string, s16>::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)
{

@ -23,12 +23,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irrlichttypes.h"
#include <string>
#include <iostream>
#include <map>
#include "util/cpp11_container.h"
#include "itemgroup.h"
struct ToolGroupCap
{
std::map<int, float> times;
UNORDERED_MAP<int, float> times;
int maxlevel;
int uses;
@ -39,8 +39,8 @@ struct ToolGroupCap
bool getTime(int rating, float *time) const
{
std::map<int, float>::const_iterator i = times.find(rating);
if(i == times.end()){
UNORDERED_MAP<int, float>::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<std::string, struct ToolGroupCap> ToolGCMap;
typedef std::map<std::string, s16> DamageGroup;
typedef UNORDERED_MAP<std::string, struct ToolGroupCap> ToolGCMap;
typedef UNORDERED_MAP<std::string, s16> 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()
):