From 55cca34ee9bf77e9c8e47330c16c735fe12873d7 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Fri, 8 Aug 2025 11:40:12 +0200 Subject: [PATCH] Cache getTextureDirs() --- src/client/game.cpp | 56 ++++++++----------------------------- src/client/imagesource.cpp | 2 +- src/client/texturepaths.cpp | 20 +++++++++---- src/client/texturepaths.h | 2 +- src/server.cpp | 1 - src/util/container.h | 6 +++- src/util/thread.h | 9 +++--- 7 files changed, 37 insertions(+), 59 deletions(-) diff --git a/src/client/game.cpp b/src/client/game.cpp index cdf36a1a7..f133c634c 100644 --- a/src/client/game.cpp +++ b/src/client/game.cpp @@ -863,46 +863,17 @@ Game::Game() : m_chat_log_buf(g_logger), m_game_ui(new GameUI()) { - g_settings->registerChangedCallback("chat_log_level", - &settingChangedCallback, this); - g_settings->registerChangedCallback("doubletap_jump", - &settingChangedCallback, this); - g_settings->registerChangedCallback("toggle_sneak_key", - &settingChangedCallback, this); - g_settings->registerChangedCallback("toggle_aux1_key", - &settingChangedCallback, this); - g_settings->registerChangedCallback("enable_joysticks", - &settingChangedCallback, this); - g_settings->registerChangedCallback("enable_fog", - &settingChangedCallback, this); - g_settings->registerChangedCallback("mouse_sensitivity", - &settingChangedCallback, this); - g_settings->registerChangedCallback("joystick_frustum_sensitivity", - &settingChangedCallback, this); - g_settings->registerChangedCallback("repeat_place_time", - &settingChangedCallback, this); - g_settings->registerChangedCallback("repeat_dig_time", - &settingChangedCallback, this); - g_settings->registerChangedCallback("noclip", - &settingChangedCallback, this); - g_settings->registerChangedCallback("free_move", - &settingChangedCallback, this); - g_settings->registerChangedCallback("fog_start", - &settingChangedCallback, this); - g_settings->registerChangedCallback("cinematic", - &settingChangedCallback, this); - g_settings->registerChangedCallback("cinematic_camera_smoothing", - &settingChangedCallback, this); - g_settings->registerChangedCallback("camera_smoothing", - &settingChangedCallback, this); - g_settings->registerChangedCallback("invert_mouse", - &settingChangedCallback, this); - g_settings->registerChangedCallback("enable_hotbar_mouse_wheel", - &settingChangedCallback, this); - g_settings->registerChangedCallback("invert_hotbar_mouse_wheel", - &settingChangedCallback, this); - g_settings->registerChangedCallback("pause_on_lost_focus", - &settingChangedCallback, this); + clearTextureNameCache(); + + const char *settings[] = { + "chat_log_level", "doubletap_jump", "toggle_sneak_key", "toggle_aux1_key", + "enable_joysticks", "enable_fog", "mouse_sensitivity", "joystick_frustum_sensitivity", + "repeat_place_time", "repeat_dig_time", "noclip", "free_move", "fog_start", + "cinematic", "cinematic_camera_smoothing", "camera_smoothing", "invert_mouse", + "enable_hotbar_mouse_wheel", "invert_hotbar_mouse_wheel", "pause_on_lost_focus", + }; + for (auto s : settings) + g_settings->registerChangedCallback(s, &settingChangedCallback, this); readSettings(); } @@ -4254,11 +4225,6 @@ void the_game(volatile std::sig_atomic_t *kill, { Game game; - /* Make a copy of the server address because if a local singleplayer server - * is created then this is updated and we don't want to change the value - * passed to us by the calling function - */ - try { if (game.startup(kill, input, rendering_engine, start_data, diff --git a/src/client/imagesource.cpp b/src/client/imagesource.cpp index 5721f9666..a1c7334b1 100644 --- a/src/client/imagesource.cpp +++ b/src/client/imagesource.cpp @@ -1113,7 +1113,7 @@ bool ImageSource::generateImagePart(std::string_view part_of_name, << " for [combine" << std::endl; continue; } - infostream << "Adding \"" << filename<< "\" to combined " + tracestream << "Adding \"" << filename << "\" to combined " << pos_base << std::endl; video::IImage *img = generateImage(filename, source_image_names); diff --git a/src/client/texturepaths.cpp b/src/client/texturepaths.cpp index f2efa6038..5642512d5 100644 --- a/src/client/texturepaths.cpp +++ b/src/client/texturepaths.cpp @@ -5,6 +5,7 @@ #include "texturepaths.h" #include "util/container.h" +#include "util/thread.h" #include "settings.h" #include "filesys.h" #include "porting.h" @@ -12,10 +13,13 @@ // A cache from texture name to texture path static MutexedMap g_texturename_to_path_cache; +// Cached result of getTextureDirs() +static MutexedVariable> g_texturedirs_cache; void clearTextureNameCache() { g_texturename_to_path_cache.clear(); + g_texturedirs_cache.set({}); } // Find out the full path of an image by trying different filename extensions. @@ -74,24 +78,28 @@ std::string getTexturePath(const std::string &filename, bool *is_base_pack) break; } - // Check from default data directory i.e. .../minetest/textures/base/pack + // Check from default data directory if (fullpath.empty()) { - std::string base_path = porting::path_share + DIR_DELIM + "textures" - + DIR_DELIM + "base" + DIR_DELIM + "pack"; + std::string base_path = porting::path_share + DIR_DELIM "textures" + DIR_DELIM "base" DIR_DELIM "pack"; // Check all filename extensions. Returns "" if not found. fullpath = getImagePath(base_path + DIR_DELIM + filename); if (is_base_pack && !fullpath.empty()) *is_base_pack = true; } - // Add to cache (also an empty result is cached) + // Add to cache (an empty result is cached too) g_texturename_to_path_cache.set(filename, fullpath); - // Finally return it return fullpath; } std::vector getTextureDirs() { - return fs::GetRecursiveDirs(g_settings->get("texture_path")); + std::vector ret = g_texturedirs_cache.get(); + if (ret.empty()) { + ret = fs::GetRecursiveDirs(g_settings->get("texture_path")); + g_texturedirs_cache.set(ret); + } + return ret; } diff --git a/src/client/texturepaths.h b/src/client/texturepaths.h index 599f637a1..b24a23447 100644 --- a/src/client/texturepaths.h +++ b/src/client/texturepaths.h @@ -7,7 +7,7 @@ #include #include -// Texture paths get cached and this clears the Cache. +// Texture paths get cached and this clears the cache. void clearTextureNameCache(); // Find out the full path of an image by trying different filename extensions. diff --git a/src/server.cpp b/src/server.cpp index 7648a43ba..3cf02a59e 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -268,7 +268,6 @@ Server::Server( m_gamespec(gamespec), m_simple_singleplayer_mode(simple_singleplayer_mode), m_dedicated(dedicated), - m_async_fatal_error(""), m_con(con::createMTP(CONNECTION_TIMEOUT, m_bind_addr.isIPv6(), this)), m_itemdef(createItemDefManager()), m_nodedef(createNodeDefManager()), diff --git a/src/util/container.h b/src/util/container.h index 50a57fc08..88cb772d2 100644 --- a/src/util/container.h +++ b/src/util/container.h @@ -105,7 +105,11 @@ public: return result; } - void clear() { m_values.clear(); } + void clear() + { + MutexAutoLock lock(m_mutex); + m_values.clear(); + } private: std::map m_values; diff --git a/src/util/thread.h b/src/util/thread.h index 857a341be..68e9aa1ee 100644 --- a/src/util/thread.h +++ b/src/util/thread.h @@ -15,12 +15,13 @@ template class MutexedVariable { public: + // default initialization + MutexedVariable() {} + MutexedVariable(const T &value): - m_value(value) - {} + m_value(value) {} MutexedVariable(T &&value): - m_value(std::move(value)) - {} + m_value(std::move(value)) {} T get() {