Skip to content

Commit 55cca34

Browse files
committed
Cache getTextureDirs()
1 parent 0b9ae73 commit 55cca34

7 files changed

Lines changed: 37 additions & 59 deletions

File tree

src/client/game.cpp

Lines changed: 11 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -863,46 +863,17 @@ Game::Game() :
863863
m_chat_log_buf(g_logger),
864864
m_game_ui(new GameUI())
865865
{
866-
g_settings->registerChangedCallback("chat_log_level",
867-
&settingChangedCallback, this);
868-
g_settings->registerChangedCallback("doubletap_jump",
869-
&settingChangedCallback, this);
870-
g_settings->registerChangedCallback("toggle_sneak_key",
871-
&settingChangedCallback, this);
872-
g_settings->registerChangedCallback("toggle_aux1_key",
873-
&settingChangedCallback, this);
874-
g_settings->registerChangedCallback("enable_joysticks",
875-
&settingChangedCallback, this);
876-
g_settings->registerChangedCallback("enable_fog",
877-
&settingChangedCallback, this);
878-
g_settings->registerChangedCallback("mouse_sensitivity",
879-
&settingChangedCallback, this);
880-
g_settings->registerChangedCallback("joystick_frustum_sensitivity",
881-
&settingChangedCallback, this);
882-
g_settings->registerChangedCallback("repeat_place_time",
883-
&settingChangedCallback, this);
884-
g_settings->registerChangedCallback("repeat_dig_time",
885-
&settingChangedCallback, this);
886-
g_settings->registerChangedCallback("noclip",
887-
&settingChangedCallback, this);
888-
g_settings->registerChangedCallback("free_move",
889-
&settingChangedCallback, this);
890-
g_settings->registerChangedCallback("fog_start",
891-
&settingChangedCallback, this);
892-
g_settings->registerChangedCallback("cinematic",
893-
&settingChangedCallback, this);
894-
g_settings->registerChangedCallback("cinematic_camera_smoothing",
895-
&settingChangedCallback, this);
896-
g_settings->registerChangedCallback("camera_smoothing",
897-
&settingChangedCallback, this);
898-
g_settings->registerChangedCallback("invert_mouse",
899-
&settingChangedCallback, this);
900-
g_settings->registerChangedCallback("enable_hotbar_mouse_wheel",
901-
&settingChangedCallback, this);
902-
g_settings->registerChangedCallback("invert_hotbar_mouse_wheel",
903-
&settingChangedCallback, this);
904-
g_settings->registerChangedCallback("pause_on_lost_focus",
905-
&settingChangedCallback, this);
866+
clearTextureNameCache();
867+
868+
const char *settings[] = {
869+
"chat_log_level", "doubletap_jump", "toggle_sneak_key", "toggle_aux1_key",
870+
"enable_joysticks", "enable_fog", "mouse_sensitivity", "joystick_frustum_sensitivity",
871+
"repeat_place_time", "repeat_dig_time", "noclip", "free_move", "fog_start",
872+
"cinematic", "cinematic_camera_smoothing", "camera_smoothing", "invert_mouse",
873+
"enable_hotbar_mouse_wheel", "invert_hotbar_mouse_wheel", "pause_on_lost_focus",
874+
};
875+
for (auto s : settings)
876+
g_settings->registerChangedCallback(s, &settingChangedCallback, this);
906877

907878
readSettings();
908879
}
@@ -4254,11 +4225,6 @@ void the_game(volatile std::sig_atomic_t *kill,
42544225
{
42554226
Game game;
42564227

4257-
/* Make a copy of the server address because if a local singleplayer server
4258-
* is created then this is updated and we don't want to change the value
4259-
* passed to us by the calling function
4260-
*/
4261-
42624228
try {
42634229

42644230
if (game.startup(kill, input, rendering_engine, start_data,

src/client/imagesource.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ bool ImageSource::generateImagePart(std::string_view part_of_name,
11131113
<< " for [combine" << std::endl;
11141114
continue;
11151115
}
1116-
infostream << "Adding \"" << filename<< "\" to combined "
1116+
tracestream << "Adding \"" << filename << "\" to combined "
11171117
<< pos_base << std::endl;
11181118

11191119
video::IImage *img = generateImage(filename, source_image_names);

src/client/texturepaths.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,21 @@
55
#include "texturepaths.h"
66

77
#include "util/container.h"
8+
#include "util/thread.h"
89
#include "settings.h"
910
#include "filesys.h"
1011
#include "porting.h"
1112
#include <vector>
1213

1314
// A cache from texture name to texture path
1415
static MutexedMap<std::string, std::string> g_texturename_to_path_cache;
16+
// Cached result of getTextureDirs()
17+
static MutexedVariable<std::vector<std::string>> g_texturedirs_cache;
1518

1619
void clearTextureNameCache()
1720
{
1821
g_texturename_to_path_cache.clear();
22+
g_texturedirs_cache.set({});
1923
}
2024

2125
// 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)
7478
break;
7579
}
7680

77-
// Check from default data directory i.e. .../minetest/textures/base/pack
81+
// Check from default data directory
7882
if (fullpath.empty()) {
79-
std::string base_path = porting::path_share + DIR_DELIM + "textures"
80-
+ DIR_DELIM + "base" + DIR_DELIM + "pack";
83+
std::string base_path = porting::path_share + DIR_DELIM "textures"
84+
DIR_DELIM "base" DIR_DELIM "pack";
8185
// Check all filename extensions. Returns "" if not found.
8286
fullpath = getImagePath(base_path + DIR_DELIM + filename);
8387
if (is_base_pack && !fullpath.empty())
8488
*is_base_pack = true;
8589
}
8690

87-
// Add to cache (also an empty result is cached)
91+
// Add to cache (an empty result is cached too)
8892
g_texturename_to_path_cache.set(filename, fullpath);
8993

90-
// Finally return it
9194
return fullpath;
9295
}
9396

9497
std::vector<std::string> getTextureDirs()
9598
{
96-
return fs::GetRecursiveDirs(g_settings->get("texture_path"));
99+
std::vector<std::string> ret = g_texturedirs_cache.get();
100+
if (ret.empty()) {
101+
ret = fs::GetRecursiveDirs(g_settings->get("texture_path"));
102+
g_texturedirs_cache.set(ret);
103+
}
104+
return ret;
97105
}

src/client/texturepaths.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <string>
88
#include <vector>
99

10-
// Texture paths get cached and this clears the Cache.
10+
// Texture paths get cached and this clears the cache.
1111
void clearTextureNameCache();
1212

1313
// Find out the full path of an image by trying different filename extensions.

src/server.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,6 @@ Server::Server(
268268
m_gamespec(gamespec),
269269
m_simple_singleplayer_mode(simple_singleplayer_mode),
270270
m_dedicated(dedicated),
271-
m_async_fatal_error(""),
272271
m_con(con::createMTP(CONNECTION_TIMEOUT, m_bind_addr.isIPv6(), this)),
273272
m_itemdef(createItemDefManager()),
274273
m_nodedef(createNodeDefManager()),

src/util/container.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,11 @@ class MutexedMap
105105
return result;
106106
}
107107

108-
void clear() { m_values.clear(); }
108+
void clear()
109+
{
110+
MutexAutoLock lock(m_mutex);
111+
m_values.clear();
112+
}
109113

110114
private:
111115
std::map<Key, Value> m_values;

src/util/thread.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ template<typename T>
1515
class MutexedVariable
1616
{
1717
public:
18+
// default initialization
19+
MutexedVariable() {}
20+
1821
MutexedVariable(const T &value):
19-
m_value(value)
20-
{}
22+
m_value(value) {}
2123
MutexedVariable(T &&value):
22-
m_value(std::move(value))
23-
{}
24+
m_value(std::move(value)) {}
2425

2526
T get()
2627
{

0 commit comments

Comments
 (0)