Remove allocation when calling handler (#190)

`Handler::call` already returns a boxed future so we don't have to box
it again.
This commit is contained in:
David Pedersen 2021-08-16 09:19:37 +02:00 committed by GitHub
parent 57e440ed2e
commit a128a672a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 11 deletions

View File

@ -1,7 +1,8 @@
//! Handler future types. //! Handler future types.
use crate::body::{box_body, BoxBody}; use crate::body::{box_body, BoxBody};
use futures_util::future::{BoxFuture, Either}; use crate::util::{Either, EitherProj};
use futures_util::{future::BoxFuture, ready};
use http::{Method, Request, Response}; use http::{Method, Request, Response};
use http_body::Empty; use http_body::Empty;
use pin_project_lite::pin_project; use pin_project_lite::pin_project;
@ -21,7 +22,7 @@ pin_project! {
{ {
#[pin] #[pin]
pub(super) inner: Either< pub(super) inner: Either<
BoxFuture<'static, Result<Response<BoxBody>, F::Error>>, BoxFuture<'static, Response<BoxBody>>,
Oneshot<F, Request<B>>, Oneshot<F, Request<B>>,
>, >,
pub(super) req_method: Method, pub(super) req_method: Method,
@ -36,7 +37,11 @@ where
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project(); let this = self.project();
let response = futures_util::ready!(this.inner.poll(cx))?; let response = match this.inner.project() {
EitherProj::A { inner } => ready!(inner.poll(cx)),
EitherProj::B { inner } => ready!(inner.poll(cx))?,
};
if this.req_method == &Method::HEAD { if this.req_method == &Method::HEAD {
let response = response.map(|_| box_body(Empty::new())); let response = response.map(|_| box_body(Empty::new()));
Poll::Ready(Ok(response)) Poll::Ready(Ok(response))

View File

@ -6,10 +6,10 @@ use crate::{
response::IntoResponse, response::IntoResponse,
routing::{EmptyRouter, MethodFilter}, routing::{EmptyRouter, MethodFilter},
service::{HandleError, HandleErrorFromRouter}, service::{HandleError, HandleErrorFromRouter},
util::Either,
}; };
use async_trait::async_trait; use async_trait::async_trait;
use bytes::Bytes; use bytes::Bytes;
use futures_util::future::Either;
use http::{Request, Response}; use http::{Request, Response};
use std::{ use std::{
convert::Infallible, convert::Infallible,
@ -601,15 +601,11 @@ where
let req_method = req.method().clone(); let req_method = req.method().clone();
let fut = if self.method.matches(req.method()) { let fut = if self.method.matches(req.method()) {
let handler = self.handler.clone(); let fut = Handler::call(self.handler.clone(), req);
let fut = Box::pin(async move { Either::A { inner: fut }
let res = Handler::call(handler, req).await;
Ok::<_, F::Error>(res)
}) as futures_util::future::BoxFuture<'static, _>;
Either::Left(fut)
} else { } else {
let fut = self.fallback.clone().oneshot(req); let fut = self.fallback.clone().oneshot(req);
Either::Right(fut) Either::B { inner: fut }
}; };
future::OnMethodFuture { future::OnMethodFuture {