Skip to content

Commit 2a0949f

Browse files
committed
Merge bitcoin/bitcoin#30888: build: optimize .h generation in GenerateHeaderFrom{Raw,Json}.cmake
2a58114 build: Minimize I/O operations in GenerateHeaderFromJson.cmake (Lőrinc) aa003d1 build: Minimize I/O operations in GenerateHeaderFromRaw.cmake (Lőrinc) Pull request description: Follow up of the bitcoin/bitcoin#30883 revert. Replaced multiple file writes with a single string template write. The raw content is first grouped into 8 byte chunks, followed by another regex replace which wraps them in `std::byte` or just the raw bytes, prefixed with `0x`. Tested the output with `diff -w` and they're the same - only whitespace differences because slightly different source formatting. ---- Tested the `Raw` performance with: ```bash time cmake -DRAW_SOURCE_PATH=src/bench/data/block413567.raw -DHEADER_PATH=build/after/block413567.raw.h -DRAW_NAMESPACE=benchmark::data -P cmake/script/GenerateHeaderFromRaw.cmake ``` Before: > 15.41s user 23.06s system 97% cpu 39.593 total After: > 0.77s user 0.06s system 97% cpu 0.849 total ---- Tested the `Json` performance with: ```bash time cmake -DJSON_SOURCE_PATH=src/secp256k1/src/wycheproof/ecdsa_secp256k1_sha256_bitcoin_test.json -DHEADER_PATH=build/after/ecdsa_secp256k1_sha256_bitcoin_test.json -P cmake/script/GenerateHeaderFromJson.cmake ```` Before: > 3.57s user 6.01s system 94% cpu 10.136 total After: > 0.17s user 0.01s system 98% cpu 0.187 total ACKs for top commit: maflcko: review ACK 2a58114 👒 hebasto: ACK 2a58114. willcl-ark: tACK 2a58114 Tree-SHA512: 5e44f79d1c0dbb61d8b64f28d4c3c87a176981f72104b28800eef2037b0728076cbcf14ff07b05ff94d4e8800605586cfd5df00519db9027933c5943348c01d2
2 parents bdbc90f + 2a58114 commit 2a0949f

File tree

2 files changed

+29
-37
lines changed

2 files changed

+29
-37
lines changed

cmake/script/GenerateHeaderFromJson.cmake

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,21 @@
22
# Distributed under the MIT software license, see the accompanying
33
# file COPYING or https://opensource.org/license/mit/.
44

5+
cmake_path(GET JSON_SOURCE_PATH STEM json_source_basename)
6+
57
file(READ ${JSON_SOURCE_PATH} hex_content HEX)
6-
string(REGEX MATCHALL "([A-Za-z0-9][A-Za-z0-9])" bytes "${hex_content}")
8+
string(REGEX REPLACE "................" "\\0\n" formatted_bytes "${hex_content}")
9+
string(REGEX REPLACE "[^\n][^\n]" "0x\\0, " formatted_bytes "${formatted_bytes}")
710

8-
file(WRITE ${HEADER_PATH} "#include <string_view>\n")
9-
file(APPEND ${HEADER_PATH} "namespace json_tests{\n")
10-
get_filename_component(json_source_basename ${JSON_SOURCE_PATH} NAME_WE)
11-
file(APPEND ${HEADER_PATH} "inline constexpr char detail_${json_source_basename}_bytes[]{\n")
11+
set(header_content
12+
"#include <string_view>
1213
13-
set(i 0)
14-
foreach(byte ${bytes})
15-
math(EXPR i "${i} + 1")
16-
math(EXPR remainder "${i} % 8")
17-
if(remainder EQUAL 0)
18-
file(APPEND ${HEADER_PATH} "0x${byte},\n")
19-
else()
20-
file(APPEND ${HEADER_PATH} "0x${byte}, ")
21-
endif()
22-
endforeach()
14+
namespace json_tests {
15+
inline constexpr char detail_${json_source_basename}_bytes[] {
16+
${formatted_bytes}
17+
};
2318
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}")
19+
inline constexpr std::string_view ${json_source_basename}{std::begin(detail_${json_source_basename}_bytes), std::end(detail_${json_source_basename}_bytes)};
20+
}
21+
")
22+
file(WRITE ${HEADER_PATH} "${header_content}")

cmake/script/GenerateHeaderFromRaw.cmake

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,22 @@
22
# Distributed under the MIT software license, see the accompanying
33
# file COPYING or https://opensource.org/license/mit/.
44

5+
cmake_path(GET RAW_SOURCE_PATH STEM raw_source_basename)
6+
57
file(READ ${RAW_SOURCE_PATH} hex_content HEX)
6-
string(REGEX MATCHALL "([A-Za-z0-9][A-Za-z0-9])" bytes "${hex_content}")
8+
string(REGEX REPLACE "................" "\\0\n" formatted_bytes "${hex_content}")
9+
string(REGEX REPLACE "[^\n][^\n]" "std::byte{0x\\0}, " formatted_bytes "${formatted_bytes}")
710

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")
11-
get_filename_component(raw_source_basename ${RAW_SOURCE_PATH} NAME_WE)
12-
file(APPEND ${HEADER_PATH} "inline constexpr std::byte detail_${raw_source_basename}_raw[]{\n")
11+
set(header_content
12+
"#include <cstddef>
13+
#include <span>
1314
14-
set(i 0)
15-
foreach(byte ${bytes})
16-
math(EXPR i "${i} + 1")
17-
math(EXPR remainder "${i} % 8")
18-
if(remainder EQUAL 0)
19-
file(APPEND ${HEADER_PATH} "std::byte{0x${byte}},\n")
20-
else()
21-
file(APPEND ${HEADER_PATH} "std::byte{0x${byte}}, ")
22-
endif()
23-
endforeach()
15+
namespace ${RAW_NAMESPACE} {
16+
inline constexpr std::byte detail_${raw_source_basename}_raw[] {
17+
${formatted_bytes}
18+
};
2419
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} "}")
20+
inline constexpr std::span ${raw_source_basename}{detail_${raw_source_basename}_raw};
21+
}
22+
")
23+
file(WRITE ${HEADER_PATH} "${header_content}")

0 commit comments

Comments
 (0)