File: optimization-tips.md

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (33 lines) | stat: -rw-r--r-- 1,441 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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/main/docs/OptimizationTips.rst
[docs-memory-layout]: https://developer.apple.com/documentation/swift/memorylayout