Skip to content

Improve NIOAsyncChannel performance with UnsafeContinuations#3521

Open
fabianfett wants to merge 6 commits intoapple:mainfrom
fabianfett:ff-NIOUnsafeContinuation
Open

Improve NIOAsyncChannel performance with UnsafeContinuations#3521
fabianfett wants to merge 6 commits intoapple:mainfrom
fabianfett:ff-NIOUnsafeContinuation

Conversation

@fabianfett
Copy link
Member

Let's improve NIOAsyncChannels performance, by using UnsafeContinuations instead of CheckedContinuations.

Motivation:

We want to improve the performance for our async APIs.

Modifications:

  • Introduce a new typealias NIOUnsafeContinuation that behaves like a UnsafeContinuation in release mode and like a CheckedContinuation in debug mode.
  • Use the NIOUnsafeContinuation inside the NIOAsyncWriter
  • Use the NIOUnsafeContinuation inside the NIOThrowingAsyncSequenceProducer

Result:

Fewer allocations

@fabianfett fabianfett added the 🔨 semver/patch No public API change. label Feb 24, 2026
Copy link
Member

@FranzBusch FranzBusch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I want @Lukasa to review this though to ensure he is fine with moving over.

@fabianfett fabianfett force-pushed the ff-NIOUnsafeContinuation branch from 43dec89 to 89f2d04 Compare February 24, 2026 15:47
Comment on lines +16 to +20
/// A Swift Continuation that behaves like a `CheckedContinuation` in Debug mode
/// and like a `UnsafeContinuation` in release mode.
///
/// - Note: Only use this for code paths that have proven to be safe for at least one year.
/// All usages must have a comment that states, why it is safe to use NIOUnsafeContinuation.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably want this on the withNIOUnsafe<Blah> as well

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Happy to copy the comment around, but do we really need it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, yes. The with method is the point-of-use. When people are quick-looking at the docs associated with the message they should be presented with this caveat.

(continuation: CheckedContinuation<StateMachine.YieldResult, Error>) in
//
// We switched to using NIOUnsafeContinuation after running with
// CheckedContinuatuion for more than a year. Now we use NIOUnsafeContinuation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Normally I'm not that nitpicky about typos but this one got copypasta'd everywhere:

Suggested change
// CheckedContinuatuion for more than a year. Now we use NIOUnsafeContinuation
// CheckedContinuation for more than a year. Now we use NIOUnsafeContinuation
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 semver/patch No public API change.

5 participants