]> git.ipfire.org Git - thirdparty/tornado.git/commit
httputil: cache header normalization with @lru_cache instead of hand-rolling
authorRan Benita <ran@unusedvar.com>
Tue, 6 Aug 2019 15:16:31 +0000 (18:16 +0300)
committerRan Benita <ran@unusedvar.com>
Tue, 6 Aug 2019 15:33:33 +0000 (18:33 +0300)
commit549edaf64d08bedf0228489c062d1ff3f0281320
treec850a53385ed71d57a0b564db680d4f890fc0496
parent444f4cd198b25d1561d83d5cc895fdfa4b952574
httputil: cache header normalization with @lru_cache instead of hand-rolling

Tornado is now py3-only so @lru_cache is always available.

Performance is about the same. Benchmark below. Python 3.7 on Linux.

before, cached:   0.9121252089971676
before, uncached: 13.358482279989403

after, cached:    0.9175888689933345
after, uncached:  11.085199063003529

```py
from time import perf_counter

names = [f'sOMe-RanDOM-hEAdeR-{i}' for i in range(1000)]

from tornado.httputil import _normalize_header
start = perf_counter()
for i in range(10000):
    # _normalize_header.cache_clear()
    for name in names:
        _normalize_header(name)
print(perf_counter() - start)

from tornado.httputil import _NormalizedHeaderCache
start = perf_counter()
_normalized_headers = _NormalizedHeaderCache(1000)
for i in range(10000):
    # _normalized_headers = _NormalizedHeaderCache(1000)
    for name in names:
        _normalized_headers[name]
print(perf_counter() - start)
```
tornado/httputil.py