Skip to content

Conversation

@cameron-robey
Copy link
Contributor

Fix a copy on write issue in QuiescingHelper that led to significant performance problems when the number of channels became large, or connection churn.

Motivation:

When using QuiescingHelper, every channel holds onto a reference to ChannelCollector, which leads to copy on write behaviour for openChannels[]. This has performance issues when a large number of channels are open (e.g. 25k channels can be hundreds of KB), and every time a channel is opened, or closed this is copied. As accepting channels happens on a single NIO thread, this work is all done on that thread, and can lead to saturation, and no more channels being accepted.

Modifications:

Move the state into a non copyable LifecycleStateMachine, to avoid the copies.

Result:

Overhead from ChannelCollector disappears (as seen in traces from http://github.com/apple/swift-profile-recorder).

@weissi
Copy link
Member

weissi commented Nov 28, 2025

CC @glbrntt / @adam-fowler / @0xTim / @Joannis This should give gRPC, Vapor and Hummingbird a speed boost when accepting and holding lots of connections. Currently, accepting connections can cause CoWs on a dictionary holding all active connections. So this can be pretty bad!

Fantastic work @cameron-robey finding and fixing this!!

@weissi weissi requested a review from Lukasa November 28, 2025 20:54
@weissi weissi added the 🔨 semver/patch No public API change. label Nov 28, 2025
@Lukasa
Copy link
Contributor

Lukasa commented Dec 1, 2025

@cameron-robey Looks like there's a consuming annotation that we threw in for minor refcounting benefits that the older compilers are struggling with. I recommend throwing that away for now, should fix up the builds.

Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

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

Very nice, thanks @cameron-robey!

@Lukasa Lukasa merged commit a461345 into apple:main Dec 4, 2025
37 checks passed
@cameron-robey cameron-robey deleted the car/fix-quiescing-cow branch December 4, 2025 11:06
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants