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

Improve _check_key() and _store_cmd() performance #183

Merged
merged 2 commits into from
Aug 31, 2018
Merged
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
46 changes: 19 additions & 27 deletions pymemcache/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,29 +90,19 @@ def _check_key(key, allow_unicode_keys, key_prefix=b''):
try:
key = key.encode('ascii')
except (UnicodeEncodeError, UnicodeDecodeError):
raise MemcacheIllegalInputError("Non-ASCII key: '%r'" % (key,))
raise MemcacheIllegalInputError("Non-ASCII key: '%r'" % key)

key = key_prefix + key
parts = key.split()

if len(key) > 250:
raise MemcacheIllegalInputError("Key is too long: '%r'" % (key,))

for c in bytearray(key):
if c == ord(b' '):
raise MemcacheIllegalInputError(
"Key contains space: '%r'" % (key,)
)
elif c == ord(b'\n'):
raise MemcacheIllegalInputError(
"Key contains newline: '%r'" % (key,)
)
elif c == ord(b'\00'):
raise MemcacheIllegalInputError(
"Key contains null character: '%r'" % (key,)
)
elif c == ord(b'\r'):
raise MemcacheIllegalInputError(
"Key contains carriage return: '%r'" % (key,)
)
raise MemcacheIllegalInputError("Key is too long: '%r'" % key)
# second statement catches leading or trailing whitespace
elif len(parts) > 1 or parts[0] != key:
raise MemcacheIllegalInputError("Key contains whitespace: '%r'" % key)
elif b'\00' in key:
raise MemcacheIllegalInputError("Key contains null: '%r'" % key)

return key


Expand Down Expand Up @@ -765,6 +755,14 @@ def _fetch_cmd(self, name, keys, expect_cas):
def _store_cmd(self, name, values, expire, noreply, cas=None):
cmds = []
keys = []

extra = b''
if cas is not None:
extra += b' ' + cas
if noreply:
extra += b' noreply'
expire = six.text_type(expire).encode('ascii')

for key, data in six.iteritems(values):
# must be able to reliably map responses back to the original order
keys.append(key)
Expand All @@ -781,15 +779,9 @@ def _store_cmd(self, name, values, expire, noreply, cas=None):
except UnicodeEncodeError as e:
raise MemcacheIllegalInputError(str(e))

extra = b''
if cas is not None:
extra += b' ' + cas
if noreply:
extra += b' noreply'

cmds.append(name + b' ' + key + b' ' +
six.text_type(flags).encode('ascii') +
b' ' + six.text_type(expire).encode('ascii') +
b' ' + expire +
b' ' + six.text_type(len(data)).encode('ascii') +
extra + b'\r\n' + data + b'\r\n')

Expand Down