Преглед на файлове

Improved performance of reading

Sergey Erokhin преди 5 години
родител
ревизия
ef326c421e
променени са 2 файла, в които са добавени 4 реда и са изтрити 7 реда
  1. 1 0
      CHANGELOG.md
  2. 3 7
      Xcode/Sources/Socket.swift

+ 1 - 0
CHANGELOG.md

@@ -32,6 +32,7 @@ All notable changes to this project will be documented in this file. Changes not
 - Fix misspell `serialise`. ([#473](https://github.com/httpswift/swifter/pull/473)) by [@mtgto](https://github.com/mtgto)
 - Fix an issue causing Danger was not working properly. ([#486](https://github.com/httpswift/swifter/pull/486)) by [@Vkt0r](https://github.com/Vkt0r)
 - 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)
+- Improved performance of data reading. ([#487](https://github.com/httpswift/swifter/pull/487)) by [@till0xff](https://github.com/till0xff)
 
 ## Changed
 

+ 3 - 7
Xcode/Sources/Socket.swift

@@ -149,13 +149,9 @@ open class Socket: Hashable, Equatable {
     /// - Returns: A buffer containing the bytes read
     /// - Throws: SocketError.recvFailed if unable to read bytes from the socket
     open func read(length: Int) throws -> [UInt8] {
-        var buffer = UnsafeMutableBufferPointer<UInt8>.allocate(capacity: length)
-
-        let bytesRead = try read(into: &buffer, length: length)
-
-        let rv = [UInt8](buffer[0..<bytesRead])
-        buffer.deallocate()
-        return rv
+        return try [UInt8](unsafeUninitializedCapacity: length) { buffer, bytesRead in
+            bytesRead = try read(into: &buffer, length: length)
+        }
     }
 
     static let kBufferLength = 1024