perf: eliminate recursive call and unnecessary Option (#814)

This commit is contained in:
Yuki Okushi 2026-02-09 20:46:41 +09:00 committed by GitHub
parent c347906c78
commit dd205fd155
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 47 additions and 44 deletions

View File

@ -57,7 +57,7 @@ where
AndThenServiceResponse { AndThenServiceResponse {
state: State::A { state: State::A {
fut: self.0 .0.call(req), fut: self.0 .0.call(req),
b: Some(self.0.clone()), b: self.0.clone(),
}, },
} }
} }
@ -84,7 +84,7 @@ pin_project! {
A { A {
#[pin] #[pin]
fut: A::Future, fut: A::Future,
b: Option<Rc<(A, B)>>, b: Rc<(A, B)>,
}, },
B { B {
#[pin] #[pin]
@ -101,17 +101,17 @@ where
type Output = Result<B::Response, A::Error>; type Output = Result<B::Response, A::Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
loop {
let mut this = self.as_mut().project(); let mut this = self.as_mut().project();
match this.state.as_mut().project() { match this.state.as_mut().project() {
StateProj::A { fut, b } => { StateProj::A { fut, b } => {
let res = ready!(fut.poll(cx))?; let res = ready!(fut.poll(cx))?;
let b = b.take().unwrap();
let fut = b.1.call(res); let fut = b.1.call(res);
this.state.set(State::B { fut }); this.state.set(State::B { fut });
self.poll(cx)
} }
StateProj::B { fut } => fut.poll(cx), StateProj::B { fut } => return fut.poll(cx),
}
} }
} }
} }

View File

@ -209,24 +209,26 @@ where
type Output = Result<S, SF::InitError>; type Output = Result<S, SF::InitError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
loop {
let mut this = self.as_mut().project(); let mut this = self.as_mut().project();
match this.state.as_mut().project() { match this.state.as_mut().project() {
StateProj::A { fut } => { StateProj::A { fut } => {
let svc = ready!(fut.poll(cx))?; let svc = ready!(fut.poll(cx))?;
this.state.set(State::B { svc }); this.state.set(State::B { svc });
self.poll(cx)
} }
StateProj::B { svc } => { StateProj::B { svc } => {
ready!(svc.poll_ready(cx))?; ready!(svc.poll_ready(cx))?;
{
let fut = {
let (_, f) = &**this.store; let (_, f) = &**this.store;
let fut = f(this.cfg.take().unwrap(), svc); f(this.cfg.take().unwrap(), svc)
};
this.state.set(State::C { fut }); this.state.set(State::C { fut });
} }
self.poll(cx) StateProj::C { fut } => return fut.poll(cx),
} }
StateProj::C { fut } => fut.poll(cx),
} }
} }
} }

View File

@ -57,7 +57,7 @@ where
ThenServiceResponse { ThenServiceResponse {
state: State::A { state: State::A {
fut: self.0 .0.call(req), fut: self.0 .0.call(req),
b: Some(self.0.clone()), b: self.0.clone(),
}, },
} }
} }
@ -81,7 +81,7 @@ pin_project! {
A: Service<Req>, A: Service<Req>,
B: Service<Result<A::Response, A::Error>>, B: Service<Result<A::Response, A::Error>>,
{ {
A { #[pin] fut: A::Future, b: Option<Rc<(A, B)>> }, A { #[pin] fut: A::Future, b: Rc<(A, B)> },
B { #[pin] fut: B::Future }, B { #[pin] fut: B::Future },
} }
} }
@ -94,17 +94,17 @@ where
type Output = Result<B::Response, B::Error>; type Output = Result<B::Response, B::Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
loop {
let mut this = self.as_mut().project(); let mut this = self.as_mut().project();
match this.state.as_mut().project() { match this.state.as_mut().project() {
StateProj::A { fut, b } => { StateProj::A { fut, b } => {
let res = ready!(fut.poll(cx)); let res = ready!(fut.poll(cx));
let b = b.take().unwrap();
let fut = b.1.call(res); let fut = b.1.call(res);
this.state.set(State::B { fut }); this.state.set(State::B { fut });
self.poll(cx)
} }
StateProj::B { fut } => fut.poll(cx), StateProj::B { fut } => return fut.poll(cx),
}
} }
} }
} }

View File

@ -205,6 +205,7 @@ where
type Output = Result<T::Transform, T::InitError>; type Output = Result<T::Transform, T::InitError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
loop {
let mut this = self.as_mut().project(); let mut this = self.as_mut().project();
match this.state.as_mut().project() { match this.state.as_mut().project() {
@ -212,9 +213,9 @@ where
let srv = ready!(fut.poll(cx))?; let srv = ready!(fut.poll(cx))?;
let fut = this.store.0.new_transform(srv); let fut = this.store.0.new_transform(srv);
this.state.set(ApplyTransformFutureState::B { fut }); this.state.set(ApplyTransformFutureState::B { fut });
self.poll(cx)
} }
ApplyTransformFutureStateProj::B { fut } => fut.poll(cx), ApplyTransformFutureStateProj::B { fut } => return fut.poll(cx),
}
} }
} }
} }