-
Notifications
You must be signed in to change notification settings - Fork 38.7k
refactor: Extract MIB_BYTES constant for init.cpp #25386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ouch, good point. I'm starting to doubt this change now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though I maintain uint64_t for GB_BYTES to maintain protections against overflow in PruneGBtoMiB.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 <benthecarman@live.com> Co-authored-by: Justin Litchfield <litch@me.com> Co-authored-by: Liran Cohen <c.liran.c@gmail.com> Co-authored-by: Ryan Loomba <ryan.loomba@gmail.com> Co-authored-by: Buck Perley <bucko.perley@gmail.com> Co-authored-by: bajjer <bajjer@bajjer.xyz> Co-authored-by: Suhail Saqan <suhail.saqan@gmail.com> Co-authored-by: Christopher Sweeney <sweeney.chris@gmail.com> Co-authored-by: Alyssa <orbitalturtle@protonmail.com> Co-authored-by: Ben Schroth <ben@styng.social> Co-authored-by: Jason Hester <mail@jason-hester.me> Co-authored-by: Matt Clough <Matt.clough@pm.me> Co-authored-by: Elise Schedler <eliseschedler@gmail.com> Co-authored-by: ghander <cen254@gmail.com> Co-authored-by: PopeLaz <btclz@fastmail.com>
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.