Skip to content

Commit

Permalink
fix: more performance
Browse files Browse the repository at this point in the history
  • Loading branch information
koraykoska committed Oct 10, 2023
1 parent 0d31675 commit fdcb674
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Sources/libwebsockets/WebsocketClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Sources/libwebsockets/WebsocketClientContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
36 changes: 30 additions & 6 deletions Sources/libwebsockets/WebsocketFrameSequence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,53 @@ public protocol WebsocketFrameSequence: Sendable {
var binaryBuffer: Data { get }
var textBuffer: Data { get }
var type: WebsocketOpcode { get }
var count: Int { get }

init(type: WebsocketOpcode)

mutating func append(_ frame: Data)
}

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
}
Expand Down

0 comments on commit fdcb674

Please sign in to comment.