Skip to content

Commit 2a58114

Browse files
committed
build: Minimize I/O operations in GenerateHeaderFromJson.cmake
Tested the performance with: > 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
1 parent aa003d1 commit 2a58114

File tree

1 file changed

+14
-18
lines changed

1 file changed

+14
-18
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}")

0 commit comments

Comments
 (0)