From 7e7a2f2058690e8286187c1bd7ac999dcfa506b6 Mon Sep 17 00:00:00 2001 From: Ryan Fowler Date: Tue, 26 Jul 2022 10:45:58 -0400 Subject: [PATCH] Create BytesMut for Json with initial capacity (#1196) * Create BytesMut for Json with initial capacity * Add a comment explaining BytesMut initial capacity * Update link to serde_json::to_vec function to docs.rs --- axum/src/json.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/axum/src/json.rs b/axum/src/json.rs index d496f607..6f4b0988 100644 --- a/axum/src/json.rs +++ b/axum/src/json.rs @@ -186,7 +186,9 @@ where T: Serialize, { fn into_response(self) -> Response { - let mut buf = BytesMut::new().writer(); + // Use a small initial capacity of 128 bytes like serde_json::to_vec + // https://docs.rs/serde_json/1.0.82/src/serde_json/ser.rs.html#2189 + let mut buf = BytesMut::with_capacity(128).writer(); match serde_json::to_writer(&mut buf, &self.0) { Ok(()) => ( [(