From 62fd8729d3cfec62dc76ea143ef4b6c13b35af22 Mon Sep 17 00:00:00 2001 From: Kamil Borzym Date: Thu, 31 Oct 2019 09:05:51 +0100 Subject: [PATCH] `ByteBuffer.clear` should reset the `ByteBuffer._slice`. (#1212) Motivation: ByteBuffer.clear doesn't reset the ByteBuffer._slice. Imagine having a byte buffer slice (ByteBuffer.getSlice). Calling ByteBuffer.clear on the slice will reset the reader and writer, but the ByteBuffer.capacity will return the old slice size, not the full storage capacity. After this change ByteBuffer.clear will also reset the ByteBuffer._slice to regain the whole ByteBuffer._storage. Modifications: Just setting the full storage slice in the ByteBuffer.clear method. Result: After that change ByteBuffer.clear will reset byte buffer slice to full capacity of underlying storage. --- Sources/NIO/ByteBuffer-core.swift | 1 + Tests/NIOTests/ByteBufferTest+XCTest.swift | 2 ++ Tests/NIOTests/ByteBufferTest.swift | 34 +++++++++++++++++++--- 3 files changed, 33 insertions(+), 4 deletions(-) diff --git a/Sources/NIO/ByteBuffer-core.swift b/Sources/NIO/ByteBuffer-core.swift index b34b312c7..07085f024 100644 --- a/Sources/NIO/ByteBuffer-core.swift +++ b/Sources/NIO/ByteBuffer-core.swift @@ -663,6 +663,7 @@ public struct ByteBuffer { if !isKnownUniquelyReferenced(&self._storage) { self._storage = self._storage.allocateStorage() } + self._slice = self._storage.fullSlice self._moveWriterIndex(to: 0) self._moveReaderIndex(to: 0) } diff --git a/Tests/NIOTests/ByteBufferTest+XCTest.swift b/Tests/NIOTests/ByteBufferTest+XCTest.swift index a35d15913..dc7cee8c1 100644 --- a/Tests/NIOTests/ByteBufferTest+XCTest.swift +++ b/Tests/NIOTests/ByteBufferTest+XCTest.swift @@ -107,6 +107,8 @@ extension ByteBufferTest { ("testWritableBytesAccountsForSlicing", testWritableBytesAccountsForSlicing), ("testClearDupesStorageIfTheresTwoBuffersSharingStorage", testClearDupesStorageIfTheresTwoBuffersSharingStorage), ("testClearDoesNotDupeStorageIfTheresOnlyOneBuffer", testClearDoesNotDupeStorageIfTheresOnlyOneBuffer), + ("testClearResetsTheSliceCapacityIfTheresOnlyOneBuffer", testClearResetsTheSliceCapacityIfTheresOnlyOneBuffer), + ("testClearResetsTheSliceCapacityIfTheresTwoSlicesSharingStorage", testClearResetsTheSliceCapacityIfTheresTwoSlicesSharingStorage), ("testWeUseFastWriteForContiguousCollections", testWeUseFastWriteForContiguousCollections), ("testUnderestimatingSequenceWorks", testUnderestimatingSequenceWorks), ("testZeroSizeByteBufferResizes", testZeroSizeByteBufferResizes), diff --git a/Tests/NIOTests/ByteBufferTest.swift b/Tests/NIOTests/ByteBufferTest.swift index c0ca0fd9c..f03229e73 100644 --- a/Tests/NIOTests/ByteBufferTest.swift +++ b/Tests/NIOTests/ByteBufferTest.swift @@ -238,7 +238,7 @@ class ByteBufferTest: XCTestCase { func testWithMutableWritePointerWithMinimumSpecifiedAdjustsCapacity() { XCTAssertEqual(0, buf.writerIndex) - XCTAssertEqual(512, buf.capacity) + XCTAssertEqual(1024, buf.capacity) var bytesWritten = buf.writeWithUnsafeMutableBytes(minimumWritableBytes: 256) { XCTAssertTrue($0.count >= 256) @@ -246,7 +246,7 @@ class ByteBufferTest: XCTestCase { } XCTAssertEqual(256, bytesWritten) XCTAssertEqual(256, buf.writerIndex) - XCTAssertEqual(512, buf.capacity) + XCTAssertEqual(1024, buf.capacity) bytesWritten += buf.writeWithUnsafeMutableBytes(minimumWritableBytes: 1024) { XCTAssertTrue($0.count >= 1024) @@ -260,7 +260,7 @@ class ByteBufferTest: XCTestCase { func testWithMutableWritePointerWithMinimumSpecifiedWhileAtMaxCapacity() { XCTAssertEqual(0, buf.writerIndex) - XCTAssertEqual(512, buf.capacity) + XCTAssertEqual(1024, buf.capacity) var bytesWritten = buf.writeWithUnsafeMutableBytes(minimumWritableBytes: 512) { XCTAssertTrue($0.count >= 512) @@ -268,7 +268,7 @@ class ByteBufferTest: XCTestCase { } XCTAssertEqual(512, bytesWritten) XCTAssertEqual(512, buf.writerIndex) - XCTAssertEqual(512, buf.capacity) + XCTAssertEqual(1024, buf.capacity) bytesWritten += buf.writeWithUnsafeMutableBytes(minimumWritableBytes: 1) { XCTAssertTrue($0.count >= 1) @@ -1228,6 +1228,32 @@ class ByteBufferTest: XCTestCase { XCTAssertEqual(bufPtrValPre, bufPtrValPost) } + func testClearResetsTheSliceCapacityIfTheresOnlyOneBuffer() { + let alloc = ByteBufferAllocator() + var buf = alloc.buffer(capacity: 16) + buf.writeString("qwertyuiop") + XCTAssertEqual(buf.capacity, 16) + + var slice = buf.getSlice(at: 3, length: 4)! + XCTAssertEqual(slice.capacity, 4) + + slice.clear() + XCTAssertEqual(slice.capacity, 16) + } + + func testClearResetsTheSliceCapacityIfTheresTwoSlicesSharingStorage() { + let alloc = ByteBufferAllocator() + var buf = alloc.buffer(capacity: 16) + buf.writeString("qwertyuiop") + + var slice1 = buf.getSlice(at: 3, length: 4)! + let slice2 = slice1 + + slice1.clear() + XCTAssertEqual(slice1.capacity, 16) + XCTAssertEqual(slice2.capacity, 4) + } + func testWeUseFastWriteForContiguousCollections() throws { struct WrongCollection: Collection { let storage: [UInt8] = [1, 2, 3]