Skip to content

SwiftQUIC: PERF: Reduce exclusive access checks by 1.1 gigacycles in the send path - #114

Open
agnosticdev wants to merge 6 commits into
mainfrom
agnosticdev/ReduceExclusivity
Open

SwiftQUIC: PERF: Reduce exclusive access checks by 1.1 gigacycles in the send path#114
agnosticdev wants to merge 6 commits into
mainfrom
agnosticdev/ReduceExclusivity

Conversation

@agnosticdev

Copy link
Copy Markdown
Collaborator

Swift has something called exclusive access checks, its part of what makes Swift a memory safe language. These exclusive access checks happen when accessing properties of a class or other reference type to make sure something else is not accessing and mutating that property at the same time. These exclusive access checks end up costing a very small amount of CPU when a property of a reference type is used. Well, in the data path (millions of times) these very small amounts of CPU add up to becoming a lot of CPU. One thing you can do to avoid these costs and still keep your code safe is to hoist these properties up to the top of the call stack and pass them down through the stack. This ends up satisfying the ownership rules and avoids redundant exclusive access checks. And that's what this change does, it rewrites the send path to reduce calling QUICConnection properties directly on the send path, and instead, passes these properties down through the send path. This change reduces the CPU usage by over 1.1 gigacycles.

Top of tree:

4.05 G 100.0%	-	 swift_beginAccess
775.78 M 100.0%	-	 swift_endAccess

With this change:

3.16 G 99.4%	-	 swift_beginAccess	
493.56 M 99.8%	-	  swift_endAccess

@agnosticdev
agnosticdev requested a review from glbrntt August 24, 2026 19:14
@agnosticdev agnosticdev added the 🔨 semver/patch No public API change. label Aug 24, 2026
path.pathStatistics[.txPackets] += 1
path.pathStatistics[.txBytes] += Int(totalPacketLength)
}
path.pathStatistics[.txPackets] += 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why don't path statistics work the same way as connection statistics? path.stats.increment(.txPackets)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think I forgot to change path stats when I initially overhauled this (before this pr). Overhauled them in connection. Addressed 9ba1c9f

@agnosticdev
agnosticdev requested a review from ekinnear as a code owner August 26, 2026 22:52
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