Support zero-copy memoryview uploads as request content#1043
Draft
technillogue wants to merge 1 commit into
Draft
Support zero-copy memoryview uploads as request content#1043technillogue wants to merge 1 commit into
technillogue wants to merge 1 commit into
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
Previously `content=memoryview(...)` fell through `encode_content` to the generic iterable branch, was iterated element-by-element into integers, and raised `TypeError: object of type 'int' has no len()`. httpx2: * `encode_content` now accepts a C-contiguous `memoryview` (e.g. over a `bytearray`, `array.array`, or an `mmap`) and yields it as a single chunk via a new `BufferStream`, normalised to a 1-D unsigned-byte view. A non-contiguous view raises rather than being silently copied. * `BufferStream`, unlike `ByteStream`, is treated as (replayable) streaming content - it is not eagerly read into a `bytes` object at request construction - so the buffer is never materialised up front. * `memoryview` is added to `RequestContent`/`ResponseContent`. httpcore2: * `_send_event` uses h11's `send_with_data_passthrough` for `Data` events. With Content-Length framing the body is written straight to the network rather than via `b"".join(...)`. Chunked framing and header writes are unchanged (coalesced into a single write). * Body `memoryview` chunks with itemsize > 1 are normalised to a flat byte view before h11, since both h11's Content-Length tracking (`len()`) and the backends' byte-count slicing assume one byte per element. Net effect: a large buffer (e.g. a `memoryview` over an `mmap`) is only faulted into memory as it is written to the socket. A 128 MiB upload allocates ~18 MiB peak instead of a full-body copy.
70f8d89 to
c78ecf3
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Previously
content=memoryview(...)fell throughencode_contentto the generic iterable branch, was iterated element-by-element into integers, and raisedTypeError: object of type 'int' has no len().httpx2:
encode_contentnow accepts a C-contiguousmemoryview(e.g. over abytearray,array.array, or anmmap) and yields it as a single chunk via a newBufferStream, normalised to a 1-D unsigned-byte view. A non-contiguous view raises rather than being silently copied.BufferStream, unlikeByteStream, is treated as (replayable) streaming content - it is not eagerly read into abytesobject at request construction - so the buffer is never materialised up front.memoryviewis added toRequestContent/ResponseContent.httpcore2:
_send_eventuses h11'ssend_with_data_passthroughforDataevents. With Content-Length framing the body is written straight to the network rather than viab"".join(...). Chunked framing and header writes are unchanged (coalesced into a single write).memoryviewchunks with itemsize > 1 are normalised to a flat byte view before h11, since both h11's Content-Length tracking (len()) and the backends' byte-count slicing assume one byte per element.Net effect: a large buffer (e.g. a
memoryviewover anmmap) is only faulted into memory as it is written to the socket. A 128 MiB upload allocates ~18 MiB peak instead of a full-body copy.Checklist