Skip to content
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

Speeding up the protocol parsing #2596

Merged
merged 3 commits into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions redis/asyncio/connection.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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