Skip to content

Commit d1561ae

Browse files
authored
fix unnecessary bytebuffer copy during packet length check (#232)
1 parent 84047df commit d1561ae

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

Sources/MySQL/Utilities/ByteBuffer+MySQL.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,19 @@ extension ByteBuffer {
8080
extension ByteBuffer {
8181
/// Returns packet length if there are enough readable bytes.
8282
mutating func checkPacketLength() throws -> Int32? {
83-
// erase sequence id so we can easily parse 4 byte integer
84-
set(integer: Byte(0), at: readerIndex + 3)
85-
guard let length = peekInteger(endianness: .little, as: Int32.self) else {
83+
guard var length = peekInteger(endianness: .little, as: Int32.self) else {
8684
return nil
8785
}
86+
// erase sequence id
87+
length = length & 0x00FFFFFF
8888
if readableBytes >= length + 4 /* must be enough bytes to read length too */ {
89-
return try requireInteger(endianness: .little)
89+
moveReaderIndex(forwardBy: 4)
90+
return length
9091
} else {
9192
return nil
9293
}
9394
}
94-
95+
9596
func peekInteger<T>(endianness: Endianness = .big, as type: T.Type = T.self, skipping: Int = 0) -> T? where T: FixedWidthInteger {
9697
guard readableBytes >= MemoryLayout<T>.size + skipping else {
9798
return nil

0 commit comments

Comments
 (0)