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

View File

@ -209,24 +209,26 @@ where
type Output = Result<S, SF::InitError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project();
loop {
let mut this = self.as_mut().project();
match this.state.as_mut().project() {
StateProj::A { fut } => {
let svc = ready!(fut.poll(cx))?;
this.state.set(State::B { svc });
}
StateProj::B { svc } => {
ready!(svc.poll_ready(cx))?;
let fut = {
let (_, f) = &**this.store;
f(this.cfg.take().unwrap(), svc)
};
match this.state.as_mut().project() {
StateProj::A { fut } => {
let svc = ready!(fut.poll(cx))?;
this.state.set(State::B { svc });
self.poll(cx)
}
StateProj::B { svc } => {
ready!(svc.poll_ready(cx))?;
{
let (_, f) = &**this.store;
let fut = f(this.cfg.take().unwrap(), svc);
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 {
state: State::A {
fut: self.0 .0.call(req),
b: Some(self.0.clone()),
b: self.0.clone(),
},
}
}
@ -81,7 +81,7 @@ pin_project! {
A: Service<Req>,
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 },
}
}
@ -94,17 +94,17 @@ where
type Output = Result<B::Response, B::Error>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project();
loop {
let mut this = self.as_mut().project();
match this.state.as_mut().project() {
StateProj::A { fut, b } => {
let res = ready!(fut.poll(cx));
let b = b.take().unwrap();
let fut = b.1.call(res);
this.state.set(State::B { fut });
self.poll(cx)
match this.state.as_mut().project() {
StateProj::A { fut, b } => {
let res = ready!(fut.poll(cx));
let fut = b.1.call(res);
this.state.set(State::B { fut });
}
StateProj::B { fut } => return fut.poll(cx),
}
StateProj::B { fut } => fut.poll(cx),
}
}
}

View File

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