fix benches

This commit is contained in:
Rob Ede 2020-12-25 02:24:38 +00:00
parent 6e19dde3e7
commit f5808c6c40
No known key found for this signature in database
GPG Key ID: C2A3B36E841A91E6
2 changed files with 73 additions and 73 deletions

View File

@ -5,11 +5,14 @@ use actix_service::Service;
use criterion::{criterion_main, Criterion}; use criterion::{criterion_main, Criterion};
use futures_util::future::join_all; use futures_util::future::join_all;
use futures_util::future::TryFutureExt; use futures_util::future::TryFutureExt;
use std::cell::{RefCell, UnsafeCell};
use std::future::Future; use std::future::Future;
use std::pin::Pin; use std::pin::Pin;
use std::rc::Rc; use std::rc::Rc;
use std::task::{Context, Poll}; use std::task::{Context, Poll};
use std::{
cell::{RefCell, UnsafeCell},
marker::PhantomData,
};
/* /*
* Test services A,B for AndThen service implementations * Test services A,B for AndThen service implementations
@ -28,71 +31,72 @@ async fn svc2(req: usize) -> Result<usize, ()> {
* Cut down version of actix_service::AndThenService based on actix-service::Cell * Cut down version of actix_service::AndThenService based on actix-service::Cell
*/ */
struct AndThenUC<A, B>(Rc<UnsafeCell<(A, B)>>); struct AndThenUC<A, Req, B>(Rc<UnsafeCell<(A, B)>>, PhantomData<Req>);
impl<A, B> AndThenUC<A, B> { impl<A, Req, B> AndThenUC<A, Req, B> {
fn new(a: A, b: B) -> Self fn new(a: A, b: B) -> Self
where where
A: Service, A: Service<Req>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
Self(Rc::new(UnsafeCell::new((a, b)))) Self(Rc::new(UnsafeCell::new((a, b))), PhantomData)
} }
} }
impl<A, B> Clone for AndThenUC<A, B> { impl<A, Req, B> Clone for AndThenUC<A, Req, B> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self(self.0.clone()) Self(self.0.clone(), PhantomData)
} }
} }
impl<A, B> Service for AndThenUC<A, B> impl<A, Req, B> Service<Req> for AndThenUC<A, Req, B>
where where
A: Service, A: Service<Req>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
type Request = A::Request;
type Response = B::Response; type Response = B::Response;
type Error = A::Error; type Error = A::Error;
type Future = AndThenServiceResponse<A, B>; type Future = AndThenServiceResponse<A, Req, B>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(())) Poll::Ready(Ok(()))
} }
fn call(&mut self, req: A::Request) -> Self::Future { fn call(&mut self, req: Req) -> Self::Future {
let fut = unsafe { &mut *(*self.0).get() }.0.call(req); let fut = unsafe { &mut *(*self.0).get() }.0.call(req);
AndThenServiceResponse { AndThenServiceResponse {
state: State::A(fut, Some(self.0.clone())), state: State::A(fut, Some(self.0.clone())),
_phantom: PhantomData,
} }
} }
} }
#[pin_project::pin_project] #[pin_project::pin_project]
pub(crate) struct AndThenServiceResponse<A, B> pub(crate) struct AndThenServiceResponse<A, Req, B>
where where
A: Service, A: Service<Req>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
#[pin] #[pin]
state: State<A, B>, state: State<A, Req, B>,
_phantom: PhantomData<Req>,
} }
#[pin_project::pin_project(project = StateProj)] #[pin_project::pin_project(project = StateProj)]
enum State<A, B> enum State<A, Req, B>
where where
A: Service, A: Service<Req>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
A(#[pin] A::Future, Option<Rc<UnsafeCell<(A, B)>>>), A(#[pin] A::Future, Option<Rc<UnsafeCell<(A, B)>>>),
B(#[pin] B::Future), B(#[pin] B::Future),
Empty, Empty(PhantomData<Req>),
} }
impl<A, B> Future for AndThenServiceResponse<A, B> impl<A, Req, B> Future for AndThenServiceResponse<A, Req, B>
where where
A: Service, A: Service<Req>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
type Output = Result<B::Response, A::Error>; type Output = Result<B::Response, A::Error>;
@ -103,7 +107,7 @@ where
StateProj::A(fut, b) => match fut.poll(cx)? { StateProj::A(fut, b) => match fut.poll(cx)? {
Poll::Ready(res) => { Poll::Ready(res) => {
let b = b.take().unwrap(); let b = b.take().unwrap();
this.state.set(State::Empty); // drop fut A this.state.set(State::Empty(PhantomData)); // drop fut A
let fut = unsafe { &mut (*b.get()).1 }.call(res); let fut = unsafe { &mut (*b.get()).1 }.call(res);
this.state.set(State::B(fut)); this.state.set(State::B(fut));
self.poll(cx) self.poll(cx)
@ -111,10 +115,10 @@ where
Poll::Pending => Poll::Pending, Poll::Pending => Poll::Pending,
}, },
StateProj::B(fut) => fut.poll(cx).map(|r| { StateProj::B(fut) => fut.poll(cx).map(|r| {
this.state.set(State::Empty); this.state.set(State::Empty(PhantomData));
r r
}), }),
StateProj::Empty => { StateProj::Empty(_) => {
panic!("future must not be polled after it returned `Poll::Ready`") panic!("future must not be polled after it returned `Poll::Ready`")
} }
} }
@ -125,39 +129,38 @@ where
* AndThenRC - AndThen service based on RefCell * AndThenRC - AndThen service based on RefCell
*/ */
struct AndThenRC<A, B>(Rc<RefCell<(A, B)>>); struct AndThenRC<A, Req, B>(Rc<RefCell<(A, B)>>, PhantomData<Req>);
impl<A, B> AndThenRC<A, B> { impl<A, Req, B> AndThenRC<A, Req, B> {
fn new(a: A, b: B) -> Self fn new(a: A, b: B) -> Self
where where
A: Service, A: Service<Req>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
Self(Rc::new(RefCell::new((a, b)))) Self(Rc::new(RefCell::new((a, b))), PhantomData)
} }
} }
impl<A, B> Clone for AndThenRC<A, B> { impl<A, Req, B> Clone for AndThenRC<A, Req, B> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self(self.0.clone()) Self(self.0.clone(), PhantomData)
} }
} }
impl<A, B> Service for AndThenRC<A, B> impl<A, Req, B> Service<Req> for AndThenRC<A, Req, B>
where where
A: Service, A: Service<Req>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
type Request = A::Request;
type Response = B::Response; type Response = B::Response;
type Error = A::Error; type Error = A::Error;
type Future = AndThenServiceResponseRC<A, B>; type Future = AndThenServiceResponseRC<A, Req, B>;
fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
Poll::Ready(Ok(())) Poll::Ready(Ok(()))
} }
fn call(&mut self, req: A::Request) -> Self::Future { fn call(&mut self, req: Req) -> Self::Future {
let fut = self.0.borrow_mut().0.call(req); let fut = self.0.borrow_mut().0.call(req);
AndThenServiceResponseRC { AndThenServiceResponseRC {
state: StateRC::A(fut, Some(self.0.clone())), state: StateRC::A(fut, Some(self.0.clone())),
@ -166,30 +169,30 @@ where
} }
#[pin_project::pin_project] #[pin_project::pin_project]
pub(crate) struct AndThenServiceResponseRC<A, B> pub(crate) struct AndThenServiceResponseRC<A, Req, B>
where where
A: Service, A: Service<Req>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
#[pin] #[pin]
state: StateRC<A, B>, state: StateRC<A, Req, B>,
} }
#[pin_project::pin_project(project = StateRCProj)] #[pin_project::pin_project(project = StateRCProj)]
enum StateRC<A, B> enum StateRC<A, Req, B>
where where
A: Service, A: Service<Req>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
A(#[pin] A::Future, Option<Rc<RefCell<(A, B)>>>), A(#[pin] A::Future, Option<Rc<RefCell<(A, B)>>>),
B(#[pin] B::Future), B(#[pin] B::Future),
Empty, Empty(PhantomData<Req>),
} }
impl<A, B> Future for AndThenServiceResponseRC<A, B> impl<A, Req, B> Future for AndThenServiceResponseRC<A, Req, B>
where where
A: Service, A: Service<Req>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
type Output = Result<B::Response, A::Error>; type Output = Result<B::Response, A::Error>;
@ -200,7 +203,7 @@ where
StateRCProj::A(fut, b) => match fut.poll(cx)? { StateRCProj::A(fut, b) => match fut.poll(cx)? {
Poll::Ready(res) => { Poll::Ready(res) => {
let b = b.take().unwrap(); let b = b.take().unwrap();
this.state.set(StateRC::Empty); // drop fut A this.state.set(StateRC::Empty(PhantomData)); // drop fut A
let fut = b.borrow_mut().1.call(res); let fut = b.borrow_mut().1.call(res);
this.state.set(StateRC::B(fut)); this.state.set(StateRC::B(fut));
self.poll(cx) self.poll(cx)
@ -208,10 +211,10 @@ where
Poll::Pending => Poll::Pending, Poll::Pending => Poll::Pending,
}, },
StateRCProj::B(fut) => fut.poll(cx).map(|r| { StateRCProj::B(fut) => fut.poll(cx).map(|r| {
this.state.set(StateRC::Empty); this.state.set(StateRC::Empty(PhantomData));
r r
}), }),
StateRCProj::Empty => { StateRCProj::Empty(_) => {
panic!("future must not be polled after it returned `Poll::Ready`") panic!("future must not be polled after it returned `Poll::Ready`")
} }
} }
@ -223,32 +226,31 @@ where
* and standard futures::future::and_then combinator in a Box * and standard futures::future::and_then combinator in a Box
*/ */
struct AndThenRCFuture<A, B>(Rc<RefCell<(A, B)>>); struct AndThenRCFuture<A, Req, B>(Rc<RefCell<(A, B)>>, PhantomData<Req>);
impl<A, B> AndThenRCFuture<A, B> { impl<A, Req, B> AndThenRCFuture<A, Req, B> {
fn new(a: A, b: B) -> Self fn new(a: A, b: B) -> Self
where where
A: Service, A: Service<Req>,
B: Service<Request = A::Response, Error = A::Error>, B: Service<A::Response, Error = A::Error>,
{ {
Self(Rc::new(RefCell::new((a, b)))) Self(Rc::new(RefCell::new((a, b))), PhantomData)
} }
} }
impl<A, B> Clone for AndThenRCFuture<A, B> { impl<A, Req, B> Clone for AndThenRCFuture<A, Req, B> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self(self.0.clone()) Self(self.0.clone(), PhantomData)
} }
} }
impl<A, B> Service for AndThenRCFuture<A, B> impl<A, Req, B> Service<Req> for AndThenRCFuture<A, Req, B>
where where
A: Service + 'static, A: Service<Req> + 'static,
A::Future: 'static, A::Future: 'static,
B: Service<Request = A::Response, Error = A::Error> + 'static, B: Service<A::Response, Error = A::Error> + 'static,
B::Future: 'static, B::Future: 'static,
{ {
type Request = A::Request;
type Response = B::Response; type Response = B::Response;
type Error = A::Error; type Error = A::Error;
type Future = BoxFuture<Self::Response, Self::Error>; type Future = BoxFuture<Self::Response, Self::Error>;
@ -257,7 +259,7 @@ where
Poll::Ready(Ok(())) Poll::Ready(Ok(()))
} }
fn call(&mut self, req: A::Request) -> Self::Future { fn call(&mut self, req: Req) -> Self::Future {
let fut = self.0.borrow_mut().0.call(req); let fut = self.0.borrow_mut().0.call(req);
let core = self.0.clone(); let core = self.0.clone();
let fut2 = move |res| (*core).borrow_mut().1.call(res); let fut2 = move |res| (*core).borrow_mut().1.call(res);
@ -281,7 +283,7 @@ where
/// async_service_direct time: [1.0908 us 1.1656 us 1.2613 us] /// async_service_direct time: [1.0908 us 1.1656 us 1.2613 us]
pub fn bench_async_service<S>(c: &mut Criterion, srv: S, name: &str) pub fn bench_async_service<S>(c: &mut Criterion, srv: S, name: &str)
where where
S: Service<Request = (), Response = usize, Error = ()> + Clone + 'static, S: Service<(), Response = usize, Error = ()> + Clone + 'static,
{ {
let mut rt = actix_rt::System::new("test"); let mut rt = actix_rt::System::new("test");

View File

@ -20,8 +20,7 @@ impl Clone for SrvUC {
} }
} }
impl Service for SrvUC { impl Service<()> for SrvUC {
type Request = ();
type Response = usize; type Response = usize;
type Error = (); type Error = ();
type Future = Ready<Result<Self::Response, ()>>; type Future = Ready<Result<Self::Response, ()>>;
@ -50,8 +49,7 @@ impl Clone for SrvRC {
} }
} }
impl Service for SrvRC { impl Service<()> for SrvRC {
type Request = ();
type Response = usize; type Response = usize;
type Error = (); type Error = ();
type Future = Ready<Result<Self::Response, ()>>; type Future = Ready<Result<Self::Response, ()>>;
@ -83,7 +81,7 @@ impl Service for SrvRC {
/// async_service_direct time: [1.0908 us 1.1656 us 1.2613 us] /// async_service_direct time: [1.0908 us 1.1656 us 1.2613 us]
pub fn bench_async_service<S>(c: &mut Criterion, srv: S, name: &str) pub fn bench_async_service<S>(c: &mut Criterion, srv: S, name: &str)
where where
S: Service<Request = (), Response = usize, Error = ()> + Clone + 'static, S: Service<(), Response = usize, Error = ()> + Clone + 'static,
{ {
let mut rt = actix_rt::System::new("test"); let mut rt = actix_rt::System::new("test");