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.
<!--e57a25ab6845829454e8d69fc972939a-->
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
<!--006a51241073e994b41acfe9ec718e94-->
For detailed information about the code coverage, see the test coverage report.
<!--021abf342d371248e50ceaed478a90ca-->
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.
Concept ACK.
On my system the test_bitcoin and bench_bitcoin binaries are smaller by 30kB, and 4kB respectively. But that isn't the goal of this pull, just a nice side-effect.
Concept ACK
Concept ACK
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")
What does the 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.
Ok, I hadn't come across this yet. I don't have a preference, you can leave it as is.
Code review ACK fa0321cc742754af4e5822d4227d4136a6f4f467
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])};
Would it better to use 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
ACK faecca9a85c1c1917d024f55cca34d19cc94f3b9
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:
<details> <summary>git diff on faecca9a85</summary>
diff --git a/src/test/addrman_tests.cpp b/src/test/addrman_tests.cpp
index da6ff77924..fd7a1f2ba2 100644
--- a/src/test/addrman_tests.cpp
+++ b/src/test/addrman_tests.cpp
@@ -49,11 +49,10 @@ static CService ResolveService(const std::string& ip, uint16_t port = 0)
static std::vector<bool> FromBytes(std::span<const std::byte> source)
{
- int vector_size(source.size() * 8);
- std::vector<bool> result(vector_size);
- for (int byte_i = 0; byte_i < vector_size / 8; ++byte_i) {
+ std::vector<bool> result(source.size() * 8);
+ for (size_t byte_i = 0; byte_i < source.size(); ++byte_i) {
uint8_t cur_byte{std::to_integer<uint8_t>(source[byte_i])};
- for (int bit_i = 0; bit_i < 8; ++bit_i) {
+ for (size_t bit_i = 0; bit_i < 8; ++bit_i) {
result[byte_i * 8 + bit_i] = (cur_byte >> bit_i) & 1;
}
}
</details>
Will push if I have to re-touch.
ACK faecca9a85c1c1917d024f55cca34d19cc94f3b9
Commit messages were very helpful, thank you.
ACK faecca9a85c1c1917d024f55cca34d19cc94f3b9, I have reviewed the code and the generated headers.