From 997fc59c7e29a4d2b3f19df9972f3a7d4ac894e3 Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Thu, 6 Oct 2016 10:12:14 +0200 Subject: [PATCH 1/5] Use relative position for nametags --- src/camera.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/camera.cpp b/src/camera.cpp index 4feab1fe..b86f218f 100644 --- a/src/camera.cpp +++ b/src/camera.cpp @@ -552,7 +552,7 @@ void Camera::drawNametags() // shadow can remain. continue; } - v3f pos = nametag->parent_node->getPosition() + v3f(0.0, 1.1 * BS, 0.0); + v3f pos = nametag->parent_node->getAbsolutePosition() + v3f(0.0, 1.1 * BS, 0.0); f32 transformed_pos[4] = { pos.X, pos.Y, pos.Z, 1.0f }; trans.multiplyWith1x4Matrix(transformed_pos); if (transformed_pos[3] > 0) { From 667975fe3adee935a3f4d2b1a421a295771c664d Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Thu, 6 Oct 2016 08:48:20 +0200 Subject: [PATCH 2/5] Use more unordered_maps to improve performance in c++11 builds --- src/client.cpp | 9 +++------ src/client.h | 6 +++--- src/clientobject.cpp | 10 ++++------ src/clientobject.h | 3 ++- src/content_cao.cpp | 2 +- src/network/clientpackethandler.cpp | 4 +--- src/script/lua_api/l_mapgen.cpp | 4 ++-- src/server.cpp | 8 ++++---- src/util/string.h | 3 ++- 9 files changed, 22 insertions(+), 27 deletions(-) diff --git a/src/client.cpp b/src/client.cpp index a599e21d..63653998 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -623,10 +623,8 @@ void Client::step(float dtime) Update positions of sounds attached to objects */ { - for(std::map::iterator - i = m_sounds_to_objects.begin(); - i != m_sounds_to_objects.end(); ++i) - { + for(UNORDERED_MAP::iterator i = m_sounds_to_objects.begin(); + i != m_sounds_to_objects.end(); ++i) { int client_id = i->first; u16 object_id = i->second; ClientActiveObject *cao = m_env.getActiveObject(object_id); @@ -645,8 +643,7 @@ void Client::step(float dtime) m_removed_sounds_check_timer = 0; // Find removed sounds and clear references to them std::vector removed_server_ids; - for(std::map::iterator - i = m_sounds_server_to_client.begin(); + for(UNORDERED_MAP::iterator i = m_sounds_server_to_client.begin(); i != m_sounds_server_to_client.end();) { s32 server_id = i->first; int client_id = i->second; diff --git a/src/client.h b/src/client.h index fb479068..a71c1dcb 100644 --- a/src/client.h +++ b/src/client.h @@ -663,11 +663,11 @@ private: // Sounds float m_removed_sounds_check_timer; // Mapping from server sound ids to our sound ids - std::map m_sounds_server_to_client; + UNORDERED_MAP m_sounds_server_to_client; // And the other way! - std::map m_sounds_client_to_server; + UNORDERED_MAP m_sounds_client_to_server; // And relations to objects - std::map m_sounds_to_objects; + UNORDERED_MAP m_sounds_to_objects; // Privileges UNORDERED_SET m_privileges; diff --git a/src/clientobject.cpp b/src/clientobject.cpp index a11757ea..ff3f4718 100644 --- a/src/clientobject.cpp +++ b/src/clientobject.cpp @@ -43,12 +43,11 @@ ClientActiveObject* ClientActiveObject::create(ActiveObjectType type, IGameDef *gamedef, ClientEnvironment *env) { // Find factory function - std::map::iterator n; - n = m_types.find(type); + UNORDERED_MAP::iterator n = m_types.find(type); if(n == m_types.end()) { // If factory is not found, just return. - warningstream<<"ClientActiveObject: No factory for type=" - <<(int)type< +#include "util/cpp11_container.h" /* @@ -103,7 +104,7 @@ protected: ClientEnvironment *m_env; private: // Used for creating objects based on type - static std::map m_types; + static UNORDERED_MAP m_types; }; struct DistanceSortedActiveObject diff --git a/src/content_cao.cpp b/src/content_cao.cpp index a141690f..33dae682 100644 --- a/src/content_cao.cpp +++ b/src/content_cao.cpp @@ -51,7 +51,7 @@ struct ToolCapabilities; #define PP(x) "("<<(x).X<<","<<(x).Y<<","<<(x).Z<<")" -std::map ClientActiveObject::m_types; +UNORDERED_MAP ClientActiveObject::m_types; SmoothTranslator::SmoothTranslator(): vect_old(0,0,0), diff --git a/src/network/clientpackethandler.cpp b/src/network/clientpackethandler.cpp index 48c573da..6d42edd7 100644 --- a/src/network/clientpackethandler.cpp +++ b/src/network/clientpackethandler.cpp @@ -812,9 +812,7 @@ void Client::handleCommand_StopSound(NetworkPacket* pkt) *pkt >> server_id; - std::map::iterator i = - m_sounds_server_to_client.find(server_id); - + UNORDERED_MAP::iterator i = m_sounds_server_to_client.find(server_id); if (i != m_sounds_server_to_client.end()) { int client_id = i->second; m_sound->stopSound(client_id); diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index a176f4f5..da8e71cd 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -244,7 +244,7 @@ bool read_schematic_def(lua_State *L, int index, schem->schemdata = new MapNode[numnodes]; size_t names_base = names->size(); - std::map name_id_map; + UNORDERED_MAP name_id_map; u32 i = 0; for (lua_pushnil(L); lua_next(L, -2); i++, lua_pop(L, 1)) { @@ -266,7 +266,7 @@ bool read_schematic_def(lua_State *L, int index, u8 param2 = getintfield_default(L, -1, "param2", 0); //// Find or add new nodename-to-ID mapping - std::map::iterator it = name_id_map.find(name); + UNORDERED_MAP::iterator it = name_id_map.find(name); content_t name_index; if (it != name_id_map.end()) { name_index = it->second; diff --git a/src/server.cpp b/src/server.cpp index a8494f76..e9983ba1 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -794,7 +794,7 @@ void Server::AsyncRunStep(bool initial_step) // Key = object id // Value = data sent by object - std::map* > buffered_messages; + UNORDERED_MAP* > buffered_messages; // Get active object messages from environment for(;;) { @@ -803,7 +803,7 @@ void Server::AsyncRunStep(bool initial_step) break; std::vector* message_list = NULL; - std::map* >::iterator n; + UNORDERED_MAP* >::iterator n; n = buffered_messages.find(aom.id); if (n == buffered_messages.end()) { message_list = new std::vector; @@ -824,7 +824,7 @@ void Server::AsyncRunStep(bool initial_step) std::string reliable_data; std::string unreliable_data; // Go through all objects in message buffer - for (std::map* >::iterator + for (UNORDERED_MAP* >::iterator j = buffered_messages.begin(); j != buffered_messages.end(); ++j) { // If object is not known by client, skip it @@ -868,7 +868,7 @@ void Server::AsyncRunStep(bool initial_step) m_clients.unlock(); // Clear buffered_messages - for(std::map* >::iterator + for(UNORDERED_MAP* >::iterator i = buffered_messages.begin(); i != buffered_messages.end(); ++i) { delete i->second; diff --git a/src/util/string.h b/src/util/string.h index 724543a3..572c3715 100644 --- a/src/util/string.h +++ b/src/util/string.h @@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #define UTIL_STRING_HEADER #include "irrlichttypes_bloated.h" +#include "cpp11_container.h" #include #include #include @@ -54,7 +55,7 @@ with this program; if not, write to the Free Software Foundation, Inc., (((unsigned char)(x) < 0xe0) ? 2 : \ (((unsigned char)(x) < 0xf0) ? 3 : 4)) -typedef std::map StringMap; +typedef UNORDERED_MAP StringMap; struct FlagDesc { const char *name; From b66a5d2f8842cc84ae44257dc0ead255e5b0538f Mon Sep 17 00:00:00 2001 From: SmallJoker Date: Thu, 6 Oct 2016 13:49:40 +0200 Subject: [PATCH 3/5] Fix narrow string compiling issue on MSVC2010 --- src/server.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/server.cpp b/src/server.cpp index e9983ba1..639e6462 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -2757,7 +2757,7 @@ std::wstring Server::handleChat(const std::string &name, const std::wstring &wna std::wstringstream ws; ws << L"You cannot send more messages. You are limited to " << g_settings->getFloat("chat_message_limit_per_10sec") - << " messages per 10 seconds."; + << L" messages per 10 seconds."; return ws.str(); } case RPLAYER_CHATRESULT_KICK: @@ -2770,7 +2770,7 @@ std::wstring Server::handleChat(const std::string &name, const std::wstring &wna if (m_max_chatmessage_length > 0 && wmessage.length() > m_max_chatmessage_length) { return L"Your message exceed the maximum chat message limit set on the server. " - "It was refused. Send a shorter message"; + L"It was refused. Send a shorter message"; } // Commands are implemented in Lua, so only catch invalid From 155288ee981c70f505526347cb2bcda4df1c8e6b Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Thu, 6 Oct 2016 19:20:12 +0200 Subject: [PATCH 4/5] use unordered containers where possible (patch 4 on X) Also remove some unused parameters/functions --- src/content_sao.cpp | 28 ++++++++++++++-------- src/content_sao.h | 5 ++-- src/guiFormSpecMenu.h | 4 +--- src/map.h | 1 + src/mapblock_mesh.cpp | 24 ++++++++----------- src/mapblock_mesh.h | 15 ++++++------ src/network/serverpackethandler.cpp | 4 +--- src/script/cpp_api/s_async.cpp | 3 ++- src/script/cpp_api/s_async.h | 2 +- src/server.cpp | 21 +++++++--------- src/server.h | 16 +++++-------- src/util/numeric.cpp | 2 +- src/util/numeric.h | 37 ++--------------------------- 13 files changed, 63 insertions(+), 99 deletions(-) diff --git a/src/content_sao.cpp b/src/content_sao.cpp index 1e7e788e..895c2604 100644 --- a/src/content_sao.cpp +++ b/src/content_sao.cpp @@ -347,8 +347,10 @@ void LuaEntitySAO::step(float dtime, bool send_recommended) if(m_bone_position_sent == false){ m_bone_position_sent = true; - for(std::map >::const_iterator ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){ - std::string str = gob_cmd_update_bone_position((*ii).first, (*ii).second.X, (*ii).second.Y); + for (UNORDERED_MAP >::const_iterator + ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){ + std::string str = gob_cmd_update_bone_position((*ii).first, + (*ii).second.X, (*ii).second.Y); // create message and add to list ActiveObjectMessage aom(getId(), true, str); m_messages_out.push(aom); @@ -383,8 +385,10 @@ std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version) os< >::const_iterator ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){ - os< >::const_iterator + ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) { + os << serializeLongString(gob_cmd_update_bone_position((*ii).first, + (*ii).second.X, (*ii).second.Y)); // m_bone_position.size } os< >::const_iterator ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){ + for (UNORDERED_MAP >::const_iterator + ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) { os< >::const_iterator ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){ - std::string str = gob_cmd_update_bone_position((*ii).first, (*ii).second.X, (*ii).second.Y); + for (UNORDERED_MAP >::const_iterator + ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) { + std::string str = gob_cmd_update_bone_position((*ii).first, + (*ii).second.X, (*ii).second.Y); // create message and add to list ActiveObjectMessage aom(getId(), true, str); m_messages_out.push(aom); } } - if(m_attachment_sent == false){ + if (!m_attachment_sent){ m_attachment_sent = true; - std::string str = gob_cmd_update_attachment(m_attachment_parent_id, m_attachment_bone, m_attachment_position, m_attachment_rotation); + std::string str = gob_cmd_update_attachment(m_attachment_parent_id, + m_attachment_bone, m_attachment_position, m_attachment_rotation); // create message and add to list ActiveObjectMessage aom(getId(), true, str); m_messages_out.push(aom); diff --git a/src/content_sao.h b/src/content_sao.h index 44d40d33..ccae90b7 100644 --- a/src/content_sao.h +++ b/src/content_sao.h @@ -112,7 +112,7 @@ private: bool m_animation_loop; bool m_animation_sent; - std::map > m_bone_position; + UNORDERED_MAP > m_bone_position; bool m_bone_position_sent; int m_attachment_parent_id; @@ -321,7 +321,8 @@ private: bool m_animation_loop; bool m_animation_sent; - std::map > m_bone_position; // Stores position and rotation for each bone name + // Stores position and rotation for each bone name + UNORDERED_MAP > m_bone_position; bool m_bone_position_sent; int m_attachment_parent_id; diff --git a/src/guiFormSpecMenu.h b/src/guiFormSpecMenu.h index 21054b72..15372097 100644 --- a/src/guiFormSpecMenu.h +++ b/src/guiFormSpecMenu.h @@ -409,8 +409,6 @@ protected: std::vector > > m_dropdowns; ItemSpec *m_selected_item; - f32 m_timer1; - f32 m_timer2; u32 m_selected_amount; bool m_selected_dragging; @@ -462,7 +460,7 @@ private: GUITable::TableOptions table_options; GUITable::TableColumns table_columns; // used to restore table selection/scroll/treeview state - std::map table_dyndata; + UNORDERED_MAP table_dyndata; } parserData; typedef struct { diff --git a/src/map.h b/src/map.h index 13775fde..c73fa92b 100644 --- a/src/map.h +++ b/src/map.h @@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "voxel.h" #include "modifiedstate.h" #include "util/container.h" +#include "util/cpp11_container.h" #include "nodetimer.h" #include "map_settings_manager.h" diff --git a/src/mapblock_mesh.cpp b/src/mapblock_mesh.cpp index a11fb588..00f83e7a 100644 --- a/src/mapblock_mesh.cpp +++ b/src/mapblock_mesh.cpp @@ -1033,7 +1033,7 @@ MapBlockMesh::MapBlockMesh(MeshMakeData *data, v3s16 camera_offset): m_enable_shaders = data->m_use_shaders; m_use_tangent_vertices = data->m_use_tangent_vertices; m_enable_vbo = g_settings->getBool("enable_vbo"); - + if (g_settings->getBool("enable_minimap")) { m_minimap_mapblock = new MinimapMapblock; m_minimap_mapblock->getMinimapNodes( @@ -1298,10 +1298,8 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_rat // Cracks if(crack != m_last_crack) { - for(std::map::iterator - i = m_crack_materials.begin(); - i != m_crack_materials.end(); ++i) - { + for (UNORDERED_MAP::iterator i = m_crack_materials.begin(); + i != m_crack_materials.end(); ++i) { scene::IMeshBuffer *buf = m_mesh->getMeshBuffer(i->first); std::string basename = i->second; @@ -1315,9 +1313,9 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_rat // If the current material is also animated, // update animation info - std::map::iterator anim_iter = - m_animation_tiles.find(i->first); - if(anim_iter != m_animation_tiles.end()){ + UNORDERED_MAP::iterator anim_iter = + m_animation_tiles.find(i->first); + if (anim_iter != m_animation_tiles.end()){ TileSpec &tile = anim_iter->second; tile.texture = new_texture; tile.texture_id = new_texture_id; @@ -1330,10 +1328,8 @@ bool MapBlockMesh::animate(bool faraway, float time, int crack, u32 daynight_rat } // Texture animation - for(std::map::iterator - i = m_animation_tiles.begin(); - i != m_animation_tiles.end(); ++i) - { + for (UNORDERED_MAP::iterator i = m_animation_tiles.begin(); + i != m_animation_tiles.end(); ++i) { const TileSpec &tile = i->second; // Figure out current frame int frameoffset = m_animation_frame_offsets[i->first]; @@ -1443,7 +1439,7 @@ void MeshCollector::append(const TileSpec &tile, vertices[i].Color, vertices[i].TCoords); p->vertices.push_back(vert); } - } + } for (u32 i = 0; i < numIndices; i++) { u32 j = indices[i] + vertex_count; @@ -1499,7 +1495,7 @@ void MeshCollector::append(const TileSpec &tile, vertices[i].Normal, c, vertices[i].TCoords); p->vertices.push_back(vert); } - } + } for (u32 i = 0; i < numIndices; i++) { u32 j = indices[i] + vertex_count; diff --git a/src/mapblock_mesh.h b/src/mapblock_mesh.h index f89fbe66..8376468d 100644 --- a/src/mapblock_mesh.h +++ b/src/mapblock_mesh.h @@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "irrlichttypes_extrabloated.h" #include "client/tile.h" #include "voxel.h" +#include "util/cpp11_container.h" #include class IGameDef; @@ -121,7 +122,7 @@ public: if(m_animation_force_timer > 0) m_animation_force_timer--; } - + void updateCameraOffset(v3s16 camera_offset); private: @@ -144,20 +145,20 @@ private: // Last crack value passed to animate() int m_last_crack; // Maps mesh buffer (i.e. material) indices to base texture names - std::map m_crack_materials; + UNORDERED_MAP m_crack_materials; // Animation info: texture animationi // Maps meshbuffers to TileSpecs - std::map m_animation_tiles; - std::map m_animation_frames; // last animation frame - std::map m_animation_frame_offsets; - + UNORDERED_MAP m_animation_tiles; + UNORDERED_MAP m_animation_frames; // last animation frame + UNORDERED_MAP m_animation_frame_offsets; + // Animation info: day/night transitions // Last daynight_ratio value passed to animate() u32 m_last_daynight_ratio; // For each meshbuffer, maps vertex indices to (day,night) pairs std::map > > m_daynight_diffs; - + // Camera offset info -> do we have to translate the mesh? v3s16 m_camera_offset; }; diff --git a/src/network/serverpackethandler.cpp b/src/network/serverpackethandler.cpp index a8bfd906..f9061cc4 100644 --- a/src/network/serverpackethandler.cpp +++ b/src/network/serverpackethandler.cpp @@ -1693,9 +1693,7 @@ void Server::handleCommand_RemovedSounds(NetworkPacket* pkt) *pkt >> id; - std::map::iterator i = - m_playing_sounds.find(id); - + UNORDERED_MAP::iterator i = m_playing_sounds.find(id); if (i == m_playing_sounds.end()) continue; diff --git a/src/script/cpp_api/s_async.cpp b/src/script/cpp_api/s_async.cpp index 9bf3fcf4..1fb84fab 100644 --- a/src/script/cpp_api/s_async.cpp +++ b/src/script/cpp_api/s_async.cpp @@ -81,6 +81,7 @@ bool AsyncEngine::registerFunction(const char* name, lua_CFunction func) if (initDone) { return false; } + functionList[name] = func; return true; } @@ -203,7 +204,7 @@ void AsyncEngine::pushFinishedJobs(lua_State* L) { /******************************************************************************/ void AsyncEngine::prepareEnvironment(lua_State* L, int top) { - for (std::map::iterator it = functionList.begin(); + for (UNORDERED_MAP::iterator it = functionList.begin(); it != functionList.end(); it++) { lua_pushstring(L, it->first.c_str()); lua_pushcfunction(L, it->second); diff --git a/src/script/cpp_api/s_async.h b/src/script/cpp_api/s_async.h index 8d612d58..016381e5 100644 --- a/src/script/cpp_api/s_async.h +++ b/src/script/cpp_api/s_async.h @@ -132,7 +132,7 @@ private: bool initDone; // Internal store for registred functions - std::map functionList; + UNORDERED_MAP functionList; // Internal counter to create job IDs unsigned int jobIdCounter; diff --git a/src/server.cpp b/src/server.cpp index 639e6462..2dd070b1 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -868,7 +868,7 @@ void Server::AsyncRunStep(bool initial_step) m_clients.unlock(); // Clear buffered_messages - for(UNORDERED_MAP* >::iterator + for (UNORDERED_MAP* >::iterator i = buffered_messages.begin(); i != buffered_messages.end(); ++i) { delete i->second; @@ -2016,16 +2016,15 @@ s32 Server::playSound(const SimpleSoundSpec &spec, void Server::stopSound(s32 handle) { // Get sound reference - std::map::iterator i = - m_playing_sounds.find(handle); - if(i == m_playing_sounds.end()) + UNORDERED_MAP::iterator i = m_playing_sounds.find(handle); + if (i == m_playing_sounds.end()) return; ServerPlayingSound &psound = i->second; NetworkPacket pkt(TOCLIENT_STOP_SOUND, 4); pkt << handle; - for(std::set::iterator i = psound.clients.begin(); + for (UNORDERED_SET::iterator i = psound.clients.begin(); i != psound.clients.end(); ++i) { // Send as reliable m_clients.send(*i, 0, &pkt, true); @@ -2322,7 +2321,7 @@ void Server::sendMediaAnnouncement(u16 peer_id) NetworkPacket pkt(TOCLIENT_ANNOUNCE_MEDIA, 0, peer_id); pkt << (u16) m_media.size(); - for (std::map::iterator i = m_media.begin(); + for (UNORDERED_MAP::iterator i = m_media.begin(); i != m_media.end(); ++i) { pkt << i->first << i->second.sha1_digest; } @@ -2367,7 +2366,7 @@ void Server::sendRequestedMedia(u16 peer_id, i != tosend.end(); ++i) { const std::string &name = *i; - if(m_media.find(name) == m_media.end()) { + if (m_media.find(name) == m_media.end()) { errorstream<<"Server::sendRequestedMedia(): Client asked for " <<"unknown file \""<<(name)<<"\""<::iterator - i = m_playing_sounds.begin(); - i != m_playing_sounds.end();) - { + for (UNORDERED_MAP::iterator + i = m_playing_sounds.begin(); i != m_playing_sounds.end();) { ServerPlayingSound &psound = i->second; psound.clients.erase(peer_id); - if(psound.clients.empty()) + if (psound.clients.empty()) m_playing_sounds.erase(i++); else ++i; diff --git a/src/server.h b/src/server.h index 3ad894b3..34427a71 100644 --- a/src/server.h +++ b/src/server.h @@ -157,7 +157,7 @@ struct ServerSoundParams struct ServerPlayingSound { ServerSoundParams params; - std::set clients; // peer ids + UNORDERED_SET clients; // peer ids }; class Server : public con::PeerHandler, public MapEventReceiver, @@ -243,11 +243,9 @@ public: std::wstring getStatusString(); // read shutdown state - inline bool getShutdownRequested() - { return m_shutdown_requested; } + inline bool getShutdownRequested() const { return m_shutdown_requested; } // request server to shutdown - inline void requestShutdown() { m_shutdown_requested = true; } void requestShutdown(const std::string &msg, bool reconnect) { m_shutdown_requested = true; @@ -323,8 +321,7 @@ public: const ModSpec* getModSpec(const std::string &modname) const; void getModNames(std::vector &modlist); std::string getBuiltinLuaPath(); - inline std::string getWorldPath() const - { return m_path_world; } + inline std::string getWorldPath() const { return m_path_world; } inline bool isSingleplayer() { return m_simple_singleplayer_mode; } @@ -356,8 +353,7 @@ public: bool setSky(Player *player, const video::SColor &bgcolor, const std::string &type, const std::vector ¶ms); - bool overrideDayNightRatio(Player *player, bool do_override, - float brightness); + bool overrideDayNightRatio(Player *player, bool do_override, float brightness); /* con::PeerHandler implementation. */ void peerAdded(con::Peer *peer); @@ -654,12 +650,12 @@ private: u16 m_ignore_map_edit_events_peer_id; // media files known to server - std::map m_media; + UNORDERED_MAP m_media; /* Sounds */ - std::map m_playing_sounds; + UNORDERED_MAP m_playing_sounds; s32 m_next_sound_id; /* diff --git a/src/util/numeric.cpp b/src/util/numeric.cpp index 42ebd902..6a7bfcac 100644 --- a/src/util/numeric.cpp +++ b/src/util/numeric.cpp @@ -27,7 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include -std::map > FacePositionCache::m_cache; +UNORDERED_MAP > FacePositionCache::m_cache; Mutex FacePositionCache::m_cache_mutex; // Calculate the borders of a "d-radius" cube // TODO: Make it work without mutex and data races, probably thread-local diff --git a/src/util/numeric.h b/src/util/numeric.h index 61532786..4cdc254c 100644 --- a/src/util/numeric.h +++ b/src/util/numeric.h @@ -26,8 +26,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "../irr_v3d.h" #include "../irr_aabb3d.h" #include "../threading/mutex.h" +#include "cpp11_container.h" #include -#include #include @@ -41,26 +41,10 @@ public: static std::vector getFacePositions(u16 d); private: static void generateFacePosition(u16 d); - static std::map > m_cache; + static UNORDERED_MAP > m_cache; static Mutex m_cache_mutex; }; -class IndentationRaiser -{ -public: - IndentationRaiser(u16 *indentation) - { - m_indentation = indentation; - (*m_indentation)++; - } - ~IndentationRaiser() - { - (*m_indentation)--; - } -private: - u16 *m_indentation; -}; - inline s16 getContainerPos(s16 p, s16 d) { return (p>=0 ? p : p-d+1) / d; @@ -149,23 +133,6 @@ inline bool isInArea(v3s16 p, v3s16 d) #define rangelim(d, min, max) ((d) < (min) ? (min) : ((d)>(max)?(max):(d))) #define myfloor(x) ((x) > 0.0 ? (int)(x) : (int)(x) - 1) -inline v3s16 arealim(v3s16 p, s16 d) -{ - if(p.X < 0) - p.X = 0; - if(p.Y < 0) - p.Y = 0; - if(p.Z < 0) - p.Z = 0; - if(p.X > d-1) - p.X = d-1; - if(p.Y > d-1) - p.Y = d-1; - if(p.Z > d-1) - p.Z = d-1; - return p; -} - // The naive swap performs better than the xor version #define SWAP(t, x, y) do { \ t temp = x; \ From 0a16e53b40d347db7dcd04cb694d0f8f2ed1a5a7 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Thu, 6 Oct 2016 21:13:04 +0200 Subject: [PATCH 5/5] Fix C++11 Windows build of threading code The initial problem was that mutex_auto_lock.h tries to use std::unique_lock despite mutex.h not using C++11's std::mutex on Windows. The problem here is the mismatch between C++11 usage conditions of the two headers. This commit moves the decision logic to threads.h and makes sure mutex.h, mutex_auto_lock.h and event.h all use the same features. --- src/threading/event.cpp | 16 ++++++++-------- src/threading/event.h | 17 ++++++----------- src/threading/mutex.cpp | 19 +++++++++---------- src/threading/mutex.h | 15 ++++++++------- src/threading/mutex_auto_lock.h | 4 +++- src/threading/thread.cpp | 20 ++++++++++---------- src/threading/thread.h | 13 ++++--------- src/threads.h | 18 +++++++++++++++++- 8 files changed, 65 insertions(+), 57 deletions(-) diff --git a/src/threading/event.cpp b/src/threading/event.cpp index 165f9d83..0d5928f1 100644 --- a/src/threading/event.cpp +++ b/src/threading/event.cpp @@ -27,8 +27,8 @@ DEALINGS IN THE SOFTWARE. Event::Event() { -#if __cplusplus < 201103L -# ifdef _WIN32 +#ifndef USE_CPP11_MUTEX +# if USE_WIN_MUTEX event = CreateEvent(NULL, false, false, NULL); # else pthread_cond_init(&cv, NULL); @@ -38,10 +38,10 @@ Event::Event() #endif } -#if __cplusplus < 201103L +#ifndef USE_CPP11_MUTEX Event::~Event() { -#ifdef _WIN32 +#if USE_WIN_MUTEX CloseHandle(event); #else pthread_cond_destroy(&cv); @@ -53,13 +53,13 @@ Event::~Event() void Event::wait() { -#if __cplusplus >= 201103L +#if USE_CPP11_MUTEX MutexAutoLock lock(mutex); while (!notified) { cv.wait(lock); } notified = false; -#elif defined(_WIN32) +#elif USE_WIN_MUTEX WaitForSingleObject(event, INFINITE); #else pthread_mutex_lock(&mutex); @@ -74,11 +74,11 @@ void Event::wait() void Event::signal() { -#if __cplusplus >= 201103L +#if USE_CPP11_MUTEX MutexAutoLock lock(mutex); notified = true; cv.notify_one(); -#elif defined(_WIN32) +#elif USE_WIN_MUTEX SetEvent(event); #else pthread_mutex_lock(&mutex); diff --git a/src/threading/event.h b/src/threading/event.h index dd516457..26cb8997 100644 --- a/src/threading/event.h +++ b/src/threading/event.h @@ -26,17 +26,12 @@ DEALINGS IN THE SOFTWARE. #ifndef THREADING_EVENT_H #define THREADING_EVENT_H -#if __cplusplus >= 201103L +#include "threads.h" + +#if USE_CPP11_MUTEX #include #include "threading/mutex.h" #include "threading/mutex_auto_lock.h" -#elif defined(_WIN32) - #ifndef WIN32_LEAN_AND_MEAN - #define WIN32_LEAN_AND_MEAN - #endif - #include -#else - #include #endif @@ -49,18 +44,18 @@ DEALINGS IN THE SOFTWARE. class Event { public: Event(); -#if __cplusplus < 201103L +#ifndef USE_CPP11_MUTEX ~Event(); #endif void wait(); void signal(); private: -#if __cplusplus >= 201103L +#if USE_CPP11_MUTEX std::condition_variable cv; Mutex mutex; bool notified; -#elif defined(_WIN32) +#elif USE_WIN_MUTEX HANDLE event; #else pthread_cond_t cv; diff --git a/src/threading/mutex.cpp b/src/threading/mutex.cpp index f2b07bec..0908b5d3 100644 --- a/src/threading/mutex.cpp +++ b/src/threading/mutex.cpp @@ -23,14 +23,13 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -// Windows std::mutex is much slower than the critical section API -#if __cplusplus < 201103L || defined(_WIN32) +#include "threads.h" + +#ifndef USE_CPP11_MUTEX #include "threading/mutex.h" -#ifndef _WIN32 - #include -#endif +#include #define UNUSED(expr) do { (void)(expr); } while (0) @@ -47,7 +46,7 @@ Mutex::Mutex(bool recursive) void Mutex::init_mutex(bool recursive) { -#ifdef _WIN32 +#if USE_WIN_MUTEX // Windows critical sections are recursive by default UNUSED(recursive); @@ -69,7 +68,7 @@ void Mutex::init_mutex(bool recursive) Mutex::~Mutex() { -#ifdef _WIN32 +#if USE_WIN_MUTEX DeleteCriticalSection(&mutex); #else int ret = pthread_mutex_destroy(&mutex); @@ -80,7 +79,7 @@ Mutex::~Mutex() void Mutex::lock() { -#ifdef _WIN32 +#if USE_WIN_MUTEX EnterCriticalSection(&mutex); #else int ret = pthread_mutex_lock(&mutex); @@ -91,7 +90,7 @@ void Mutex::lock() void Mutex::unlock() { -#ifdef _WIN32 +#if USE_WIN_MUTEX LeaveCriticalSection(&mutex); #else int ret = pthread_mutex_unlock(&mutex); @@ -104,5 +103,5 @@ RecursiveMutex::RecursiveMutex() : Mutex(true) {} -#endif +#endif // C++11 diff --git a/src/threading/mutex.h b/src/threading/mutex.h index dadbd050..fb5c029f 100644 --- a/src/threading/mutex.h +++ b/src/threading/mutex.h @@ -26,14 +26,15 @@ DEALINGS IN THE SOFTWARE. #ifndef THREADING_MUTEX_H #define THREADING_MUTEX_H -// Windows std::mutex is much slower than the critical section API -#if __cplusplus >= 201103L && !defined(_WIN32) +#include "threads.h" + +#if USE_CPP11_MUTEX #include using Mutex = std::mutex; using RecursiveMutex = std::recursive_mutex; #else -#ifdef _WIN32 +#if USE_WIN_MUTEX #ifndef _WIN32_WINNT #define _WIN32_WINNT 0x0501 #endif @@ -41,7 +42,7 @@ DEALINGS IN THE SOFTWARE. #define WIN32_LEAN_AND_MEAN #endif #include -#else // pthread +#else #include #endif @@ -59,9 +60,9 @@ protected: Mutex(bool recursive); void init_mutex(bool recursive); private: -#ifdef _WIN32 +#if USE_WIN_MUTEX CRITICAL_SECTION mutex; -#else // pthread +#else pthread_mutex_t mutex; #endif @@ -76,6 +77,6 @@ public: DISABLE_CLASS_COPY(RecursiveMutex); }; -#endif // C++11 +#endif // C++11 #endif diff --git a/src/threading/mutex_auto_lock.h b/src/threading/mutex_auto_lock.h index 25caf7e1..d79c68a9 100644 --- a/src/threading/mutex_auto_lock.h +++ b/src/threading/mutex_auto_lock.h @@ -26,7 +26,9 @@ DEALINGS IN THE SOFTWARE. #ifndef THREADING_MUTEX_AUTO_LOCK_H #define THREADING_MUTEX_AUTO_LOCK_H -#if __cplusplus >= 201103L +#include "threads.h" + +#if USE_CPP11_MUTEX #include using MutexAutoLock = std::unique_lock; using RecursiveMutexAutoLock = std::unique_lock; diff --git a/src/threading/thread.cpp b/src/threading/thread.cpp index 0cd53679..fbe4ba1f 100644 --- a/src/threading/thread.cpp +++ b/src/threading/thread.cpp @@ -198,7 +198,7 @@ bool Thread::kill() m_running = false; -#ifdef _WIN32 +#if USE_WIN_THREADS TerminateThread(m_thread_handle, 0); CloseHandle(m_thread_handle); #else @@ -310,10 +310,16 @@ void Thread::setName(const std::string &name) unsigned int Thread::getNumberOfProcessors() { -#if __cplusplus >= 201103L +#if USE_CPP11_THREADS return std::thread::hardware_concurrency(); +#elif USE_WIN_THREADS + + SYSTEM_INFO sysinfo; + GetSystemInfo(&sysinfo); + return sysinfo.dwNumberOfProcessors; + #elif defined(_SC_NPROCESSORS_ONLN) return sysconf(_SC_NPROCESSORS_ONLN); @@ -335,12 +341,6 @@ unsigned int Thread::getNumberOfProcessors() return get_nprocs(); -#elif defined(_WIN32) - - SYSTEM_INFO sysinfo; - GetSystemInfo(&sysinfo); - return sysinfo.dwNumberOfProcessors; - #elif defined(PTW32_VERSION) || defined(__hpux) return pthread_num_processors_np(); @@ -359,7 +359,7 @@ bool Thread::bindToProcessor(unsigned int proc_number) return false; -#elif defined(_WIN32) +#elif USE_WIN_THREADS return SetThreadAffinityMask(getThreadHandle(), 1 << proc_number); @@ -407,7 +407,7 @@ bool Thread::bindToProcessor(unsigned int proc_number) bool Thread::setPriority(int prio) { -#if defined(_WIN32) +#if USE_WIN_THREADS return SetThreadPriority(getThreadHandle(), prio); diff --git a/src/threading/thread.h b/src/threading/thread.h index de800ecb..14a0e13a 100644 --- a/src/threading/thread.h +++ b/src/threading/thread.h @@ -32,9 +32,6 @@ DEALINGS IN THE SOFTWARE. #include "threads.h" #include -#if USE_CPP11_THREADS - #include // for std::thread -#endif #ifdef _AIX #include // for tid_t #endif @@ -157,9 +154,11 @@ private: Atomic m_running; Mutex m_mutex; -#ifndef USE_CPP11_THREADS +#if USE_CPP11_THREADS + std::thread *m_thread_obj; +#else threadhandle_t m_thread_handle; -# if _WIN32 +# if USE_WIN_THREADS threadid_t m_thread_id; # endif #endif @@ -172,10 +171,6 @@ private: tid_t m_kernel_thread_id; #endif -#if USE_CPP11_THREADS - std::thread *m_thread_obj; -#endif - DISABLE_CLASS_COPY(Thread); }; diff --git a/src/threads.h b/src/threads.h index d4306f63..ce98593c 100644 --- a/src/threads.h +++ b/src/threads.h @@ -21,7 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #define THREADS_HEADER // -// Determine which threading API we will use +// Determine which threading APIs we will use // #if __cplusplus >= 201103L #define USE_CPP11_THREADS 1 @@ -31,11 +31,27 @@ with this program; if not, write to the Free Software Foundation, Inc., #define USE_POSIX_THREADS 1 #endif +#if defined(_WIN32) + // Prefer critical section API because std::mutex is much slower on Windows + #define USE_WIN_MUTEX 1 +#elif __cplusplus >= 201103L + #define USE_CPP11_MUTEX 1 +#else + #define USE_POSIX_MUTEX 1 +#endif + /////////////// #if USE_CPP11_THREADS #include +#elif USE_POSIX_THREADS + #include +#else + #ifndef WIN32_LEAN_AND_MEAN + #define WIN32_LEAN_AND_MEAN + #endif + #include #endif #include "threading/mutex.h"