Skip to content

Commit

Permalink
Speeding up the protocol parsing (#2596)
Browse files Browse the repository at this point in the history
* speeding up the protocol parser

* linting

* changes to ease
  • Loading branch information
chayim committed Mar 15, 2023
1 parent b546a9a commit 5588ae0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
25 changes: 12 additions & 13 deletions redis/asyncio/connection.py
Expand Up @@ -267,9 +267,6 @@ async def _read_response(
response: Any
byte, response = raw[:1], raw[1:]

if byte not in (b"-", b"+", b":", b"$", b"*"):
raise InvalidResponse(f"Protocol Error: {raw!r}")

# server returned an error
if byte == b"-":
response = response.decode("utf-8", errors="replace")
Expand All @@ -289,22 +286,24 @@ async def _read_response(
pass
# int value
elif byte == b":":
response = int(response)
return int(response)
# bulk response
elif byte == b"$" and response == b"-1":
return None
elif byte == b"$":
length = int(response)
if length == -1:
return None
response = await self._read(length)
response = await self._read(int(response))
# multi-bulk response
elif byte == b"*" and response == b"-1":
return None
elif byte == b"*":
length = int(response)
if length == -1:
return None
response = [
(await self._read_response(disable_decoding)) for _ in range(length)
(await self._read_response(disable_decoding))
for _ in range(int(response)) # noqa
]
if isinstance(response, bytes) and disable_decoding is False:
else:
raise InvalidResponse(f"Protocol Error: {raw!r}")

if disable_decoding is False:
response = self.encoder.decode(response)
return response

Expand Down
24 changes: 11 additions & 13 deletions redis/connection.py
Expand Up @@ -358,9 +358,6 @@ def _read_response(self, disable_decoding=False):

byte, response = raw[:1], raw[1:]

if byte not in (b"-", b"+", b":", b"$", b"*"):
raise InvalidResponse(f"Protocol Error: {raw!r}")

# server returned an error
if byte == b"-":
response = response.decode("utf-8", errors="replace")
Expand All @@ -379,23 +376,24 @@ def _read_response(self, disable_decoding=False):
pass
# int value
elif byte == b":":
response = int(response)
return int(response)
# bulk response
elif byte == b"$" and response == b"-1":
return None
elif byte == b"$":
length = int(response)
if length == -1:
return None
response = self._buffer.read(length)
response = self._buffer.read(int(response))
# multi-bulk response
elif byte == b"*" and response == b"-1":
return None
elif byte == b"*":
length = int(response)
if length == -1:
return None
response = [
self._read_response(disable_decoding=disable_decoding)
for i in range(length)
for i in range(int(response))
]
if isinstance(response, bytes) and disable_decoding is False:
else:
raise InvalidResponse(f"Protocol Error: {raw!r}")

if disable_decoding is False:
response = self.encoder.decode(response)
return response

Expand Down

0 comments on commit 5588ae0

Please sign in to comment.