Skip to content

Avoid copying remaining buffer in Decoder._decode_literal - #306

Open
amirulian wants to merge 1 commit into
python-hyper:masterfrom
amirulian:fix-literal-decode-memoryview-copy
Open

amirulian wants to merge 1 commit into
python-hyper:masterfrom
amirulian:fix-literal-decode-memoryview-copy

Conversation

@amirulian

Copy link
Copy Markdown

Summary

  • Decoder.decode() wraps data in a memoryview (data_mem) and passes data_mem[current_index:] to _decode_literal_index and _decode_literal_no_index to avoid copying the remaining buffer on each header.
  • Commit f4a5ee7 added if isinstance(data, memoryview): data = data.tobytes() at the start of _decode_literal(), which copies the entire remaining HPACK buffer on every literal header field (followed by two additional bytes slice copies data[1:] and data[consumed + length:]), resulting in quadratic O(N^2) memory copying across blocks with many literal headers.
  • Keep data as a memoryview inside _decode_literal() and convert only the extracted non-Huffman raw_name and raw_value slices to bytes.

Testing

  • Added test_decode_many_literal_headers_memoryview in tests/test_hpack.py.
  • Ran pytest (503 passed) and ruff check src/.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant