From fac973647d69dd14089cee9972e8dfa0074c8a97 Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Tue, 3 Sep 2024 10:31:13 +0200 Subject: [PATCH] test: Use string_view for json_tests This avoids a static constructor of the global std::string, and rules out possibly expensive and implicit copies of the string completely. --- cmake/script/GenerateHeaderFromJson.cmake | 8 +++++--- src/test/util/json.cpp | 8 ++++---- src/test/util/json.h | 8 ++++---- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/cmake/script/GenerateHeaderFromJson.cmake b/cmake/script/GenerateHeaderFromJson.cmake index 279ceedf044..53d11652728 100644 --- a/cmake/script/GenerateHeaderFromJson.cmake +++ b/cmake/script/GenerateHeaderFromJson.cmake @@ -5,10 +5,10 @@ file(READ ${JSON_SOURCE_PATH} hex_content HEX) string(REGEX MATCHALL "([A-Za-z0-9][A-Za-z0-9])" bytes "${hex_content}") -file(WRITE ${HEADER_PATH} "#include \n") +file(WRITE ${HEADER_PATH} "#include \n") file(APPEND ${HEADER_PATH} "namespace json_tests{\n") get_filename_component(json_source_basename ${JSON_SOURCE_PATH} NAME_WE) -file(APPEND ${HEADER_PATH} "static const std::string ${json_source_basename}{\n") +file(APPEND ${HEADER_PATH} "inline constexpr char detail_${json_source_basename}_bytes[]{\n") set(i 0) foreach(byte ${bytes}) @@ -21,4 +21,6 @@ foreach(byte ${bytes}) endif() endforeach() -file(APPEND ${HEADER_PATH} "\n};};") +file(APPEND ${HEADER_PATH} "\n};\n") +file(APPEND ${HEADER_PATH} "inline constexpr std::string_view ${json_source_basename}{std::begin(detail_${json_source_basename}_bytes), std::end(detail_${json_source_basename}_bytes)};") +file(APPEND ${HEADER_PATH} "\n}") diff --git a/src/test/util/json.cpp b/src/test/util/json.cpp index ad3c346c84e..46a4a9f9a19 100644 --- a/src/test/util/json.cpp +++ b/src/test/util/json.cpp @@ -1,15 +1,15 @@ -// Copyright (c) 2023 The Bitcoin Core developers +// Copyright (c) 2023-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include -#include +#include #include -#include +#include -UniValue read_json(const std::string& jsondata) +UniValue read_json(std::string_view jsondata) { UniValue v; Assert(v.read(jsondata) && v.isArray()); diff --git a/src/test/util/json.h b/src/test/util/json.h index 5b1026762e6..f6f4e6ab71b 100644 --- a/src/test/util/json.h +++ b/src/test/util/json.h @@ -1,14 +1,14 @@ -// Copyright (c) 2023 The Bitcoin Core developers +// Copyright (c) 2023-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_TEST_UTIL_JSON_H #define BITCOIN_TEST_UTIL_JSON_H -#include - #include -UniValue read_json(const std::string& jsondata); +#include + +UniValue read_json(std::string_view jsondata); #endif // BITCOIN_TEST_UTIL_JSON_H