Changes between Initial Version and Version 2 of Ticket #34681
- Timestamp:
- Jun 27, 2023, 2:08:58 AM (17 months ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #34681
- Property Owner changed from to
-
Ticket #34681 – Description
initial v2 1 The cache functino memcache_key_warnings() iterates the key character-by-character, a slow operation in Python because it has to create and destroy many individual `str` objects. Instead, we can search the string with a regular expression for a 6x speedup.1 The cache functino memcache_key_warnings() iterates the key character-by-character, a slow operation in Python because it has to create and destroy many individual `str` objects. Instead, we can search the string with a regular expression for a ~3x speedup on a reasonably sized error-free cache key. 2 2 3 3 IPython session comparing old and new approaches: … … 23 23 ...: break 24 24 ...: 25 ...: memcached_error_chars_re = re.compile(r"[\x00-\x 32\x127]")25 ...: memcached_error_chars_re = re.compile(r"[\x00-\x20\x7f]") 26 26 ...: 27 27 ...: … … 32 32 ...: "(longer than %s)" % (key, MEMCACHE_MAX_KEY_LENGTH) 33 33 ...: ) 34 ...: if memcached_error_chars_re. match(key):34 ...: if memcached_error_chars_re.search(key): 35 35 ...: yield ( 36 36 ...: "Cache key contains characters that will cause errors if " 37 37 ...: "used with memcached: %r" % key 38 38 ...: ) 39 ...:40 39 41 40 In [2]: %timeit list(old('acme-bookstore-user-1234567-book-78910391')) 42 1.35 µs ± 1.0 9ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)41 1.35 µs ± 1.05 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each) 43 42 44 43 In [3]: %timeit list(new('acme-bookstore-user-1234567-book-78910391')) 45 212 ns ± 1.42ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)44 475 ns ± 1.37 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each) 46 45 47 46 In [4]: %timeit list(old('homepage\n')) 48 54 5 ns ± 1.17ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)47 546 ns ± 2.88 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each) 49 48 50 49 In [5]: %timeit list(new('homepage\n')) 51 209 ns ± 1.03ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)50 413 ns ± 1.35 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each) 52 51 }}}