From df3c6f29a97cb8d4c9ce6b3c7f3bad297729f75e Mon Sep 17 00:00:00 2001 From: jerrrrycho Date: Wed, 2 Dec 2026 08:23:18 +0100 Subject: [PATCH] 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. --- tests/unit/engine_stubs.cpp | 5 ++++- utilities/Globals.cpp | 5 ++++- utilities/Globals.h | 18 +++++++++++++++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/tests/unit/engine_stubs.cpp b/tests/unit/engine_stubs.cpp index 52a503ac5..fae34c5da 100644 --- a/tests/unit/engine_stubs.cpp +++ b/tests/unit/engine_stubs.cpp @@ -19,9 +19,12 @@ // settings --------------------------------------------------------------------------------------- -global_settings &GetGlobalSettings() { +std::atomic 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; } diff --git a/utilities/Globals.cpp b/utilities/Globals.cpp index 4461e8278..260dad8e8 100644 --- a/utilities/Globals.cpp +++ b/utilities/Globals.cpp @@ -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_detail::instance{nullptr}; + +global_settings &global_settings_detail::make() { static global_settings global{}; + instance.store(&global, std::memory_order_release); return global; } diff --git a/utilities/Globals.h b/utilities/Globals.h index 0a47cd13d..9da132b8d 100644 --- a/utilities/Globals.h +++ b/utilities/Globals.h @@ -15,6 +15,7 @@ http://mozilla.org/MPL/2.0/. #include "rendering/light.h" #include "utilities/utilities.h" #include "utilities/motiontelemetry.h" +#include #include #include @@ -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 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())