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

Remove a few unnecessary allocations. #243

Merged
merged 2 commits into from
Sep 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -250,6 +250,9 @@ private struct DataBuffer {
return self.bufferedChunks.hasMark
}

/// An empty buffer, we use this avoid an allocation in 'evacuatePendingWrites'.
private static let emptyBuffer = MarkedCircularBuffer<BufferElement>(initialCapacity: 0)

init() {
self.bufferedChunks = MarkedCircularBuffer(initialCapacity: 8)
self.flushedBufferedBytes = 0
@@ -307,7 +310,7 @@ private struct DataBuffer {

/// Removes all pending writes, invalidating this structure as it does so.
mutating func evacuatePendingWrites() -> MarkedCircularBuffer<BufferElement> {
var buffer = MarkedCircularBuffer<BufferElement>(initialCapacity: 0)
var buffer = DataBuffer.emptyBuffer
swap(&buffer, &self.bufferedChunks)
return buffer
}
4 changes: 2 additions & 2 deletions Sources/NIOHTTP2/HTTP2StreamChannel.swift
Original file line number Diff line number Diff line change
@@ -649,8 +649,8 @@ final class HTTP2StreamChannel: Channel, ChannelCore {
private extension HTTP2StreamChannel {
/// Drop all pending reads.
private func dropPendingReads() {
/// To drop all the reads, as we don't need to report it, we just allocate a new buffer of 0 size.
self.pendingReads = CircularBuffer(initialCapacity: 0)
/// We don't need to report the dropped reads, just remove them all.
self.pendingReads.removeAll()
}

/// Deliver all pending reads to the channel.