Skip to content

Commit

Permalink
[REFACTOR] Speed up bulk string parser and flush! using readbytes!, r…
Browse files Browse the repository at this point in the history
…ename internal functions
  • Loading branch information
captchanjack committed Jan 30, 2022
1 parent 4d738eb commit 9881a08
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Jedis"
uuid = "b89ccfe0-2c5f-46f6-b89b-da3e1c2e286f"
authors = ["Jack Chan <[email protected]>"]
version = "0.2.8"
version = "0.2.9"

[deps]
MbedTLS = "739be429-bea8-5141-9913-cc70e7f3736d"
Expand Down
6 changes: 3 additions & 3 deletions src/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,9 @@ end
Reads and discards any bytes that remain unread in the client socket.
"""
function flush!(client::Client)
while bytesavailable(client.socket) > 0
recv(client.socket)
end
nb = bytesavailable(client.socket)
buffer = Vector{UInt8}(undef, nb)
readbytes!(client.socket, buffer, nb)
end

"""
Expand Down
48 changes: 26 additions & 22 deletions src/protocol.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,48 +39,52 @@ function resp(command::AbstractArray)
return "$(RedisType.array)$(n)$(CRLF)" * r
end

function handle_simple_string(_, x)
function parse_simple_string(_, x)
return x
end

function handle_integer(_, x)
function parse_integer(_, x)
return parse(Int64, x)
end

function handle_error(_, x)
function parse_error(_, x)
err_type, err_msg = split(x, ' '; limit=2)
return RedisError(err_type, err_msg)
end

function handle_bulk_string(io, x)
function parse_bulk_string(io, x)
if x == "-1"
return nothing
end

x = parse(Int, x)
r = ""

while length(r) < x + 2
r *= readline(io; keep=true)
end

return r[1:end-2]
x = parse(Int, x) + 2
buffer = Vector{UInt8}(undef, x)
readbytes!(io, buffer, x)
return String(buffer[1:end-2])
end

function handle_array(io, x)
function parse_array(io, x)
if x == "0"
return []
end
return [recv(io) for _ in 1:parse(Int64, x)]
end

const RESPHandler = Dict{Char,Function}(
'+' => handle_simple_string,
'-' => handle_error,
':' => handle_integer,
'$' => handle_bulk_string,
'*' => handle_array
)
function resp_parser(b::Char)::Function
if b == '+'
return parse_simple_string
elseif b == '-'
return parse_error
elseif b == ':'
return parse_integer
elseif b == '$'
return parse_bulk_string
elseif b == '*'
return parse_array
else
throw(RedisError("INVALIDBYTE", "Parser for byte '$b' does not exist."))
end
end

"""
recv(io::Union{TCPSocket,Base.GenericIOBuffer})
Expand All @@ -94,8 +98,8 @@ function recv(io::Union{TCPSocket,Base.GenericIOBuffer})
return nothing
end

handler = RESPHandler[line[1]]
return handler(io, line[2:end])
parser = resp_parser(line[1])
return parser(io, line[2:end])
end

"""
Expand Down

2 comments on commit 9881a08

@captchanjack
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/53462

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.9 -m "<description of version>" 9881a08068a9f1773c16d1de27ac9109044809e8
git push origin v0.2.9

Please sign in to comment.