From 3fa5c20e9bd8d91fe960dd4b0e19b512787eb56b Mon Sep 17 00:00:00 2001 From: Tobias Loew Date: Sun, 22 Mar 2026 16:10:51 +0100 Subject: [PATCH] make _binary arrays constexpr and use string_view instead of string (#2692) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * make _binary arrays constexpr and use string_view instead of string By making _binary arrays constexpr and using string_view instead of string, startup speed of the library is improved. * fixed interfaces for string_view added /bigobj /MP to MSVC CXX-flags * empty commit to trigger CI * empty commit to force CI build --------- Co-authored-by: Löw, Tobias --- CMakeLists.txt | 2 +- dev/generate_headers.py | 4 ++-- include/rapidjson_include.h | 10 +++++----- src/Backends/Cubics/CubicsLibrary.cpp | 6 +++--- src/Backends/Cubics/CubicsLibrary.h | 4 ++-- src/Backends/Helmholtz/MixtureParameters.cpp | 12 ++++++------ .../Incompressible/IncompressibleLibrary.cpp | 2 +- src/Backends/PCSAFT/PCSAFTLibrary.cpp | 10 +++++----- src/Backends/PCSAFT/PCSAFTLibrary.h | 6 +++--- src/CoolProp.cpp | 4 ++-- 10 files changed, 30 insertions(+), 30 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4960d960..902016ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -322,7 +322,7 @@ list(APPEND APP_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/externals/fmtlib" if (MSVC) # fmtlib requires that the utf-8 support be compiled in # TODO: add the fmt target from fmtlib directly which does this - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8 -D_CRT_SECURE_NO_WARNINGS") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj /MP /utf-8 -D_CRT_SECURE_NO_WARNINGS") endif() list(APPEND APP_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/include") list(APPEND APP_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/src") diff --git a/dev/generate_headers.py b/dev/generate_headers.py index f4bbba06..28c69937 100644 --- a/dev/generate_headers.py +++ b/dev/generate_headers.py @@ -176,9 +176,9 @@ def TO_CPP(root_dir, hashes): # Generate the output string output = '// File generated by the script dev/generate_headers.py on ' + str(datetime.now()) + '\n\n' output += '// JSON file encoded in binary form\n' - output += 'const unsigned char ' + variable + '_binary[] = {\n' + hex_string + '\n};' + '\n\n' + output += 'static constexpr char ' + variable + '_binary[] = {\n' + hex_string + '\n};' + '\n\n' output += '// Combined into a single std::string \n' - output += 'std::string {v:s}({v:s}_binary, {v:s}_binary + sizeof({v:s}_binary)/sizeof({v:s}_binary[0]));'.format(v=variable) + output += 'static constexpr std::string_view {v:s}(std::begin({v:s}_binary), std::size({v:s}_binary));'.format(v=variable) # Write it to file f = open(os.path.join(root_dir, 'include', outfile), 'w') diff --git a/include/rapidjson_include.h b/include/rapidjson_include.h index 39a4c267..7e3815b4 100644 --- a/include/rapidjson_include.h +++ b/include/rapidjson_include.h @@ -310,19 +310,19 @@ enum schema_validation_code /** * Validate a JSON-formatted string against a JSON-formatted schema string */ -inline schema_validation_code validate_schema(const std::string& schemaJson, const std::string& inputJson, std::string& errstr) { +inline schema_validation_code validate_schema(const std::string_view& schemaJson, const std::string_view& inputJson, std::string& errstr) { rapidjson::Document sd; - sd.Parse(schemaJson.c_str()); + sd.Parse(schemaJson.data(), schemaJson.size()); if (sd.HasParseError()) { - errstr = format("Invalid schema: %s\n", schemaJson.c_str()); + errstr = format("Invalid schema: %s\n", schemaJson); return SCHEMA_INVALID_JSON; } rapidjson::SchemaDocument schema(sd); // Compile a Document to SchemaDocument rapidjson::Document d; - d.Parse(inputJson.c_str()); + d.Parse(inputJson.data(), inputJson.size()); if (d.HasParseError()) { - errstr = format("Invalid input json: %s\n", inputJson.c_str()); + errstr = format("Invalid input json: %s\n", inputJson); return INPUT_INVALID_JSON; } diff --git a/src/Backends/Cubics/CubicsLibrary.cpp b/src/Backends/Cubics/CubicsLibrary.cpp index 093199c6..03c7f288 100644 --- a/src/Backends/Cubics/CubicsLibrary.cpp +++ b/src/Backends/Cubics/CubicsLibrary.cpp @@ -140,7 +140,7 @@ class CubicsLibraryClass }; static CubicsLibraryClass library; -void add_fluids_as_JSON(const std::string& JSON) { +void add_fluids_as_JSON(const std::string_view& JSON) { // First we validate the json string against the schema; std::string errstr; cpjson::schema_validation_code val_code = cpjson::validate_schema(cubic_fluids_schema_JSON, JSON, errstr); @@ -148,7 +148,7 @@ void add_fluids_as_JSON(const std::string& JSON) { if (val_code == cpjson::SCHEMA_VALIDATION_OK) { rapidjson::Document dd; - dd.Parse<0>(JSON.c_str()); + dd.Parse<0>(JSON.data(), JSON.size()); if (dd.HasParseError()) { throw ValueError("Cubics JSON is not valid JSON"); } else { @@ -169,7 +169,7 @@ std::string get_fluid_as_JSONstring(const std::string& identifier) { CubicLibrary::CubicsValues get_cubic_values(const std::string& identifier) { return library.get(identifier); } -std::string get_cubic_fluids_schema() { +std::string_view get_cubic_fluids_schema() { return cubic_fluids_schema_JSON; } std::string get_cubic_fluids_list() { diff --git a/src/Backends/Cubics/CubicsLibrary.h b/src/Backends/Cubics/CubicsLibrary.h index 84469524..865eb46f 100644 --- a/src/Backends/Cubics/CubicsLibrary.h +++ b/src/Backends/Cubics/CubicsLibrary.h @@ -41,10 +41,10 @@ CubicsValues get_cubic_values(const std::string& identifier); /** \brief Add an array of fluids to the cubics library (as a JSON-formatted string) * @param JSON A JSON-formatted string with the fluid information */ -void add_fluids_as_JSON(const std::string& JSON); +void add_fluids_as_JSON(const std::string_view& JSON); /// Get the schema used to validate the cubic fluids -std::string get_cubic_fluids_schema(); +std::string_view get_cubic_fluids_schema(); /// Get a csv separated list of fluid names that are loaded std::string get_cubic_fluids_list(); diff --git a/src/Backends/Helmholtz/MixtureParameters.cpp b/src/Backends/Helmholtz/MixtureParameters.cpp index c148e1a8..74cf5b63 100644 --- a/src/Backends/Helmholtz/MixtureParameters.cpp +++ b/src/Backends/Helmholtz/MixtureParameters.cpp @@ -19,9 +19,9 @@ class PredefinedMixturesLibrary load_from_string(predefined_mixtures_JSON); } - void load_from_string(const std::string& str) { + void load_from_string(const std::string_view& str) { rapidjson::Document doc; - doc.Parse<0>(str.c_str()); + doc.Parse<0>(str.data(), str.size()); if (doc.HasParseError()) { std::cout << str << std::endl; throw ValueError("Unable to parse predefined mixture string"); @@ -92,9 +92,9 @@ class MixtureBinaryPairLibrary return m_binary_pair_map; }; - void load_from_string(const std::string& str) { + void load_from_string(const std::string_view& str) { rapidjson::Document doc; - doc.Parse<0>(str.c_str()); + doc.Parse<0>(str.data(), str.size()); if (doc.HasParseError()) { std::cout << str << std::endl; throw ValueError("Unable to parse binary interaction function string"); @@ -416,9 +416,9 @@ class MixtureDepartureFunctionsLibrary return m_departure_function_map; }; - void load_from_string(const std::string& str) { + void load_from_string(const std::string_view& str) { rapidjson::Document doc; - doc.Parse<0>(str.c_str()); + doc.Parse<0>(str.data(), str.size()); if (doc.HasParseError()) { std::cout << str << std::endl; throw ValueError("Unable to parse departure function string"); diff --git a/src/Backends/Incompressible/IncompressibleLibrary.cpp b/src/Backends/Incompressible/IncompressibleLibrary.cpp index 7a7221b4..32d7f38c 100644 --- a/src/Backends/Incompressible/IncompressibleLibrary.cpp +++ b/src/Backends/Incompressible/IncompressibleLibrary.cpp @@ -542,7 +542,7 @@ static JSONIncompressibleLibrary library; void load_incompressible_library() { rapidjson::Document dd; // This json formatted string comes from the all_incompressibles_JSON.h header which is a C++-escaped version of the JSON file - dd.Parse<0>(all_incompressibles_JSON.c_str()); + dd.Parse<0>(all_incompressibles_JSON.data(), all_incompressibles_JSON.size()); if (dd.HasParseError()) { throw ValueError("Unable to load all_incompressibles_JSON.json"); } else { diff --git a/src/Backends/PCSAFT/PCSAFTLibrary.cpp b/src/Backends/PCSAFT/PCSAFTLibrary.cpp index f9f1c5d0..018c3b82 100644 --- a/src/Backends/PCSAFT/PCSAFTLibrary.cpp +++ b/src/Backends/PCSAFT/PCSAFTLibrary.cpp @@ -66,7 +66,7 @@ PCSAFTFluid& PCSAFTLibraryClass::get(std::size_t key) { } }; -void add_fluids_as_JSON(const std::string& JSON) { +void add_fluids_as_JSON(const std::string_view& JSON) { // First we validate the json string against the schema; std::string errstr; cpjson::schema_validation_code val_code = cpjson::validate_schema(pcsaft_fluids_schema_JSON, JSON, errstr); @@ -75,7 +75,7 @@ void add_fluids_as_JSON(const std::string& JSON) { if (val_code == cpjson::SCHEMA_VALIDATION_OK) { rapidjson::Document dd; - dd.Parse<0>(JSON.c_str()); + dd.Parse<0>(JSON.data(), JSON.size()); if (dd.HasParseError()) { throw ValueError("Unable to load all_pcsaft_JSON.json"); } else { @@ -196,7 +196,7 @@ int PCSAFTLibraryClass::add_many(rapidjson::Value& listing) { return counter; }; -std::string get_pcsaft_fluids_schema() { +std::string_view get_pcsaft_fluids_schema() { return pcsaft_fluids_schema_JSON; } @@ -373,9 +373,9 @@ void PCSAFTLibraryClass::load_from_JSON(rapidjson::Document& doc) { } } -void PCSAFTLibraryClass::load_from_string(const std::string& str) { +void PCSAFTLibraryClass::load_from_string(const std::string_view& str) { rapidjson::Document doc; - doc.Parse<0>(str.c_str()); + doc.Parse<0>(str.data(), str.size()); if (doc.HasParseError()) { throw ValueError("Unable to parse PC-SAFT binary interaction parameter string"); } diff --git a/src/Backends/PCSAFT/PCSAFTLibrary.h b/src/Backends/PCSAFT/PCSAFTLibrary.h index eede5434..f33eb3cd 100644 --- a/src/Backends/PCSAFT/PCSAFTLibrary.h +++ b/src/Backends/PCSAFT/PCSAFTLibrary.h @@ -26,7 +26,7 @@ class PCSAFTLibraryClass std::map, std::vector> m_binary_pair_map; void load_from_JSON(rapidjson::Document& doc); - void load_from_string(const std::string& str); + void load_from_string(const std::string_view& str); public: PCSAFTLibraryClass(); @@ -51,10 +51,10 @@ class PCSAFTLibraryClass /** \brief Add an array of fluids to the PC-SAFT library (as a JSON-formatted string) * @param JSON A JSON-formatted string with the fluid information */ -void add_fluids_as_JSON(const std::string& JSON); +void add_fluids_as_JSON(const std::string_view& JSON); /// Get the schema used to validate the PC-SAFT fluids -std::string get_pcsaft_fluids_schema(); +std::string_view get_pcsaft_fluids_schema(); PCSAFTLibraryClass& get_library(void); } // namespace PCSAFTLibrary diff --git a/src/CoolProp.cpp b/src/CoolProp.cpp index f43098a6..2ffbeab4 100644 --- a/src/CoolProp.cpp +++ b/src/CoolProp.cpp @@ -1016,11 +1016,11 @@ std::string get_global_param_string(const std::string& ParamName) { } else if (ParamName == "REFPROP_version") { return REFPROPMixtureBackend::version(); } else if (ParamName == "cubic_fluids_schema") { - return CoolProp::CubicLibrary::get_cubic_fluids_schema(); + return std::string{CoolProp::CubicLibrary::get_cubic_fluids_schema()}; } else if (ParamName == "cubic_fluids_list") { return CoolProp::CubicLibrary::get_cubic_fluids_list(); } else if (ParamName == "pcsaft_fluids_schema") { - return CoolProp::PCSAFTLibrary::get_pcsaft_fluids_schema(); + return std::string{CoolProp::PCSAFTLibrary::get_pcsaft_fluids_schema()}; } else { throw ValueError(format("Input parameter [%s] is invalid", ParamName.c_str())); }