-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Create BytesMut for Json with initial capacity #1196
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense!
actix-web uses an initial capacity of 8192 bytes. I'm wondering if we should benchmark a slightly bigger initial capacity as well 🤔
Oh, interesting! I suppose it completely depends on the size of JSON being serialized. I would guess that JSON responses in real-world servers are usually >128 bytes... |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Intuitively, 128 seems like a good initial capacity to me but 8k seems a little much.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep its cool with me as well. We can always tweak later.
Head branch was pushed to by a user without write access
Hmm it appears that the required checks for the |
Yeah I'll do that separately. Thanks! |
This PR initializes the
BytesMut
for serializingJson
with a small initial capacity to reduce allocations as the innerVec<u8>
grows.A recent PR removed an unnecessary copy by introducing
BytesMut
and switching away fromserde_json::to_vec
, but I noticed a small regression that I believe is due toserde_json::to_vec
creating aVec<u8>
with an initial capacity (code). I figured matching the initial capacity of "128 bytes" made sense. In a simple benchmark like below (mostly pulled from this comment), I'm seeing a modest ~1% speed bump.