From f5808c6c40f20bd9b830f5c4959a340cd39ed18d Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Fri, 25 Dec 2020 02:24:38 +0000 Subject: [PATCH] fix benches --- actix-service/benches/and_then.rs | 138 +++++++++--------- .../benches/unsafecell_vs_refcell.rs | 8 +- 2 files changed, 73 insertions(+), 73 deletions(-) diff --git a/actix-service/benches/and_then.rs b/actix-service/benches/and_then.rs index f4174dd7..c8aa315d 100644 --- a/actix-service/benches/and_then.rs +++ b/actix-service/benches/and_then.rs @@ -5,11 +5,14 @@ use actix_service::Service; use criterion::{criterion_main, Criterion}; use futures_util::future::join_all; use futures_util::future::TryFutureExt; -use std::cell::{RefCell, UnsafeCell}; use std::future::Future; use std::pin::Pin; use std::rc::Rc; use std::task::{Context, Poll}; +use std::{ + cell::{RefCell, UnsafeCell}, + marker::PhantomData, +}; /* * Test services A,B for AndThen service implementations @@ -28,71 +31,72 @@ async fn svc2(req: usize) -> Result { * Cut down version of actix_service::AndThenService based on actix-service::Cell */ -struct AndThenUC(Rc>); +struct AndThenUC(Rc>, PhantomData); -impl AndThenUC { +impl AndThenUC { fn new(a: A, b: B) -> Self where - A: Service, - B: Service, + A: Service, + B: Service, { - Self(Rc::new(UnsafeCell::new((a, b)))) + Self(Rc::new(UnsafeCell::new((a, b))), PhantomData) } } -impl Clone for AndThenUC { +impl Clone for AndThenUC { fn clone(&self) -> Self { - Self(self.0.clone()) + Self(self.0.clone(), PhantomData) } } -impl Service for AndThenUC +impl Service for AndThenUC where - A: Service, - B: Service, + A: Service, + B: Service, { - type Request = A::Request; type Response = B::Response; type Error = A::Error; - type Future = AndThenServiceResponse; + type Future = AndThenServiceResponse; fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll> { 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); AndThenServiceResponse { state: State::A(fut, Some(self.0.clone())), + _phantom: PhantomData, } } } #[pin_project::pin_project] -pub(crate) struct AndThenServiceResponse +pub(crate) struct AndThenServiceResponse where - A: Service, - B: Service, + A: Service, + B: Service, { #[pin] - state: State, + state: State, + _phantom: PhantomData, } #[pin_project::pin_project(project = StateProj)] -enum State +enum State where - A: Service, - B: Service, + A: Service, + B: Service, { A(#[pin] A::Future, Option>>), B(#[pin] B::Future), - Empty, + Empty(PhantomData), } -impl Future for AndThenServiceResponse +impl Future for AndThenServiceResponse where - A: Service, - B: Service, + A: Service, + B: Service, { type Output = Result; @@ -103,7 +107,7 @@ where StateProj::A(fut, b) => match fut.poll(cx)? { Poll::Ready(res) => { 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); this.state.set(State::B(fut)); self.poll(cx) @@ -111,10 +115,10 @@ where Poll::Pending => Poll::Pending, }, StateProj::B(fut) => fut.poll(cx).map(|r| { - this.state.set(State::Empty); + this.state.set(State::Empty(PhantomData)); r }), - StateProj::Empty => { + StateProj::Empty(_) => { panic!("future must not be polled after it returned `Poll::Ready`") } } @@ -125,39 +129,38 @@ where * AndThenRC - AndThen service based on RefCell */ -struct AndThenRC(Rc>); +struct AndThenRC(Rc>, PhantomData); -impl AndThenRC { +impl AndThenRC { fn new(a: A, b: B) -> Self where - A: Service, - B: Service, + A: Service, + B: Service, { - Self(Rc::new(RefCell::new((a, b)))) + Self(Rc::new(RefCell::new((a, b))), PhantomData) } } -impl Clone for AndThenRC { +impl Clone for AndThenRC { fn clone(&self) -> Self { - Self(self.0.clone()) + Self(self.0.clone(), PhantomData) } } -impl Service for AndThenRC +impl Service for AndThenRC where - A: Service, - B: Service, + A: Service, + B: Service, { - type Request = A::Request; type Response = B::Response; type Error = A::Error; - type Future = AndThenServiceResponseRC; + type Future = AndThenServiceResponseRC; fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll> { 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); AndThenServiceResponseRC { state: StateRC::A(fut, Some(self.0.clone())), @@ -166,30 +169,30 @@ where } #[pin_project::pin_project] -pub(crate) struct AndThenServiceResponseRC +pub(crate) struct AndThenServiceResponseRC where - A: Service, - B: Service, + A: Service, + B: Service, { #[pin] - state: StateRC, + state: StateRC, } #[pin_project::pin_project(project = StateRCProj)] -enum StateRC +enum StateRC where - A: Service, - B: Service, + A: Service, + B: Service, { A(#[pin] A::Future, Option>>), B(#[pin] B::Future), - Empty, + Empty(PhantomData), } -impl Future for AndThenServiceResponseRC +impl Future for AndThenServiceResponseRC where - A: Service, - B: Service, + A: Service, + B: Service, { type Output = Result; @@ -200,7 +203,7 @@ where StateRCProj::A(fut, b) => match fut.poll(cx)? { Poll::Ready(res) => { 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); this.state.set(StateRC::B(fut)); self.poll(cx) @@ -208,10 +211,10 @@ where Poll::Pending => Poll::Pending, }, StateRCProj::B(fut) => fut.poll(cx).map(|r| { - this.state.set(StateRC::Empty); + this.state.set(StateRC::Empty(PhantomData)); r }), - StateRCProj::Empty => { + StateRCProj::Empty(_) => { 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 */ -struct AndThenRCFuture(Rc>); +struct AndThenRCFuture(Rc>, PhantomData); -impl AndThenRCFuture { +impl AndThenRCFuture { fn new(a: A, b: B) -> Self where - A: Service, - B: Service, + A: Service, + B: Service, { - Self(Rc::new(RefCell::new((a, b)))) + Self(Rc::new(RefCell::new((a, b))), PhantomData) } } -impl Clone for AndThenRCFuture { +impl Clone for AndThenRCFuture { fn clone(&self) -> Self { - Self(self.0.clone()) + Self(self.0.clone(), PhantomData) } } -impl Service for AndThenRCFuture +impl Service for AndThenRCFuture where - A: Service + 'static, + A: Service + 'static, A::Future: 'static, - B: Service + 'static, + B: Service + 'static, B::Future: 'static, { - type Request = A::Request; type Response = B::Response; type Error = A::Error; type Future = BoxFuture; @@ -257,7 +259,7 @@ where 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 core = self.0.clone(); 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] pub fn bench_async_service(c: &mut Criterion, srv: S, name: &str) where - S: Service + Clone + 'static, + S: Service<(), Response = usize, Error = ()> + Clone + 'static, { let mut rt = actix_rt::System::new("test"); diff --git a/actix-service/benches/unsafecell_vs_refcell.rs b/actix-service/benches/unsafecell_vs_refcell.rs index a599795f..cdf91233 100644 --- a/actix-service/benches/unsafecell_vs_refcell.rs +++ b/actix-service/benches/unsafecell_vs_refcell.rs @@ -20,8 +20,7 @@ impl Clone for SrvUC { } } -impl Service for SrvUC { - type Request = (); +impl Service<()> for SrvUC { type Response = usize; type Error = (); type Future = Ready>; @@ -50,8 +49,7 @@ impl Clone for SrvRC { } } -impl Service for SrvRC { - type Request = (); +impl Service<()> for SrvRC { type Response = usize; type Error = (); type Future = Ready>; @@ -83,7 +81,7 @@ impl Service for SrvRC { /// async_service_direct time: [1.0908 us 1.1656 us 1.2613 us] pub fn bench_async_service(c: &mut Criterion, srv: S, name: &str) where - S: Service + Clone + 'static, + S: Service<(), Response = usize, Error = ()> + Clone + 'static, { let mut rt = actix_rt::System::new("test");