Skip to content

Commit e0edf6b

Browse files
committed
WebsocketSession.readFrame() should batch read payload bytes.
This is much faster than calling `socket.read()` to read byte-by-byte. I believe this is the same issue raised here: #314
1 parent a6a2ba2 commit e0edf6b

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ All notable changes to this project will be documented in this file. Changes not
2424
## Fixed
2525
- An issue in the `HttpRouter` causing issues to handle routes with overlapping in the tail. ([#379](https://github.com/httpswift/swifter/pull/359), [#382](https://github.com/httpswift/swifter/pull/382)) by [@Vkt0r](https://github.com/Vkt0r)
2626

27+
## Changed
28+
- Performance: Batch reads of websocket payloads rather than reading byte-by-byte. ([#387](https://github.com/httpswift/swifter/pull/387)) by [@lynaghk](https://github.com/lynaghk)
29+
30+
2731
# [1.4.6]
2832
## Added
2933
- The `.movedTemporarily` case (HTTP 307) to possibles HTTP responses. ([#352](https://github.com/httpswift/swifter/pull/352)) by [@csch](https://github.com/csch)

Sources/WebSockets.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,12 @@ public class WebSocketSession: Hashable, Equatable {
276276
let b7 = UInt64(try socket.read())
277277
len = UInt64(littleEndian: b0 | b1 | b2 | b3 | b4 | b5 | b6 | b7)
278278
}
279+
279280
let mask = [try socket.read(), try socket.read(), try socket.read(), try socket.read()]
281+
//Read payload all at once, then apply mask (calling `socket.read` byte-by-byte is super slow).
282+
frm.payload = try socket.read(length: Int(len))
280283
for i in 0..<len {
281-
frm.payload.append(try socket.read() ^ mask[Int(i % 4)])
284+
frm.payload[Int(i)] ^= mask[Int(i % 4)]
282285
}
283286
return frm
284287
}

0 commit comments

Comments
 (0)