summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin J <hello@martinnj.dk>2022-06-07 20:46:20 +0200
committerGitHub <noreply@github.com>2022-06-07 11:46:20 -0700
commit43349a490506e0418acd643d89a8f2a800893e19 (patch)
tree9df3d2f20d8fde384fb8c418e3c2d0f68d8c8deb
parent8f0a135991944190025f991c67357ff3435c5f7d (diff)
downloadpymemcache-43349a490506e0418acd643d89a8f2a800893e19.tar.gz
Small buffer pass optimization as discussed in #395. (#402)
Basically ensure the client only does one pass over the buffer instead of two. Exact thread: https://github.com/pinterest/pymemcache/pull/395#discussion_r890288417
-rw-r--r--pymemcache/client/base.py11
1 files changed, 7 insertions, 4 deletions
diff --git a/pymemcache/client/base.py b/pymemcache/client/base.py
index 22251da..2654661 100644
--- a/pymemcache/client/base.py
+++ b/pymemcache/client/base.py
@@ -1443,10 +1443,13 @@ def _readline(sock, buf):
# Strip the last character from the last chunk.
chunks[-1] = chunks[-1][:-1]
return buf[1:], b"".join(chunks)
- elif buf.find(b"\r\n") != -1:
- before, sep, after = buf.partition(b"\r\n")
- chunks.append(before)
- return after, b"".join(chunks)
+ else:
+ token_pos = buf.find(b"\r\n")
+ if token_pos != -1:
+ # Note: 2 == len(b"\r\n")
+ before, after = buf[:token_pos], buf[token_pos + 2 :]
+ chunks.append(before)
+ return after, b"".join(chunks)
if buf:
chunks.append(buf)