Skip to content

Commit 6852d1d

Browse files
committed
Merge bitcoin/bitcoin#30796: test: Use std::span and std::string_view for raw data
faecca9 test: Use span for raw data (MarcoFalke) fac9736 test: Use string_view for json_tests (MarcoFalke) Pull request description: The build system converts raw data into a C++ header file for tests. This change modernizes the code to use the convenience wrappers `std::span` and `std::string_view`, so that redundant copies can be avoided. ACKs for top commit: fjahr: re-ACK bitcoin/bitcoin@faecca9 TheCharlatan: ACK faecca9 stickies-v: ACK faecca9 hebasto: ACK faecca9, I have reviewed the code and the generated headers. Tree-SHA512: 1f4951c54aff11ba27c41fb70f2821bdb79e06ca0abae734b970bd0d64dda9d8cced824a891fd51b3e9d4e5715ee9eb49ed5d369010a45eca7c3bec9f8641235
2 parents fa05ee0 + faecca9 commit 6852d1d

15 files changed

+37
-65
lines changed

cmake/module/GenerateHeaders.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ function(generate_header_from_json json_source_relpath)
1111
)
1212
endfunction()
1313

14-
function(generate_header_from_raw raw_source_relpath)
14+
function(generate_header_from_raw raw_source_relpath raw_namespace)
1515
add_custom_command(
1616
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${raw_source_relpath}.h
17-
COMMAND ${CMAKE_COMMAND} -DRAW_SOURCE_PATH=${CMAKE_CURRENT_SOURCE_DIR}/${raw_source_relpath} -DHEADER_PATH=${CMAKE_CURRENT_BINARY_DIR}/${raw_source_relpath}.h -P ${PROJECT_SOURCE_DIR}/cmake/script/GenerateHeaderFromRaw.cmake
17+
COMMAND ${CMAKE_COMMAND} -DRAW_SOURCE_PATH=${CMAKE_CURRENT_SOURCE_DIR}/${raw_source_relpath} -DHEADER_PATH=${CMAKE_CURRENT_BINARY_DIR}/${raw_source_relpath}.h -DRAW_NAMESPACE=${raw_namespace} -P ${PROJECT_SOURCE_DIR}/cmake/script/GenerateHeaderFromRaw.cmake
1818
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/${raw_source_relpath} ${PROJECT_SOURCE_DIR}/cmake/script/GenerateHeaderFromRaw.cmake
1919
VERBATIM
2020
)

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}")

cmake/script/GenerateHeaderFromRaw.cmake

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,23 @@
55
file(READ ${RAW_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 <cstddef>\n")
9+
file(APPEND ${HEADER_PATH} "#include <span>\n")
10+
file(APPEND ${HEADER_PATH} "namespace ${RAW_NAMESPACE} {\n")
811
get_filename_component(raw_source_basename ${RAW_SOURCE_PATH} NAME_WE)
9-
file(WRITE ${HEADER_PATH} "static unsigned const char ${raw_source_basename}_raw[] = {\n")
12+
file(APPEND ${HEADER_PATH} "inline constexpr std::byte detail_${raw_source_basename}_raw[]{\n")
1013

1114
set(i 0)
1215
foreach(byte ${bytes})
1316
math(EXPR i "${i} + 1")
1417
math(EXPR remainder "${i} % 8")
1518
if(remainder EQUAL 0)
16-
file(APPEND ${HEADER_PATH} "0x${byte},\n")
19+
file(APPEND ${HEADER_PATH} "std::byte{0x${byte}},\n")
1720
else()
18-
file(APPEND ${HEADER_PATH} "0x${byte}, ")
21+
file(APPEND ${HEADER_PATH} "std::byte{0x${byte}}, ")
1922
endif()
2023
endforeach()
2124

22-
file(APPEND ${HEADER_PATH} "\n};")
25+
file(APPEND ${HEADER_PATH} "\n};\n")
26+
file(APPEND ${HEADER_PATH} "inline constexpr std::span ${raw_source_basename}{detail_${raw_source_basename}_raw};\n")
27+
file(APPEND ${HEADER_PATH} "}")

src/bench/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,11 @@
33
# file COPYING or https://opensource.org/license/mit/.
44

55
include(GenerateHeaders)
6-
generate_header_from_raw(data/block413567.raw)
6+
generate_header_from_raw(data/block413567.raw benchmark::data)
77

88
add_executable(bench_bitcoin
99
bench_bitcoin.cpp
1010
bench.cpp
11-
data.cpp
1211
nanobench.cpp
1312
${CMAKE_CURRENT_BINARY_DIR}/data/block413567.raw.h
1413
# Benchmarks:

src/bench/checkblock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <bench/bench.h>
6-
#include <bench/data.h>
6+
#include <bench/data/block413567.raw.h>
77
#include <chainparams.h>
88
#include <common/args.h>
99
#include <consensus/validation.h>

src/bench/data.cpp

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/bench/data.h

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/bench/load_external.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// file COPYING or https://www.opensource.org/licenses/mit-license.php.
44

55
#include <bench/bench.h>
6-
#include <bench/data.h>
6+
#include <bench/data/block413567.raw.h>
77
#include <chainparams.h>
88
#include <flatfile.h>
99
#include <node/blockstorage.h>

src/bench/readblock.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <bench/bench.h>
6-
#include <bench/data.h>
6+
#include <bench/data/block413567.raw.h>
77
#include <flatfile.h>
88
#include <node/blockstorage.h>
99
#include <primitives/block.h>

src/bench/rpc_blockchain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

55
#include <bench/bench.h>
6-
#include <bench/data.h>
6+
#include <bench/data/block413567.raw.h>
77
#include <chain.h>
88
#include <core_io.h>
99
#include <primitives/block.h>

0 commit comments

Comments
 (0)