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

impl FromRequest for BytesMut #2548

Open
1 task done
K4rakara opened this issue Jan 28, 2024 · 2 comments · May be fixed by #2583
Open
1 task done

impl FromRequest for BytesMut #2548

K4rakara opened this issue Jan 28, 2024 · 2 comments · May be fixed by #2583

Comments

@K4rakara
Copy link

  • I have looked for existing issues (including closed) about this

Feature Request

Motivation

I'm working on a Fediverse server implementation, using axum as the web framework. Fediverse servers receive thousands of request every second, even for small instances, like the one I run, so performance is important. Because ActivityPub uses copious amounts of JSON, I figure it's best to use a high-performance JSON parser, like simd-json. However, simd-json's simd_json::from_slice takes a mutable byte slice rather than an immutable one, so I can't use Bytes for that.

This is just one particular usecase, but I'm confident there are other performance-critical scenarios where you'd want a BytesMut value instead of a Bytes value.

Proposal

In axum-core, add the impl, using the existing Bytes impl as a starting point.

Alternatives

For my usecase, I could theoretically turn the Bytes value into a Vec<u8> and then into a BytesMut, but that would likely involve expensive copy operations that would nullify the performance gains from using simd-json.

@davidpdrsn
Copy link
Member

For my usecase, I could theoretically turn the Bytes value into a Vec and then into a BytesMut,

I suppose that’s what axum would have to do as well. Since the body is an async stream you can’t get a mutable reference to the data until you’ve buffered the whole thing. axum doesn’t store the body as Bytes internally or anything.

@jplatte
Copy link
Member

jplatte commented Jan 29, 2024

I'm pretty sure an efficient impl is possible, easy even. I'm surprised the BodyExt::collect thing we use now isn't implemented in terms of collecting to BytesMut + .freeze(), I suppose it is too generic.

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 a pull request may close this issue.

3 participants