Skip to content

Commit aa003d1

Browse files
committed
build: Minimize I/O operations in GenerateHeaderFromRaw.cmake
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`. Tested the output with `diff -w` and they're the same - only whitespace differences because slightly different source formatting. Tested the performance with: > 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
1 parent e46bebb commit aa003d1

File tree

1 file changed

+15
-19
lines changed

1 file changed

+15
-19
lines changed

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)