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

Not cause a CoW when calling ByteBuffer.discardReadBytes() while it wa… #363

Merged
merged 5 commits into from
Apr 27, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 7 additions & 0 deletions Sources/NIO/ByteBuffer-core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,13 @@ public struct ByteBuffer {
return false
}

if self._readerIndex == self._writerIndex {
// If the whole buffer was consumed we can just reset the readerIndex and writerIndex to 0 and move on.
self._moveWriterIndex(to: 0)
self._moveReaderIndex(to: 0)
return true
}

if isKnownUniquelyReferenced(&self._storage) {
self._storage.bytes.advanced(by: Int(self._slice.lowerBound))
.copyMemory(from: self._storage.bytes.advanced(by: Int(self._slice.lowerBound + self._readerIndex)),
Expand Down
1 change: 1 addition & 0 deletions Tests/NIOTests/ByteBufferTest+XCTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ extension ByteBufferTest {
("testByteBufferFitsInACoupleOfEnums", testByteBufferFitsInACoupleOfEnums),
("testLargeSliceBegin16MBIsOkayAndDoesNotCopy", testLargeSliceBegin16MBIsOkayAndDoesNotCopy),
("testLargeSliceBeginMoreThan16MBIsOkay", testLargeSliceBeginMoreThan16MBIsOkay),
("testDiscardReadBytesOnConsumedBuffer", testDiscardReadBytesOnConsumedBuffer),
]
}
}
Expand Down
15 changes: 15 additions & 0 deletions Tests/NIOTests/ByteBufferTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1316,6 +1316,21 @@ class ByteBufferTest: XCTestCase {
XCTAssertEqual(0xdd, slice.getInteger(at: slice.writerIndex - 1, as: UInt8.self))
}

func testDiscardReadBytesOnConsumedBuffer() {
var buffer = self.allocator.buffer(capacity: 8)
buffer.write(integer: 0xaa, as: UInt8.self)
XCTAssertEqual(1, buffer.readableBytes)
XCTAssertEqual(0xaa, buffer.readInteger(as: UInt8.self))
XCTAssertEqual(0, buffer.readableBytes)

var buffer2 = buffer
XCTAssertTrue(buffer.discardReadBytes())
XCTAssertEqual(0, buffer.readerIndex)
XCTAssertEqual(0, buffer.writerIndex)
// As we fully consumed the buffer we should only have adjusted the indices but not triggered a copy as result of CoW sematics.
// So we should still be able to also read the old data.
XCTAssertEqual(0xaa, buffer.getInteger(at: 0, as: UInt8.self))
}
}

private enum AllocationExpectationState: Int {
Expand Down