Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep capacity when dropping pending reads in the stream channel #447

Merged
merged 2 commits into from
Jul 8, 2024

Conversation

glbrntt
Copy link
Contributor

@glbrntt glbrntt commented Jul 5, 2024

Motivation:

When the stream channel has closed it drops all of its pending reads. It does this by calling 'removeAll()' on its CircularBuffer. The buffers underlying storage is emptied and then a nil is appended (internally CircularBuffer is never empty) which triggers an allocation.

Modifications:

  • Remove all but keep capacity

Result:

Fewer allocations

glbrntt added 2 commits July 5, 2024 15:23
Motivation:

When the stream channel has closed it drops all of its pending reads. It
does this by calling 'removeAll()' on its CircularBuffer. The buffers
underlying storage is emptied and then a `nil` is appended (internally
CircularBuffer is never empty) which triggers an allocation.

Modifications:

- Remove all but keep capacity

Result:

Fewer allocations
@glbrntt glbrntt added the 🔨 semver/patch No public API change. label Jul 5, 2024
@glbrntt glbrntt requested a review from gjcairo July 5, 2024 15:58
@glbrntt glbrntt marked this pull request as ready for review July 5, 2024 15:58
@glbrntt glbrntt enabled auto-merge (squash) July 5, 2024 15:58
@@ -728,7 +728,7 @@ private extension HTTP2StreamChannel {
/// Drop all pending reads.
private func dropPendingReads() {
/// We don't need to report the dropped reads, just remove them all.
self.pendingReads.removeAll()
self.pendingReads.removeAll(keepingCapacity: true)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we worry about this potentially keeping an unneeded/too-big capacity for the duration of the stream?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, this is only called when the channel is closing.

@glbrntt glbrntt merged commit ac2a15c into apple:main Jul 8, 2024
6 of 7 checks passed
@glbrntt glbrntt deleted the avoid-realloc-on-remove-all branch July 8, 2024 13:51
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.

2 participants