Hmmm, a most basic example with app state, is also too heavy. I took this project and simplfied as much as possible.
[dependencies]
tokio = { version= "1", features = ["full"] }
warp = "0.3"
use std::{collections::HashMap, convert::Infallible, sync::Arc};
use tokio::sync::Mutex;
use warp::{http::StatusCode, reply::{self, Html, WithStatus}, Filter, Rejection, Reply};
mod handlers;
mod models;
type ItemsDb = Arc<Mutex<HashMap<usize, usize>>>;
type Result<T> = std::result::Result<T, Rejection>;
pub async fn handler(items_db: ItemsDb) -> Result<impl Reply> {
let mut l = items_db.lock().await;
l.insert(12345, 6790);
Ok(reply::with_status(
reply::html("delete success"),
StatusCode::OK,
))
}
fn with_items_db(
items_db: ItemsDb,
) -> impl Filter<Extract = (ItemsDb,), Error = Infallible> + Clone {
warp::any().map(move || items_db.clone())
}
#[tokio::main]
async fn main() {
let items_db: ItemsDb = Arc::new(Mutex::new(HashMap::new()));
let root = warp::path::end().and(with_items_db(items_db)).and_then(handler);
let routes = root
.with(warp::cors().allow_any_origin());
warp::serve(routes).run(([127, 0, 0, 1], 5000)).await;
}
results: 134K LOC of LLVM:
Lines Copies Function name
----- ------ -------------
134384 3393 (TOTAL)
2275 (1.7%, 1.7%) 1 (0.0%, 0.0%) h2::proto::connection::DynConnection<B>::recv_frame
2256 (1.7%, 3.4%) 4 (0.1%, 0.1%) hyper::proto::h1::io::WriteBuf<B>::buffer
2059 (1.5%, 4.9%) 1 (0.0%, 0.2%) h2::proto::streams::prioritize::Prioritize::pop_frame
1800 (1.3%, 6.2%) 1 (0.0%, 0.2%) hyper::proto::h2::server::Serving<T,B>::poll_server
1787 (1.3%, 7.6%) 1 (0.0%, 0.2%) h2::codec::framed_write::Encoder<B>::buffer
1670 (1.2%, 8.8%) 1 (0.0%, 0.3%) hyper::proto::h2::server::H2Stream<F,B>::poll2
1628 (1.2%, 10.0%) 23 (0.7%, 0.9%) std::thread::local::LocalKey<T>::try_with
1360 (1.0%, 11.0%) 40 (1.2%, 2.1%) std::panic::catch_unwind
1232 (0.9%, 12.0%) 40 (1.2%, 3.3%) tokio::loom::std::unsafe_cell::UnsafeCell<T>::with_mut
1188 (0.9%, 12.8%) 8 (0.2%, 3.5%) tokio::runtime::task::core::Cell<T,S>::new
1058 (0.8%, 13.6%) 1 (0.0%, 3.6%) <h2::server::Handshake<T,B> as core::future::future::Future>::poll
1023 (0.8%, 14.4%) 1 (0.0%, 3.6%) hyper::proto::h1::dispatch::Dispatcher<D,Bs,I,T>::poll_write
936 (0.7%, 15.1%) 1 (0.0%, 3.6%) <hyper::proto::h2::PipeToSendStream<S> as core::future::future::Future>::poll
880 (0.7%, 15.7%) 40 (1.2%, 4.8%) std::panicking::try::do_catch
864 (0.6%, 16.4%) 1 (0.0%, 4.8%) hyper::proto::h1::io::Buffered<T,B>::parse
853 (0.6%, 17.0%) 1 (0.0%, 4.9%) hyper::proto::h1::conn::Conn<I,B,T>::poll_read_body
827 (0.6%, 17.6%) 1 (0.0%, 4.9%) hyper::proto::h1::conn::Conn<I,B,T>::require_empty_read
So, in this metric, Axum actually is the lightest server. Hmm. And I can tell from observation that cargo-checking a minimal app with actix and warp took notably longer.