18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-03 06:35:21 +01:00

perf(physics): the settings read through a kept pointer, not a function-local static

Global expanded to a call of GetGlobalSettings(), which made the settings in a function-local static. The guard of that static was not inlined: each use of Global cost a call and a look at the thread's storage, and Global is read many times in the step of every vehicle (2 to 2.5 % of the main thread on Linia 107).

The settings are still made on the first call, so the static objects of the other files get them whatever order they are made in. From then on a pointer to them is kept in an atomic, and the inline GetGlobalSettings() only loads it.
This commit is contained in:
jerrrrycho
2026-12-02 08:23:18 +01:00
parent e625fa4a24
commit df3c6f29a9
3 changed files with 25 additions and 3 deletions
+4 -1
View File
@@ -19,9 +19,12 @@
// settings ---------------------------------------------------------------------------------------
global_settings &GetGlobalSettings() {
std::atomic<global_settings *> global_settings_detail::instance{nullptr};
global_settings &global_settings_detail::make() {
// defaults, as before reading eu07.ini
static global_settings settings;
instance.store(&settings, std::memory_order_release);
return settings;
}
+4 -1
View File
@@ -2966,8 +2966,11 @@ template <> void global_settings::export_as_text(std::ostream &Output, std::stri
Output << Key << " " << (Value ? "yes" : "no") << "\n";
}
global_settings &GetGlobalSettings()
std::atomic<global_settings *> global_settings_detail::instance{nullptr};
global_settings &global_settings_detail::make()
{
static global_settings global{};
instance.store(&global, std::memory_order_release);
return global;
}
+17 -1
View File
@@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/.
#include "rendering/light.h"
#include "utilities/utilities.h"
#include "utilities/motiontelemetry.h"
#include <atomic>
#include <map>
#include <optional>
@@ -874,6 +875,21 @@ inline std::string_view name_of( global_settings::derailment_model const Model )
}
}
extern global_settings& GetGlobalSettings();
// the settings are made on the first call of GetGlobalSettings(), from the static objects of the other files too, whatever
// the order they're made in. after that a pointer to them is kept, which the calls read: the guard of a function-local
// static took a call and a look at the thread's storage for each use of Global, many times in the step of every vehicle
namespace global_settings_detail
{
// zero until the settings are made: set before any other thread starts
extern std::atomic<global_settings *> instance;
// makes the settings on the first call, keeps the pointer to them
global_settings &make();
} // namespace global_settings_detail
inline global_settings &GetGlobalSettings()
{
auto *const instance{global_settings_detail::instance.load(std::memory_order_acquire)};
return instance != nullptr ? *instance : global_settings_detail::make();
}
#define Global (GetGlobalSettings())