Improve GetJson perf

This commit is contained in:
Paulius Gervė 2025-02-27 20:14:23 +02:00
parent 64180472e2
commit da1fc5788c

View file

@ -45,6 +45,14 @@ inline std::optional<std::string> get_env_utf8(const std::string& name) {
inline const nlohmann::json& GetJson() { inline const nlohmann::json& GetJson() {
static const nlohmann::json jsonConfig = []() { static const nlohmann::json jsonConfig = []() {
static bool initialized = false;
static nlohmann::json config;
if (initialized) {
return config;
}
initialized = true;
std::string jsonString; std::string jsonString;
int index = 1; int index = 1;
@ -63,16 +71,20 @@ inline const nlohmann::json& GetJson() {
if (originalConfig) jsonString = *originalConfig; if (originalConfig) jsonString = *originalConfig;
} }
if (jsonString.empty()) return nlohmann::json{}; if (jsonString.empty()) {
config = nlohmann::json{};
return config;
}
// Validate // Validate
if (!nlohmann::json::accept(jsonString)) { if (!nlohmann::json::accept(jsonString)) {
printf_stderr("ERROR: Invalid JSON passed to CAMOU_CONFIG!\n"); printf_stderr("ERROR: Invalid JSON passed to CAMOU_CONFIG!\n");
return nlohmann::json{}; config = nlohmann::json{};
return config;
} }
nlohmann::json result = nlohmann::json::parse(jsonString); config = nlohmann::json::parse(jsonString);
return result; return config;
}(); }();
return jsonConfig; return jsonConfig;
} }