Skip to content

Commit ef326c4

Browse files
till0xffs.erokhin
authored andcommitted
Improved performance of reading
1 parent eea4bb1 commit ef326c4

File tree

2 files changed

+4
-7
lines changed

2 files changed

+4
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ All notable changes to this project will be documented in this file. Changes not
3232
- Fix misspell `serialise`. ([#473](https://github.com/httpswift/swifter/pull/473)) by [@mtgto](https://github.com/mtgto)
3333
- Fix an issue causing Danger was not working properly. ([#486](https://github.com/httpswift/swifter/pull/486)) by [@Vkt0r](https://github.com/Vkt0r)
3434
- Set Swift version to 5.0 in podspec. ([#475](https://github.com/httpswift/swifter/pull/475)) by [@p-krasnobrovkin-tcs](https://github.com/p-krasnobrovkin-tcs)
35+
- Improved performance of data reading. ([#487](https://github.com/httpswift/swifter/pull/487)) by [@till0xff](https://github.com/till0xff)
3536

3637
## Changed
3738

Xcode/Sources/Socket.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,9 @@ open class Socket: Hashable, Equatable {
149149
/// - Returns: A buffer containing the bytes read
150150
/// - Throws: SocketError.recvFailed if unable to read bytes from the socket
151151
open func read(length: Int) throws -> [UInt8] {
152-
var buffer = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: length)
153-
154-
let bytesRead = try read(into: &buffer, length: length)
155-
156-
let rv = [UInt8](buffer[0..<bytesRead])
157-
buffer.deallocate()
158-
return rv
152+
return try [UInt8](unsafeUninitializedCapacity: length) { buffer, bytesRead in
153+
bytesRead = try read(into: &buffer, length: length)
154+
}
159155
}
160156

161157
static let kBufferLength = 1024

0 commit comments

Comments
 (0)