Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nerzhul committed Oct 5, 2016
1 parent d4c7625 commit 5f084cd
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 48 deletions.
18 changes: 8 additions & 10 deletions src/script/common/c_content.cpp
Expand Up @@ -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"
Expand All @@ -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());
Expand Down
4 changes: 2 additions & 2 deletions src/settings.h
Expand Up @@ -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>

Expand All @@ -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,
Expand Down
26 changes: 11 additions & 15 deletions src/sound_openal.cpp
Expand Up @@ -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

Expand Down Expand Up @@ -271,8 +271,8 @@ class OpenALSoundManager: public ISoundManager
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;
Expand Down Expand Up @@ -337,7 +337,7 @@ class OpenALSoundManager: public ISoundManager
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) {
Expand All @@ -351,7 +351,7 @@ class OpenALSoundManager: public ISoundManager

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);
Expand All @@ -365,7 +365,7 @@ class OpenALSoundManager: public ISoundManager

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;
Expand Down Expand Up @@ -443,8 +443,7 @@ class OpenALSoundManager: public ISoundManager

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;
Expand Down Expand Up @@ -484,10 +483,8 @@ class OpenALSoundManager: public ISoundManager
<<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
Expand Down Expand Up @@ -583,9 +580,8 @@ class OpenALSoundManager: public ISoundManager
}
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;

Expand Down
23 changes: 11 additions & 12 deletions src/tool.cpp
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -197,7 +196,7 @@ PunchDamageResult getPunchDamage(
do_hit = false;
}
}

PunchDamageResult result;
if(do_hit)
{
Expand Down
15 changes: 6 additions & 9 deletions src/tool.h
Expand Up @@ -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;

Expand All @@ -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;
}
Expand All @@ -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()
):
Expand Down

0 comments on commit 5f084cd

Please sign in to comment.