Skip to content

Commit

Permalink
Improve performance of serializing headers
Browse files Browse the repository at this point in the history
Avoids ref counting in `to_str` since Cython
can use a fast Py_TYPE check instead

Only call `to_str` once per each key and value
  • Loading branch information
bdraco committed Nov 21, 2024
1 parent c984a44 commit 260ef22
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES/xxx.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved performance of serializing HTTP headers -- by :user:`bdraco`.
18 changes: 11 additions & 7 deletions aiohttp/_http_writer.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,9 @@ cdef inline int _write_str(Writer* writer, str s):
# --------------- _serialize_headers ----------------------

cdef str to_str(object s):
typ = type(s)
if typ is str:
if type(s) is str:
return <str>s
elif typ is _istr:
elif type(s) is _istr:
return PyObject_Str(s)
elif not isinstance(s, str):
raise TypeError("Cannot serialize non-str key {!r}".format(s))
Expand All @@ -124,6 +123,8 @@ def _serialize_headers(str status_line, headers):
cdef object key
cdef object val
cdef bytes ret
cdef str key_str
cdef str val_str

_init_writer(&writer)

Expand All @@ -136,16 +137,19 @@ def _serialize_headers(str status_line, headers):
raise

for key, val in headers.items():
_safe_header(to_str(key))
_safe_header(to_str(val))
key_str = to_str(key)
val_str = to_str(val)

if _write_str(&writer, to_str(key)) < 0:
_safe_header(key_str)
_safe_header(val_str)

if _write_str(&writer, key_str) < 0:
raise
if _write_byte(&writer, b':') < 0:
raise
if _write_byte(&writer, b' ') < 0:
raise
if _write_str(&writer, to_str(val)) < 0:
if _write_str(&writer, val_str) < 0:
raise
if _write_byte(&writer, b'\r') < 0:
raise
Expand Down

0 comments on commit 260ef22

Please sign in to comment.