-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
[stdlib/Sema] Minor improvements to synthesized hashValue #14386
Conversation
…eHashValues magic number
cc @allevato |
@swift-ci Please smoke test |
LGTM! |
let magic = 0x9e3779b9 as UInt | ||
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) | ||
let magic = 0x9e3779b97f4a7c15 as UInt | ||
#endif |
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.
Would it make sense to have compiler directives like #if archbits(32) ... #elseif archbits(64) ... #endif
? Seems problematic/error prone to list individual architectures.
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.
We can already write this as if MemoryLayout<Int>.size == 4
, etc., and the compiler takes care of optimizing it. But here I'm following the prevailing style in this particular file.
Edit: Alternatively, a simplification which could be done down the road is to align ourselves with the assumption (already made elsewhere in the project) that only i386
and arm
are 32-bit.
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.
Is it smart enough not to complain that the 64-bit value is out of bounds on 32-bit platforms if you write that?
I'm not critiquing what you did here — more just wondering how this could be more clear. Totally possible that this is basically only an issue in places like this in the standard library.
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.
That’s a good point; the code may not actually compile on a 32-bit system depending on compiler smarts. I do know it’s optimized down to a constant on my 64-bit system.
I think, ultimately, the utility of another compilation conditional will come down to whether or not we realistically expect Swift to support 32–bit architectures other than i386 and arm. It’s not entirely out of the question but does seem pretty unlikely in the near term.
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.
A dedicated directive would certainly express the underlying intention better, even if we never add support for additional architectures. We already have _endian(little)
/_endian(big)
for endianness.
In any case, for now, we could replace the #elseif
with #else
here; the compiler will complain if the constant doesn't fit in an UInt
on some new 32-bit architecture. (It seems unnecessarily fragile/verbose to list all supported 64-bit architectures, although it is arguably the right choice in the rest of the file. We did go with #else
in the new String
implementation, although that may have just been due to laziness on my part. 🙈)
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 agree that it's a pragmatic solution for the time being. I'll create a follow-up PR with that change.
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.
Nice!
let magic = 0x9e3779b9 as UInt | ||
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x) | ||
let magic = 0x9e3779b97f4a7c15 as UInt | ||
#endif |
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.
A dedicated directive would certainly express the underlying intention better, even if we never add support for additional architectures. We already have _endian(little)
/_endian(big)
for endianness.
In any case, for now, we could replace the #elseif
with #else
here; the compiler will complain if the constant doesn't fit in an UInt
on some new 32-bit architecture. (It seems unnecessarily fragile/verbose to list all supported 64-bit architectures, although it is arguably the right choice in the rest of the file. We did go with #else
in the new String
implementation, although that may have just been due to laziness on my part. 🙈)
This PR makes two minor improvements to the synthesized implementation of
hashValue
._mixInt
call is removed._combineHashValues
, a 64-bit magic number is used instead of a 32-bit magic number on 64-bit architectures.The manual implementation of
DoubleWidth.hashValue
is adjusted to match its synthesized counterparts.