From 3f934988efad78db8b631107e91359ea327a75b9 Mon Sep 17 00:00:00 2001 From: Stephen Mitchell Date: Sun, 27 Sep 2020 10:26:47 -0400 Subject: [PATCH] Cleans up parseByPrefix code - helps speed --- Sources/SwiftRedis/RedisResp.swift | 60 ++++++++++++------------------ 1 file changed, 23 insertions(+), 37 deletions(-) diff --git a/Sources/SwiftRedis/RedisResp.swift b/Sources/SwiftRedis/RedisResp.swift index 746ac39..58e356f 100644 --- a/Sources/SwiftRedis/RedisResp.swift +++ b/Sources/SwiftRedis/RedisResp.swift @@ -114,54 +114,40 @@ class RedisResp { private func parseByPrefix(_ buffer: inout Data, from: Int) throws -> (RedisResponse, Int) { var response: RedisResponse - var (matched, offset) = try compare(&buffer, at: from, with: RedisResp.plus) - - if matched { - (response, offset) = try parseSimpleString(&buffer, offset: offset) - } else { - (matched, offset) = try compare(&buffer, at: from, with: RedisResp.colon) - if matched { - (response, offset) = try parseInteger(&buffer, offset: offset) - } else { - (matched, offset) = try compare(&buffer, at: from, with: RedisResp.dollar) - if matched { - (response, offset) = try parseBulkString(&buffer, offset: offset) - } else { - (matched, offset) = try compare(&buffer, at: from, with: RedisResp.asterisk) - if matched { - (response, offset) = try parseArray(&buffer, offset: offset) - } else { - (matched, offset) = try compare(&buffer, at: from, with: RedisResp.minus) - if matched { - (response, offset) = try parseError(&buffer, offset: offset) - } else { - response = RedisResponse.Error("Unknown response type") - } - } - } + var offset = from + + while offset+1 >= buffer.count { + let length = try socket?.read(into: &buffer) + + if length == 0 { + throw RedisRespError(code: .EOF) } } + + switch (buffer[offset.. (RedisResponse, Int) { - let (matched, offset) = try compare(&buffer, at: from, with: RedisResp.dollar) - if matched { - return try parseBulkString(&buffer, offset: offset) - } - return try parseByPrefix(&buffer, from: offset) - } private func parseArray(_ buffer: inout Data, offset: Int) throws -> (RedisResponse, Int) { var (arrayLength, newOffset) = try parseIntegerValue(&buffer, offset: offset) var responses = [RedisResponse]() var response: RedisResponse if arrayLength >= 0 { for _ in 0 ..< Int(arrayLength) { - (response, newOffset) = try parseByPrefixArrayOptimization(&buffer, from: newOffset) + (response, newOffset) = try parseByPrefix(&buffer, from: newOffset) responses.append(response) } return (RedisResponse.Array(responses), newOffset)