Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

so it turns out that everything is always Bytes #1015

Closed

Conversation

hawkw
Copy link
Member

@hawkw hawkw commented May 20, 2021

Currently, the http_box::BoxBody type erases both the type of the
http_body::Body and the Body::Data associated type (to
Box<dyn Buf + Send>). However, it turns out that erasing the Data
type isn't actually necessary: all Body types in Linkerd use
Bytes as the Data type.

This branch changes BoxBody to just require that B::Data is Bytes,
and always produce Bytes as its data type. This will enable a much
more performant implementation of request body buffering for POST
retries (linkerd/linkerd2#6130). Rather than copying all the bytes from
a dyn Buf Data type into an additional buffer, we can just clone the
Bytes, increasing the reference count for that buffer, and return the
original Bytes. This means we don't need to perform additional memcpys
to buffer bodies for retries; we just hold onto the already-allocated
Bytes buffers.

The cloned Bytes can then be stored in a vector, and if we have to
write them out again for a retry, we can do one big vectored write :)

image

Currently, the `http_box::BoxBody` type erases both the type of the
`http_body::Body` _and_ the `Body::Data` associated type (to `Box<dyn
Buf + Send>`). However, it turns out that erasing the `Data` type isn't
actually necessary: _all_ `Body` types in Linkerd use `Bytes` as the
`Bytes` type.

This branch changes `BoxBody` to just require that `B::Data` is `Bytes`,
and always produce `Bytes` as its data type. This will enable a much
more performant implementation of request body buffering for POST
retries (linkerd/linkerd2#6130). Rather than copying all the bytes from
a `dyn Buf` `Data` type into an additional buffer, we can just clone the
`Bytes`, increasing the reference count for that buffer, and return the
original `Bytes`. This means we don't need to perform additional memcpys
to buffer bodies for retries; we just hold onto the already-allocated
`Bytes` buffers.

The cloned `Bytes` can then be stored in a vector, and if we have to
write them out again for a retry, we can do one big vectored write :)
@hawkw hawkw requested a review from a team May 20, 2021 21:50
@hawkw hawkw changed the title so it turns out that everything is always bytes so it turns out that everything is always Bytes May 20, 2021
@hawkw
Copy link
Member Author

hawkw commented May 20, 2021

Actually, upon further consideration, I think we don't want to do this. Making all the buffers Bytes means we can't do the "one big vectored write thing", and would instead have to stream the body chunks out one Bytes at a time.

Instead we should use Buf::copy_to_bytes, which i had forgotten does a shallow clone ref bump when the Buf is a Bytes.

nevermind lol

@hawkw hawkw closed this May 20, 2021
@olix0r olix0r deleted the eliza/youre-telling-me-its-all-bytes-always-has-been branch March 7, 2023 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant