diff --git a/IntegrationTests/tests_04_performance/test_01_resources/run-nio-alloc-counter-tests.sh b/IntegrationTests/tests_04_performance/test_01_resources/run-nio-alloc-counter-tests.sh index 8158c1532a..2813f3d6e8 100755 --- a/IntegrationTests/tests_04_performance/test_01_resources/run-nio-alloc-counter-tests.sh +++ b/IntegrationTests/tests_04_performance/test_01_resources/run-nio-alloc-counter-tests.sh @@ -39,7 +39,7 @@ fi "$here/../../allocation-counter-tests-framework/run-allocation-counter.sh" \ -p "$here/../../.." \ - -m NIO -m NIOHTTP1 -m NIOWebSocket \ + -m NIO -m NIOHTTP1 -m NIOWebSocket -m NIOFoundationCompat \ -s "$here/shared.swift" \ -t "$tmp_dir" \ "${tests_to_run[@]}" diff --git a/IntegrationTests/tests_04_performance/test_01_resources/test_bytebuffer_lots_of_rw.swift b/IntegrationTests/tests_04_performance/test_01_resources/test_bytebuffer_lots_of_rw.swift index 62a863543f..2ef7125340 100644 --- a/IntegrationTests/tests_04_performance/test_01_resources/test_bytebuffer_lots_of_rw.swift +++ b/IntegrationTests/tests_04_performance/test_01_resources/test_bytebuffer_lots_of_rw.swift @@ -14,6 +14,7 @@ import Dispatch import NIO +import NIOFoundationCompat func run(identifier: String) { measure(identifier: identifier) { @@ -25,20 +26,19 @@ func run(identifier: String) { @inline(never) func doWrites(buffer: inout ByteBuffer) { /* these ones are zero allocations */ - // buffer.writeBytes(foundationData) // see SR-7542 buffer.writeBytes([0x41]) buffer.writeBytes("A".utf8) buffer.writeString("A") buffer.writeStaticString("A") buffer.writeInteger(0x41, as: UInt8.self) - - /* those down here should be one allocation each (on Linux) */ - buffer.writeBytes(dispatchData) // see https://bugs.swift.org/browse/SR-9597 + buffer.writeRepeatingByte(0x41, count: 16) } @inline(never) func doReads(buffer: inout ByteBuffer) { /* these ones are zero allocations */ - let val = buffer.readInteger(as: UInt8.self) + var val = buffer.readInteger(as: UInt8.self) + precondition(0x41 == val, "\(val!)") + val = buffer.readInteger(as: UInt8.self) precondition(0x41 == val, "\(val!)") var slice = buffer.readSlice(length: 1) let sliceVal = slice!.readInteger(as: UInt8.self) @@ -46,16 +46,19 @@ func run(identifier: String) { buffer.withUnsafeReadableBytes { ptr in precondition(ptr[0] == 0x41) } + let str = buffer.readString(length: 1) + precondition("A" == str, "\(str!)") /* those down here should be one allocation each */ let arr = buffer.readBytes(length: 1) precondition([0x41] == arr!, "\(arr!)") - let str = buffer.readString(length: 1) - precondition("A" == str, "\(str!)") + let data = buffer.readData(length: 16, byteTransferStrategy: .noCopy) + precondition(0x41 == data?.first && 0x41 == data?.last) } for _ in 0..<1000 { doWrites(buffer: &buffer) doReads(buffer: &buffer) + precondition(buffer.readableBytes == 0) } return buffer.readableBytes } diff --git a/Sources/NIOFoundationCompat/ByteBuffer-foundation.swift b/Sources/NIOFoundationCompat/ByteBuffer-foundation.swift index b35f4f8b6f..b69b87b0c6 100644 --- a/Sources/NIOFoundationCompat/ByteBuffer-foundation.swift +++ b/Sources/NIOFoundationCompat/ByteBuffer-foundation.swift @@ -126,10 +126,10 @@ extension ByteBuffer { return Data(bytes: UnsafeMutableRawPointer(mutating: ptr.baseAddress!.advanced(by: index)), count: Int(length)) } else { - _ = storageRef.retain() + let storage = storageRef.takeUnretainedValue() return Data(bytesNoCopy: UnsafeMutableRawPointer(mutating: ptr.baseAddress!.advanced(by: index)), count: Int(length), - deallocator: .custom { _, _ in storageRef.release() }) + deallocator: .custom { _, _ in withExtendedLifetime(storage) {} }) } } } diff --git a/docker/docker-compose.1604.52.yaml b/docker/docker-compose.1604.52.yaml index 0461ab79d6..f4374b3810 100644 --- a/docker/docker-compose.1604.52.yaml +++ b/docker/docker-compose.1604.52.yaml @@ -34,7 +34,7 @@ services: - MAX_ALLOCS_ALLOWED_1000_udpbootstraps=2050 - MAX_ALLOCS_ALLOWED_1000_udpconnections=96050 - MAX_ALLOCS_ALLOWED_1_reqs_1000_conn=455000 - - MAX_ALLOCS_ALLOWED_bytebuffer_lots_of_rw=2050 + - MAX_ALLOCS_ALLOWED_bytebuffer_lots_of_rw=3050 - MAX_ALLOCS_ALLOWED_creating_10000_headers=0 - MAX_ALLOCS_ALLOWED_decode_1000_ws_frames=2050 - MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_holding_buffer=3 diff --git a/docker/docker-compose.1604.53.yaml b/docker/docker-compose.1604.53.yaml index d5368b9c34..be739aeae8 100644 --- a/docker/docker-compose.1604.53.yaml +++ b/docker/docker-compose.1604.53.yaml @@ -34,7 +34,7 @@ services: - MAX_ALLOCS_ALLOWED_1000_udpbootstraps=2050 - MAX_ALLOCS_ALLOWED_1000_udpconnections=95050 - MAX_ALLOCS_ALLOWED_1_reqs_1000_conn=450050 - - MAX_ALLOCS_ALLOWED_bytebuffer_lots_of_rw=2050 + - MAX_ALLOCS_ALLOWED_bytebuffer_lots_of_rw=3050 - MAX_ALLOCS_ALLOWED_creating_10000_headers=0 - MAX_ALLOCS_ALLOWED_decode_1000_ws_frames=2050 - MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_holding_buffer=3 diff --git a/docker/docker-compose.1804.51.yaml b/docker/docker-compose.1804.51.yaml index f3c2110cb6..c2d043c35c 100644 --- a/docker/docker-compose.1804.51.yaml +++ b/docker/docker-compose.1804.51.yaml @@ -34,7 +34,7 @@ services: - MAX_ALLOCS_ALLOWED_1000_udpbootstraps=2050 - MAX_ALLOCS_ALLOWED_1000_udpconnections=96050 - MAX_ALLOCS_ALLOWED_1_reqs_1000_conn=452050 - - MAX_ALLOCS_ALLOWED_bytebuffer_lots_of_rw=2050 + - MAX_ALLOCS_ALLOWED_bytebuffer_lots_of_rw=3050 - MAX_ALLOCS_ALLOWED_creating_10000_headers=10050 - MAX_ALLOCS_ALLOWED_decode_1000_ws_frames=2050 - MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_holding_buffer=3 diff --git a/docker/docker-compose.2004.54.yaml b/docker/docker-compose.2004.54.yaml index 828737d539..952782d409 100644 --- a/docker/docker-compose.2004.54.yaml +++ b/docker/docker-compose.2004.54.yaml @@ -34,7 +34,7 @@ services: - MAX_ALLOCS_ALLOWED_1000_udpbootstraps=2050 - MAX_ALLOCS_ALLOWED_1000_udpconnections=96050 - MAX_ALLOCS_ALLOWED_1_reqs_1000_conn=453050 # regression from 5.3 which was 450050 - - MAX_ALLOCS_ALLOWED_bytebuffer_lots_of_rw=2050 + - MAX_ALLOCS_ALLOWED_bytebuffer_lots_of_rw=3050 - MAX_ALLOCS_ALLOWED_creating_10000_headers=0 - MAX_ALLOCS_ALLOWED_decode_1000_ws_frames=2050 - MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_holding_buffer=3 diff --git a/docker/docker-compose.2004.main.yaml b/docker/docker-compose.2004.main.yaml index 127f83e9f6..ce1dd15746 100644 --- a/docker/docker-compose.2004.main.yaml +++ b/docker/docker-compose.2004.main.yaml @@ -34,7 +34,7 @@ services: - MAX_ALLOCS_ALLOWED_1000_udpbootstraps=2050 - MAX_ALLOCS_ALLOWED_1000_udpconnections=96050 - MAX_ALLOCS_ALLOWED_1_reqs_1000_conn=453000 - - MAX_ALLOCS_ALLOWED_bytebuffer_lots_of_rw=2050 + - MAX_ALLOCS_ALLOWED_bytebuffer_lots_of_rw=3050 - MAX_ALLOCS_ALLOWED_creating_10000_headers=0 - MAX_ALLOCS_ALLOWED_decode_1000_ws_frames=2050 - MAX_ALLOCS_ALLOWED_encode_1000_ws_frames_holding_buffer=3