refactor: Extract MIB_BYTES constant for init.cpp#25386
refactor: Extract MIB_BYTES constant for init.cpp#25386Empact wants to merge 4 commits intobitcoin:masterfrom
Conversation
|
Concept ACK
Right, not sure |
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
brunoerg
left a comment
There was a problem hiding this comment.
Concept ACK
It's easier to read and maintain the code this way imo.
w0xlt
left a comment
There was a problem hiding this comment.
Approach ACK
Adding the unit makes the code more readable (in the same way that using std::chrono instead of int makes the code better)
src/init.cpp
Outdated
There was a problem hiding this comment.
This will force all places where this is used to uint64_t, even if they are supposed to be signed integers. Not sure, but maybe a #define or int type is better, so that the sign of the surrounding code is chosen?
There was a problem hiding this comment.
Ouch, good point. I'm starting to doubt this change now.
There was a problem hiding this comment.
Thanks @MarcoFalke, this was indeed surprising to me:
Otherwise, if the unsigned operand's conversion rank is greater or equal to the conversion rank of the signed operand, the signed operand is converted to the unsigned operand's type.
https://en.cppreference.com/w/cpp/language/operator_arithmetic#Conversions
There was a problem hiding this comment.
I updated the PR to use int32_t for these constants, on the theory that int being the smallest value for an unsuffixed literal field, these values would be equivalent to the inline arithmetic.
https://en.cppreference.com/w/cpp/language/integer_literal#The_type_of_the_literal
There was a problem hiding this comment.
Though I maintain uint64_t for GB_BYTES to maintain protections against overflow in PruneGBtoMiB.
There was a problem hiding this comment.
In an effort to validate these concerns, I ran build with -Wsign-conversion and -Wsign-compare, with the new and old code, and did not see any warnings relative to MIB_BYTES, MIN_PRUNE_TARGET_GB, or _FOR_BLOCK_FILES. I also reviewed each impacted line and did not identify such issues.
Prior, uint64_t code here: https://github.com/Empact/bitcoin/commits/2022-06-mib-bytes-sign-conversion-test
Current, int32_t code here: https://github.com/Empact/bitcoin/commits/2022-06-mib-bytes-sign-conversion
60236bf to
d58422b
Compare
Co-authored-by: benthecarman <[email protected]> Co-authored-by: Justin Litchfield <[email protected]> Co-authored-by: Liran Cohen <[email protected]> Co-authored-by: Ryan Loomba <[email protected]> Co-authored-by: Buck Perley <[email protected]> Co-authored-by: bajjer <[email protected]> Co-authored-by: Suhail Saqan <[email protected]> Co-authored-by: Christopher Sweeney <[email protected]> Co-authored-by: Alyssa <[email protected]> Co-authored-by: Ben Schroth <[email protected]> Co-authored-by: Jason Hester <[email protected]> Co-authored-by: Matt Clough <[email protected]> Co-authored-by: Elise Schedler <[email protected]> Co-authored-by: ghander <[email protected]> Co-authored-by: PopeLaz <[email protected]>
98b8176 to
6b64215
Compare
|
cc @mzumsande |
This requires creating a non-gui analog to guiconstants.h, and lifting GB/MIB_BYTES and MIN_*_FOR_BLOCK_FILES into it.
6b64215 to
86e1d59
Compare
mzumsande
left a comment
There was a problem hiding this comment.
Concept ACK.
git grep "1024 / 1024" shows a few more spots that could make use of the constant.
| AX_CHECK_COMPILE_FLAG([-Wunreachable-code-loop-increment], [WARN_CXXFLAGS="$WARN_CXXFLAGS -Wunreachable-code-loop-increment"], [], [$CXXFLAG_WERROR]) | ||
| AX_CHECK_COMPILE_FLAG([-Wimplicit-fallthrough], [WARN_CXXFLAGS="$WARN_CXXFLAGS -Wimplicit-fallthrough"], [], [$CXXFLAG_WERROR]) | ||
| AX_CHECK_COMPILE_FLAG([-Wsign-compare], [WARN_CXXFLAGS="$WARN_CXXFLAGS -Wsign-compare"], [], [$CXXFLAG_WERROR]) | ||
| AX_CHECK_COMPILE_FLAG([-Wsign-conversion], [WARN_CXXFLAGS="$WARN_CXXFLAGS -Wsign-conversion"], [], [$CXXFLAG_WERROR]) |
There was a problem hiding this comment.
This leads to lots of warnings all over the place for me (unrelated to MIB_BYTES). I don't think it should be part of this PR:
| static constexpr uint64_t GB_BYTES{1000000000}; | ||
|
|
||
| /* One mebibyte (MiB) in bytes */ | ||
| static constexpr int32_t MIB_BYTES{1024 * 1024}; |
There was a problem hiding this comment.
How about introducing this right from the beginning? It's a bit weird that MIB_BYTES jumps files with each commit, from init.cpp to validation.h to its final destination constants.h.
|
🐙 This pull request conflicts with the target branch and needs rebase. |
|
Closing this as it has not had any activity in a while. If you are interested in continuing work on this, please leave a comment so that it can be reopened. |
This is a common concept which is more communicative and explicit if articulated, IMO.
This could be applied elsewhere if there is a better place to reference it in common, but I don't see it.
Pulled from #25315 to ease review there.