Skip to content

Commit fac9736

Browse files
author
MarcoFalke
committed
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.
1 parent 9cb9651 commit fac9736

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

cmake/script/GenerateHeaderFromJson.cmake

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
file(READ ${JSON_SOURCE_PATH} hex_content HEX)
66
string(REGEX MATCHALL "([A-Za-z0-9][A-Za-z0-9])" bytes "${hex_content}")
77

8-
file(WRITE ${HEADER_PATH} "#include <string>\n")
8+
file(WRITE ${HEADER_PATH} "#include <string_view>\n")
99
file(APPEND ${HEADER_PATH} "namespace json_tests{\n")
1010
get_filename_component(json_source_basename ${JSON_SOURCE_PATH} NAME_WE)
11-
file(APPEND ${HEADER_PATH} "static const std::string ${json_source_basename}{\n")
11+
file(APPEND ${HEADER_PATH} "inline constexpr char detail_${json_source_basename}_bytes[]{\n")
1212

1313
set(i 0)
1414
foreach(byte ${bytes})
@@ -21,4 +21,6 @@ foreach(byte ${bytes})
2121
endif()
2222
endforeach()
2323

24-
file(APPEND ${HEADER_PATH} "\n};};")
24+
file(APPEND ${HEADER_PATH} "\n};\n")
25+
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)};")
26+
file(APPEND ${HEADER_PATH} "\n}")

src/test/util/json.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
// Copyright (c) 2023 The Bitcoin Core developers
1+
// Copyright (c) 2023-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <test/util/json.h>
66

7-
#include <string>
7+
#include <univalue.h>
88
#include <util/check.h>
99

10-
#include <univalue.h>
10+
#include <string_view>
1111

12-
UniValue read_json(const std::string& jsondata)
12+
UniValue read_json(std::string_view jsondata)
1313
{
1414
UniValue v;
1515
Assert(v.read(jsondata) && v.isArray());

src/test/util/json.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
// Copyright (c) 2023 The Bitcoin Core developers
1+
// Copyright (c) 2023-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#ifndef BITCOIN_TEST_UTIL_JSON_H
66
#define BITCOIN_TEST_UTIL_JSON_H
77

8-
#include <string>
9-
108
#include <univalue.h>
119

12-
UniValue read_json(const std::string& jsondata);
10+
#include <string_view>
11+
12+
UniValue read_json(std::string_view jsondata);
1313

1414
#endif // BITCOIN_TEST_UTIL_JSON_H

0 commit comments

Comments
 (0)