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.
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.
This avoids a static constructor of the global std::string, and rules
out possibly expensive and implicit copies of the string completely.
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
For detailed information about the code coverage, see the test coverage report.
See the guideline for information on the review process.
Type | Reviewers |
---|---|
ACK | TheCharlatan, fjahr, stickies-v, hebasto |
Concept ACK | BrandonOdiwuor |
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.
4@@ -5,10 +5,10 @@
5 file(READ ${JSON_SOURCE_PATH} hex_content HEX)
6 string(REGEX MATCHALL "([A-Za-z0-9][A-Za-z0-9])" bytes "${hex_content}")
7
8-file(WRITE ${HEADER_PATH} "#include <string>\n")
9+file(WRITE ${HEADER_PATH} "#include <string_view>\n")
10 file(APPEND ${HEADER_PATH} "namespace json_tests{\n")
11 get_filename_component(json_source_basename ${JSON_SOURCE_PATH} NAME_WE)
12-file(APPEND ${HEADER_PATH} "static const std::string ${json_source_basename}{\n")
13+file(APPEND ${HEADER_PATH} "inline constexpr char detail_${json_source_basename}_bytes[]{\n")
detail_
prefix mean?
What does the
detail_
prefix mean?
It is just the lazy version of namespace detail
, see https://duckduckgo.com/?q=namespace+detail or git grep "namespace detail"
to denote internal details that should not be considered exposed.
Happy to wrap the symbol into a namespace detail
properly.
52 {
53+ int vector_size(source.size() * 8);
54 std::vector<bool> result(vector_size);
55 for (int byte_i = 0; byte_i < vector_size / 8; ++byte_i) {
56- unsigned char cur_byte = source[byte_i];
57+ uint8_t cur_byte{uint8_t(source[byte_i])};
std::to_integer
here? It does the same thing, but maybe it would be good to use it for its compile-time guarantees.
Sure, added compile time check that ensures std::is_integral_v<uint8_t>
.
Also, fixed typo in commit message.
Should be trivial to re-ACK
Approach ACK
This looks good, just left a small question, and there is a typo at the start of the second commit’s message.
This change allows to drop brittle sizeof calls in favor of the
std::span::size method.
Other improvements include:
* Use of a namespace to mark test and bench data
* Use of the modern std::byte
* Drop of a no longer used std::vector copy and the bench/data module
re-ACK https://github.com/bitcoin/bitcoin/commit/faecca9a85c1c1917d024f55cca34d19cc94f3b9
Only changes since last review were usage of std::to_integer
and some shuffling of newline characters.
46@@ -47,11 +47,12 @@ static CService ResolveService(const std::string& ip, uint16_t port = 0)
47 }
48
49
50-static std::vector<bool> FromBytes(const unsigned char* source, int vector_size)
51+static std::vector<bool> FromBytes(std::span<const std::byte> source)
52 {
53+ int vector_size(source.size() * 8);
nit: I think the code is more clear without this helper variable, avoids the first multiplying and then dividing by 8 again:
0diff --git a/src/test/addrman_tests.cpp b/src/test/addrman_tests.cpp
1index da6ff77924..fd7a1f2ba2 100644
2--- a/src/test/addrman_tests.cpp
3+++ b/src/test/addrman_tests.cpp
4@@ -49,11 +49,10 @@ static CService ResolveService(const std::string& ip, uint16_t port = 0)
5
6 static std::vector<bool> FromBytes(std::span<const std::byte> source)
7 {
8- int vector_size(source.size() * 8);
9- std::vector<bool> result(vector_size);
10- for (int byte_i = 0; byte_i < vector_size / 8; ++byte_i) {
11+ std::vector<bool> result(source.size() * 8);
12+ for (size_t byte_i = 0; byte_i < source.size(); ++byte_i) {
13 uint8_t cur_byte{std::to_integer<uint8_t>(source[byte_i])};
14- for (int bit_i = 0; bit_i < 8; ++bit_i) {
15+ for (size_t bit_i = 0; bit_i < 8; ++bit_i) {
16 result[byte_i * 8 + bit_i] = (cur_byte >> bit_i) & 1;
17 }
18 }
ACK faecca9a85c1c1917d024f55cca34d19cc94f3b9
Commit messages were very helpful, thank you.