Skip to content
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

Merged
merged 1 commit into from
Feb 5, 2018
Merged

[stdlib/Sema] Minor improvements to synthesized hashValue #14386

merged 1 commit into from
Feb 5, 2018

Conversation

xwu
Copy link
Collaborator

@xwu xwu commented Feb 3, 2018

This PR makes two minor improvements to the synthesized implementation of hashValue.

The manual implementation of DoubleWidth.hashValue is adjusted to match its synthesized counterparts.

@xwu xwu requested a review from lorentey February 3, 2018 06:19
@xwu
Copy link
Collaborator Author

xwu commented Feb 3, 2018

cc @allevato

@xwu
Copy link
Collaborator Author

xwu commented Feb 3, 2018

@swift-ci Please smoke test

@allevato
Copy link
Member

allevato commented Feb 3, 2018

LGTM!

let magic = 0x9e3779b9 as UInt
#elseif arch(x86_64) || arch(arm64) || arch(powerpc64) || arch(powerpc64le) || arch(s390x)
let magic = 0x9e3779b97f4a7c15 as UInt
#endif
Copy link
Member

@natecook1000 natecook1000 Feb 3, 2018

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.

Copy link
Collaborator Author

@xwu xwu Feb 3, 2018

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.

Copy link
Member

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.

Copy link
Collaborator Author

@xwu xwu Feb 3, 2018

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.

Copy link
Member

@lorentey lorentey Feb 5, 2018

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. 🙈)

Copy link
Collaborator Author

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.

Copy link
Member

@lorentey lorentey left a 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
Copy link
Member

@lorentey lorentey Feb 5, 2018

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. 🙈)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants