-
Notifications
You must be signed in to change notification settings - Fork 656
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Improve pointer hygeine in SocketAddresses.swift #1588
Merged
Merged
Conversation
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
Davidde94
approved these changes
Jul 9, 2020
Motivation: The socket address types are some of the worst types for handling in Swift. They pervasively type-pun in a way that Swift strongly wants to resist, and they expose a bunch of data through awkward fixed-length arrays. We've done a number of passes over this code to improve its pointer hygeine. This is another one. Here, we remove some uses of the memory binding APIs that were unnecessary, and that can instead take advantage of the way Swift handles binding memory of homogeneous tuple types. In Swift, whenever we have a homogeneous tuple (i.e. a tuple whose elements are all the same type), that memory is actually bound to _two_ types: to the tuple type, and to the type of the elements. This allows us to use `assumingMemoryBound(to:)` to convert between the pointer to the tuple and the pointer to the first element of the tuple. This knowledge lets us remove calls to `bindMemory` and `withMemoryRebound`. Neither call is correct to use in this situation, and while we were unlikely to encounter any damage, this is the best possible use of the Swift pointer APIs for this work. Modifications: - Rewrite accesses to the UNIX domain socket path to use appropriately typed pointers without binding or rebinding memory. - Added some assertions about the static types of the stored elements. Result: Better management of pointer type bindings.
135cb13
to
dab15b9
Compare
glbrntt
approved these changes
Jul 9, 2020
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.
Looks good. Thanks for useful motivation + comments!
PeterAdams-A
approved these changes
Jul 9, 2020
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.
Yes - agree this is an improvement over previous.
slashmo
pushed a commit
to slashmo/swift-nio
that referenced
this pull request
Aug 18, 2020
Motivation: The socket address types are some of the worst types for handling in Swift. They pervasively type-pun in a way that Swift strongly wants to resist, and they expose a bunch of data through awkward fixed-length arrays. We've done a number of passes over this code to improve its pointer hygeine. This is another one. Here, we remove some uses of the memory binding APIs that were unnecessary, and that can instead take advantage of the way Swift handles binding memory of homogeneous tuple types. In Swift, whenever we have a homogeneous tuple (i.e. a tuple whose elements are all the same type), that memory is actually bound to _two_ types: to the tuple type, and to the type of the elements. This allows us to use `assumingMemoryBound(to:)` to convert between the pointer to the tuple and the pointer to the first element of the tuple. This knowledge lets us remove calls to `bindMemory` and `withMemoryRebound`. Neither call is correct to use in this situation, and while we were unlikely to encounter any damage, this is the best possible use of the Swift pointer APIs for this work. Modifications: - Rewrite accesses to the UNIX domain socket path to use appropriately typed pointers without binding or rebinding memory. - Added some assertions about the static types of the stored elements. Result: Better management of pointer type bindings.
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
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.
Motivation:
The socket address types are some of the worst types for handling in
Swift. They pervasively type-pun in a way that Swift strongly wants to
resist, and they expose a bunch of data through awkward fixed-length
arrays.
We've done a number of passes over this code to improve its pointer
hygeine. This is another one. Here, we remove some uses of the memory
binding APIs that were unnecessary, and that can instead take advantage
of the way Swift handles binding memory of homogeneous tuple types.
In Swift, whenever we have a homogeneous tuple (i.e. a tuple whose
elements are all the same type), that memory is actually bound to two
types: to the tuple type, and to the type of the elements. This allows
us to use
assumingMemoryBound(to:)
to convert between the pointer tothe tuple and the pointer to the first element of the tuple.
This knowledge lets us remove calls to
bindMemory
andwithMemoryRebound
. Neither call is correct to use in this situation,and while we were unlikely to encounter any damage, this is the best
possible use of the Swift pointer APIs for this work.
Modifications:
typed pointers without binding or rebinding memory.
Result:
Better management of pointer type bindings.