-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
src: get rid of fp arithmetic in ParseIPv4Host #46326
Merged
nodejs-github-bot
merged 1 commit into
nodejs:main
from
tniessen:src-fp-arithmetic-parseipv4host
Jan 26, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Out of curiosity, how does someone choose between
unsigned int
anduint32_t
?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.
Taste, basically. There's no difference unless you're targeting Watcom C++ for DOS, where
sizeof(int) == 2
.Of course Real Programmers(TM) don't care for repeating themselves and just write
unsigned
without theint
.This method is really quite something. Tobias cleans it up but it still looks super complicated. The code below is untested and off the cuff but I think that is what ParseIPv4Host's logic reduces to. The only thing I'm not completely sure about is whether e.g.
http://00000000000000000000/
(over 19 chars) is considered a valid input.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.
tl;dr: personally, I read
unsigned int
as "some smallish non-negative integer", whereasuint32_t
makes me question why this variable has to be exactly 32 bits.Ad-hoc thought process:
Let's assume that there is no obvious type with better semantics for this use case (e.g.,
size_t
). Otherwise, that type should likely be used instead.❌ If the variable must have the same size across all platforms,
uint32_t
is the logical choice (even though ILP32, LLP64, and LP64 always use 32-bitunsigned int
anyway, and only more exotic architectures such as ILP64 or SILP64 deviate).❌ If the algorithm requires or benefits from a specific size, then
uint32_t
is again the logical choice. This is particularly important if the algorithm relies on unsigned integer overflows (which are well-defined, unlike signed overflows).❌ If the use case requires a fixed minimum size, then
uint_fast32_t
can be useful. However, the implementation might end up quietly relying on a specific type underlyinguint_fast32_t
.uint_fast32_t
. The implementation might store values exceeding 32 bits in this variable, which will break if anyone tries to use the code on an architecture that uses a 32-bit type foruint_fast32_t
.uint_fast32_t
, then assigning auint_fast32_t
value to a 32-bit variable works, but it will break if anyone tries to use the code on an architecture that uses a 64-bit type foruint_fast32_t
.❌ If the variable is mostly used to interact with some library, the type should match whatever the library uses. This is often true for OpenSSL, which for historic reasons frequently uses signed
int
values instead of semantically more appropriate types.✔️ If none of that is true, and if the 16-bit minimum size of
unsigned int
is clearly sufficient, thenunsigned int
works just fine. Within Node.js, we can even rely on 32-bitunsigned int
, but that's not necessarily true in general, e.g., on AVR.Of course, following this argument,
unsigned short
would work just as well since it is also guaranteed to have a minimum size of 16 bits. However, aside from the (potentially) smaller size when allocating large arrays (e.g.,unsigned short[16 * 1024]
may be smaller thanunsigned int[16 * 1024]
),unsigned short
has no benefit overunsigned int
. It may even be slower since modern CPUs prefersizeof(unsigned int)
orsizeof(unsigned long)
registers, and may have to mask the upper parts of those registers for computations onunsigned short
. In fact,uint_fast16_t
usually is either the same asuint32_t
oruint64_t
.Regardless, what is important to me is the signedness of these variables. I know that this is not a widespread opinion, but to me, "signedness correctness" is almost as important as const correctness.