-
Notifications
You must be signed in to change notification settings - Fork 84
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Avoid unnecessary heap allocation when parsing frame headers. #107
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1159,17 +1159,17 @@ fileprivate struct FrameHeader { | |
|
||
fileprivate extension ByteBuffer { | ||
mutating func readFrameHeader() -> FrameHeader? { | ||
guard self.readableBytes >= 9 else { | ||
return nil | ||
let saveSelf = self | ||
guard let lenHigh = self.readInteger(as: UInt16.self), | ||
let lenLow = self.readInteger(as: UInt8.self), | ||
let type = self.readInteger(as: UInt8.self), | ||
let flags = self.readInteger(as: UInt8.self), | ||
let rawStreamID = self.readInteger(as: UInt32.self) else { | ||
self = saveSelf | ||
return nil | ||
} | ||
|
||
let lenBytes: [UInt8] = self.readBytes(length: 3)! | ||
let len = Int(lenBytes[0]) << 16 | Int(lenBytes[1]) << 8 | Int(lenBytes[2]) | ||
let type: UInt8 = self.readInteger()! | ||
let flags: UInt8 = self.readInteger()! | ||
let rawStreamID: UInt32 = self.readInteger()! | ||
|
||
return FrameHeader(length: len, type: type, flags: FrameFlags(rawValue: flags), rawStreamID: rawStreamID) | ||
return FrameHeader(length: Int(lenHigh) << 8 | Int(lenLow), type: type, flags: FrameFlags(rawValue: flags), rawStreamID: rawStreamID) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aren't we overwriting 8 bits here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No: the UInt16 is converted to an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Lukasa oops, thanks, brain fart :D |
||
} | ||
|
||
mutating func writePayloadSize(_ size: Int, at location: Int) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you should keep the old code but replace
with