From c27fbdc35f2c86fa56dc62088abed2eb108aeccd Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Tue, 2 Apr 2019 10:19:56 -0700 Subject: [PATCH] Preallocate read buffer for h1 codec, #749 --- actix-http/CHANGES.md | 2 ++ actix-http/Cargo.toml | 2 +- actix-http/src/h1/codec.rs | 8 ++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index c5c02865c..79187e7a5 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -6,6 +6,8 @@ * Rust 1.31.0 compatibility +* Preallocate read buffer for h1 codec + ## [0.1.0-alpha.2] - 2019-03-29 diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index 2de624b7c..a9fda44eb 100644 --- a/actix-http/Cargo.toml +++ b/actix-http/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "actix-http" -version = "0.1.0-alpha.2" +version = "0.1.0-alpha.3" authors = ["Nikolay Kim "] description = "Actix http primitives" readme = "README.md" diff --git a/actix-http/src/h1/codec.rs b/actix-http/src/h1/codec.rs index ceb1027e5..e4895f2dd 100644 --- a/actix-http/src/h1/codec.rs +++ b/actix-http/src/h1/codec.rs @@ -19,6 +19,9 @@ use crate::message::{ConnectionType, Head, ResponseHead}; use crate::request::Request; use crate::response::Response; +const LW: usize = 2 * 1024; +const HW: usize = 32 * 1024; + bitflags! { struct Flags: u8 { const HEAD = 0b0000_0001; @@ -105,6 +108,11 @@ impl Decoder for Codec { type Error = ParseError; fn decode(&mut self, src: &mut BytesMut) -> Result, Self::Error> { + let cap = src.capacity(); + if cap < LW { + src.reserve(HW - cap); + } + if self.payload.is_some() { Ok(match self.payload.as_mut().unwrap().decode(src)? { Some(PayloadItem::Chunk(chunk)) => Some(Message::Chunk(Some(chunk))),