Skip to content

Commit 0d7273f

Browse files
committed
where possible, use uninitialized [UInt8] buffers
1 parent 5297b35 commit 0d7273f

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.8
1+
// swift-tools-version: 5.9
22

33
import Foundation
44
import PackageDescription

Sources/IORing/IORing.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ private extension IORing {
545545
count: Int,
546546
link: Bool = false
547547
) throws -> AsyncThrowingChannel<[UInt8], Error> {
548-
var buffer = [UInt8](repeating: 0, count: count)
548+
var buffer = [UInt8]._unsafelyInitialized(count: count)
549549
return try prepareAndSubmitMultishot(
550550
.recv,
551551
fd: fd,
@@ -695,7 +695,7 @@ public extension IORing {
695695
}
696696

697697
func read(count: Int, from fd: FileDescriptorRepresentable) async throws -> [UInt8] {
698-
var buffer = [UInt8](repeating: 0, count: count)
698+
var buffer = [UInt8]._unsafelyInitialized(count: count)
699699
let nread = try await read(into: &buffer, count: count, from: fd)
700700
return Array(buffer.prefix(nread))
701701
}
@@ -722,7 +722,7 @@ public extension IORing {
722722
}
723723

724724
func receive(count: Int, from fd: FileDescriptorRepresentable) async throws -> [UInt8] {
725-
var buffer = [UInt8](repeating: 0, count: count)
725+
var buffer = [UInt8]._unsafelyInitialized(count: count)
726726
try await io_uring_op_recv(fd: fd, buffer: &buffer)
727727
return buffer
728728
}
@@ -958,3 +958,11 @@ extension IORing: Hashable {
958958
ObjectIdentifier(self).hash(into: &hasher)
959959
}
960960
}
961+
962+
package extension [UInt8] {
963+
static func _unsafelyInitialized(count: Int) -> Self {
964+
Self(unsafeUninitializedCapacity: count) { _, initializedCount in
965+
initializedCount = count
966+
}
967+
}
968+
}

Sources/IORingUtils/Extensions.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ extension Message: CustomStringConvertible {
3030
buffer: [UInt8],
3131
flags: UInt32 = 0
3232
) throws {
33-
var addressBuffer = [UInt8](repeating: 0, count: Int(address.size))
34-
addressBuffer.withUnsafeMutableBytes { bytes in
33+
let addressSize = Int(address.size)
34+
let addressBuffer = [UInt8](unsafeUninitializedCapacity: Int(address.size)) { buffer,
35+
initializedCount in
3536
var storage = address.asStorage()
36-
_ = memcpy(bytes.baseAddress!, &storage, bytes.count)
37+
_ = memcpy(buffer.baseAddress!, &storage, addressSize)
38+
initializedCount = addressSize
3739
}
3840
try self.init(name: addressBuffer, buffer: buffer, flags: flags)
3941
}

0 commit comments

Comments
 (0)