From fdcb67455014d74af2e11ee9300697edd647ff28 Mon Sep 17 00:00:00 2001 From: Koray Koska <11356621+koraykoska@users.noreply.github.com> Date: Tue, 10 Oct 2023 15:30:01 +0200 Subject: [PATCH] fix: more performance --- Sources/libwebsockets/WebsocketClient.swift | 2 +- .../WebsocketClientContext.swift | 2 +- .../WebsocketFrameSequence.swift | 36 +++++++++++++++---- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/Sources/libwebsockets/WebsocketClient.swift b/Sources/libwebsockets/WebsocketClient.swift index 1494ad2..72664f8 100644 --- a/Sources/libwebsockets/WebsocketClient.swift +++ b/Sources/libwebsockets/WebsocketClient.swift @@ -794,7 +794,7 @@ internal func _lws_swift_websocketClientCallback( frameSequence.append(data) // Check message size - let messageSize = isBinary ? frameSequence.binaryBuffer.count : frameSequence.textBuffer.count + let messageSize = frameSequence.count if let maxMessageSize = websocketClient.maxMessageSize, messageSize > maxMessageSize { // Close connection websocketClient.close(reason: .messageTooLarge) diff --git a/Sources/libwebsockets/WebsocketClientContext.swift b/Sources/libwebsockets/WebsocketClientContext.swift index cda21a4..a5f274c 100644 --- a/Sources/libwebsockets/WebsocketClientContext.swift +++ b/Sources/libwebsockets/WebsocketClientContext.swift @@ -131,7 +131,7 @@ internal final class WebsocketClientContext { lwsProtocols.callback = _lws_swift_websocketClientCallback lwsProtocols.per_session_data_size = 0 // TODO: Per instance customization? - lwsProtocols.rx_buffer_size = 4000000 + lwsProtocols.rx_buffer_size = 1000000 protocolsPointer.initialize(to: lwsProtocols) diff --git a/Sources/libwebsockets/WebsocketFrameSequence.swift b/Sources/libwebsockets/WebsocketFrameSequence.swift index a6f0d7f..52205b8 100644 --- a/Sources/libwebsockets/WebsocketFrameSequence.swift +++ b/Sources/libwebsockets/WebsocketFrameSequence.swift @@ -5,6 +5,7 @@ public protocol WebsocketFrameSequence: Sendable { var binaryBuffer: Data { get } var textBuffer: Data { get } var type: WebsocketOpcode { get } + var count: Int { get } init(type: WebsocketOpcode) @@ -12,22 +13,45 @@ public protocol WebsocketFrameSequence: Sendable { } public struct WebsocketSimpleAppendFrameSequence: WebsocketFrameSequence { - private(set) public var binaryBuffer: Data - private(set) public var textBuffer: Data + public var binaryBuffer: Data { + return _binaryBuffer[0..<_count] + } + public var textBuffer: Data { + return _textBuffer[0..<_count] + } + public var count: Int { + return _count + } + private var _count: Int + private var _binaryBuffer: Data + private var _textBuffer: Data public let type: WebsocketOpcode + private let bufferSize = 1000000 + public init(type: WebsocketOpcode) { - self.binaryBuffer = Data() - self.textBuffer = Data() + self._binaryBuffer = Data() + self._textBuffer = Data() + self._count = 0 self.type = type } public mutating func append(_ frame: Data) { switch type { case .binary: - self.binaryBuffer.append(frame) + if _binaryBuffer.count < _count + frame.count { + _binaryBuffer.append(Data(count: max(bufferSize, frame.count))) + } + + _count += frame.count + self._binaryBuffer.insert(contentsOf: frame, at: _count) case .text: - self.textBuffer.append(frame) + if _textBuffer.count < _count + frame.count { + _textBuffer.append(Data(count: max(bufferSize, frame.count))) + } + + _count += frame.count + self._textBuffer.insert(contentsOf: frame, at: _count) default: break }