-
Notifications
You must be signed in to change notification settings - Fork 656
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an optimization tips document. (#1024)
Motivation: NIO doesn't offer any tips of on how to write performant code. After discussing with @weissi we decided it would be useful to gather and document some useful tips and tricks. Modifications: Started an optimization tips document. Result: Easier for NIO users to write performant code.
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
Swift NIO Optimization Tips | ||
=========================== | ||
|
||
The following document is a list of tips and advice for writing high performance | ||
code using Swift NIO. | ||
|
||
General Optimization Tips | ||
------------------------- | ||
|
||
Although not specific to NIO, the [Swift GitHub repository][swift-repo] has a | ||
list of tips and tricks for [writing high performance Swift code][swift-optimization-tips] | ||
and is a great place to start. | ||
|
||
Wrapping types in `NIOAny` | ||
-------------------------- | ||
|
||
Anything passing through a NIO pipeline that is not special-cased by `NIOAny` | ||
(i.e. is not one of `ByteBuffer`, `FileRegion`, `IOData` or | ||
`AddressedEnvelope<ByteBuffer>`) needs to have careful attention paid to its | ||
size. If the type isn't one of the aforementioned special cases then it must be | ||
no greater than 24 bytes in size to avoid a heap-allocation each time it is | ||
added to a `NIOAny`. The size of a type can be checked using | ||
[`MemoryLayout.size`][docs-memory-layout]. | ||
|
||
If the type being wrapped is a value type then it can be narrowed by placing it | ||
in a copy-on-write box. Alternatively, if the type is an `enum` with associated | ||
data, then it is possible for the associated data to be boxed by the compiler by | ||
making it an `indirect case`. | ||
|
||
|
||
[swift-repo]: https://github.com/apple/swift | ||
[swift-optimization-tips]: https://github.com/apple/swift/blob/master/docs/OptimizationTips.rst | ||
[docs-memory-layout]: https://developer.apple.com/documentation/swift/memorylayout |