perf(body): move h2 flow control from Chunk to h2 variant of Body

This commit is contained in:
Sean McArthur
2018-05-31 15:26:04 -07:00
parent 5323c2f39c
commit 8ff42dbbb4
2 changed files with 3 additions and 31 deletions

View File

@@ -290,7 +290,8 @@ impl Body {
.map(|async| { .map(|async| {
async.map(|opt| { async.map(|opt| {
opt.map(|bytes| { opt.map(|bytes| {
Chunk::h2(bytes, h2.release_capacity()) let _ = h2.release_capacity().release_capacity(bytes.len());
Chunk::from(bytes)
}) })
}) })
}) })

View File

@@ -1,23 +1,16 @@
use std::fmt; use std::fmt;
use bytes::{Buf, Bytes}; use bytes::{Buf, Bytes};
use h2::ReleaseCapacity;
/// A piece of a message body. /// A piece of a message body.
/// ///
/// These are returned by [`Body`](::Body). It is an efficient buffer type, /// These are returned by [`Body`](::Body). It is an efficient buffer type.
/// and wraps auto-management of flow control in the case of HTTP2 messages.
/// ///
/// A `Chunk` can be easily created by many of Rust's standard types that /// A `Chunk` can be easily created by many of Rust's standard types that
/// represent a collection of bytes, using `Chunk::from`. /// represent a collection of bytes, using `Chunk::from`.
pub struct Chunk { pub struct Chunk {
/// The buffer of bytes making up this body. /// The buffer of bytes making up this body.
bytes: Bytes, bytes: Bytes,
/// A possible HTTP2 marker to ensure we release window capacity.
///
/// This version just automatically releases all capacity when `Chunk`
/// is dropped.
_flow_control: Option<AutoRelease>,
} }
// An unexported type to prevent locking `Chunk::into_iter()` to `Bytes::into_iter()`. // An unexported type to prevent locking `Chunk::into_iter()` to `Bytes::into_iter()`.
@@ -26,29 +19,8 @@ pub struct IntoIter {
inner: <Bytes as IntoIterator>::IntoIter, inner: <Bytes as IntoIterator>::IntoIter,
} }
struct AutoRelease {
cap: usize,
release: ReleaseCapacity,
}
impl Drop for AutoRelease {
fn drop(&mut self) {
let _ = self.release.release_capacity(self.cap);
}
}
impl Chunk { impl Chunk {
pub(crate) fn h2(bytes: Bytes, rel_cap: &ReleaseCapacity) -> Chunk {
let cap = bytes.len();
Chunk {
bytes: bytes,
_flow_control: Some(AutoRelease {
cap: cap,
release: rel_cap.clone(),
}),
}
}
/// Converts this `Chunk` directly into the `Bytes` type without copies. /// Converts this `Chunk` directly into the `Bytes` type without copies.
/// ///
/// This is simply an inherent alias for `Bytes::from(chunk)`, which exists, /// This is simply an inherent alias for `Bytes::from(chunk)`, which exists,
@@ -109,7 +81,6 @@ impl From<Bytes> for Chunk {
fn from(bytes: Bytes) -> Chunk { fn from(bytes: Bytes) -> Chunk {
Chunk { Chunk {
bytes: bytes, bytes: bytes,
_flow_control: None,
} }
} }
} }