Skip to content

Commit

Permalink
reduce stack size of router service (#1213)
Browse files Browse the repository at this point in the history
* reduce stack size of router service

* avoid double boxing service object
  • Loading branch information
fakeshadow authored Feb 25, 2025
1 parent 2c0dd7f commit 68e2e44
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 23 deletions.
2 changes: 1 addition & 1 deletion http/src/h1/body.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::{
cell::{RefCell, RefMut},
future::{Future, poll_fn},
future::poll_fn,
ops::DerefMut,
pin::Pin,
task::{Context, Poll, Waker},
Expand Down
2 changes: 1 addition & 1 deletion http/src/h1/dispatcher.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::{
convert::Infallible,
future::{Future, pending, poll_fn},
future::{pending, poll_fn},
marker::PhantomData,
net::SocketAddr,
pin::{Pin, pin},
Expand Down
2 changes: 1 addition & 1 deletion http/src/h1/dispatcher_uring.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::{
cell::RefCell,
fmt,
future::{Future, poll_fn},
future::poll_fn,
marker::PhantomData,
mem,
net::SocketAddr,
Expand Down
1 change: 0 additions & 1 deletion http/src/tls/native_tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ pub(crate) use native_tls::TlsAcceptor;
use core::{
convert::Infallible,
fmt,
future::Future,
pin::Pin,
task::{Context, Poll},
};
Expand Down
2 changes: 0 additions & 2 deletions http/src/util/futures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ pub(crate) use queue::*;

#[cfg(any(feature = "http2", feature = "http3"))]
mod queue {
use core::future::Future;

use futures_util::stream::{FuturesUnordered, StreamExt};

pub(crate) struct Queue<F>(FuturesUnordered<F>);
Expand Down
2 changes: 1 addition & 1 deletion http/src/util/middleware/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! middleware for adding typed state to service request.
use core::{fmt, future::Future};
use core::fmt;

use xitca_service::Service;

Expand Down
2 changes: 1 addition & 1 deletion http/src/util/service/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(non_snake_case)]

use core::{convert::Infallible, future::Future, marker::PhantomData, net::SocketAddr};
use core::{convert::Infallible, marker::PhantomData, net::SocketAddr};

use xitca_service::{Service, pipeline::PipelineE};

Expand Down
21 changes: 8 additions & 13 deletions http/src/util/service/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub use self::object::RouteObject;
/// in order to determine how the router type-erases node services.
pub struct Router<Obj> {
// record for last time PathGen is called with certain route string prefix.
prefix: Option<usize>,
prefix: usize,
routes: HashMap<String, Obj>,
}

Expand All @@ -34,7 +34,7 @@ impl<Obj> Default for Router<Obj> {
impl<Obj> Router<Obj> {
pub fn new() -> Self {
Router {
prefix: None,
prefix: 0,
routes: HashMap::new(),
}
}
Expand Down Expand Up @@ -183,8 +183,7 @@ where
path.pop();
}

let prefix = self.prefix.get_or_insert(0);
*prefix += path.len();
self.prefix += path.len();

self.routes.iter_mut().for_each(|(_, v)| {
v.path_gen(path.as_str());
Expand Down Expand Up @@ -436,7 +435,7 @@ mod service {
pub struct RouterService<S> {
// a length record of prefix of current router.
// when it's Some the request path has to be sliced to exclude the string path prefix.
pub(super) prefix: Option<usize>,
pub(super) prefix: usize,
pub(super) router: xitca_router::Router<S>,
}

Expand All @@ -452,15 +451,11 @@ mod service {
// using async fn call directly would cause significant code bloating.
#[allow(clippy::manual_async_fn)]
#[inline]
fn call(&self, mut req: Req) -> impl core::future::Future<Output = Result<Self::Response, Self::Error>> {
fn call(&self, mut req: Req) -> impl Future<Output = Result<Self::Response, Self::Error>> {
async {
let mut path = req.borrow().path();

if let Some(prefix) = self.prefix {
path = &path[prefix..];
}

let xitca_router::Match { value, params } = self.router.at(path).map_err(RouterError::Match)?;
let path = req.borrow().path();
let xitca_router::Match { value, params } =
self.router.at(&path[self.prefix..]).map_err(RouterError::Match)?;
*req.borrow_mut() = params;
Service::call(value, req).await
}
Expand Down
3 changes: 1 addition & 2 deletions http/src/util/timer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::{
future::Future,
use core::{
pin::Pin,
task::{Context, Poll, ready},
};
Expand Down

0 comments on commit 68e2e44

Please sign in to comment.