Skip to content

Commit f479884

Browse files
[rust/axum] dependencies and performance (#9785)
* feat: upgraded deps, rust version and edition * feat: avoid creating SmallRng when only one number is required * feat: update rust version
1 parent 4c41eed commit f479884

File tree

15 files changed

+396
-244
lines changed

15 files changed

+396
-244
lines changed

frameworks/Rust/axum/Cargo.lock

Lines changed: 344 additions & 179 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frameworks/Rust/axum/Cargo.toml

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[package]
22
name = "axum-techempower"
3-
version = "0.2.1"
3+
version = "0.3.0"
44
authors = ["Dragos Varovici <[email protected]>"]
5-
edition = "2021"
5+
edition = "2024"
66

77
[[bin]]
88
name = "axum"
@@ -39,49 +39,49 @@ simd-json = [
3939
]
4040

4141
[dependencies]
42-
axum = { version = "0.7.9", default-features = false, features = [
42+
axum = { version = "0.8.3", default-features = false, features = [
4343
"json",
4444
"query",
4545
"http1",
4646
"tokio",
4747
] }
48-
deadpool = { version = "0.12.1", features = ["rt_tokio_1", "serde", "managed"] }
48+
deadpool = { version = "0.12.2", features = ["rt_tokio_1", "serde", "managed"] }
4949
deadpool-postgres = { version = "0.14.1", features = ["rt_tokio_1", "serde"] }
5050
dotenv = "0.15.0"
5151
futures = "0.3.31"
5252
futures-util = "0.3.31"
53-
mongodb = { version = "3.1.1", features = [
53+
mongodb = { version = "3.2.3", features = [
5454
"zstd-compression",
5555
"snappy-compression",
5656
"zlib-compression",
5757
] }
5858
num_cpus = "1.16.0"
59-
rand = { version = "0.8.5", features = ["small_rng"] }
60-
serde = { version = "1.0.216", features = ["derive"] }
61-
serde_json = "1.0.134"
62-
sqlx = { version = "0.8.2", features = [
59+
rand = { version = "0.9.0", features = ["small_rng"] }
60+
serde = { version = "1.0.219", features = ["derive"] }
61+
serde_json = "1.0.140"
62+
sqlx = { version = "0.8.3", features = [
6363
"postgres",
6464
"macros",
6565
"runtime-tokio",
6666
"tls-rustls",
6767
] }
68-
tokio = { version = "1.42.0", features = ["full"] }
68+
tokio = { version = "1.44.2", features = ["full"] }
6969
tokio-pg-mapper = { version = "0.2.0" }
7070
tokio-pg-mapper-derive = { version = "0.2.0" }
71-
tokio-postgres = { version = "0.7.12" }
71+
tokio-postgres = { version = "0.7.13" }
7272
tower = { version = "0.5.2", features = ["util"] }
7373
tower-http = { version = "0.6.2", features = ["set-header"] }
7474
yarte = "0.15.7"
75-
simd-json = { version = "0.14.3", optional = true }
76-
axum-core = { version = "0.4.5", optional = true }
75+
simd-json = { version = "0.15.0", optional = true }
76+
axum-core = { version = "0.5.2", optional = true }
7777
mime = { version = "0.3.17", optional = true }
78-
bytes = { version = "1.9.0", optional = true }
79-
serde_path_to_error = { version = "0.1.16", optional = true }
80-
socket2 = "0.5.8"
81-
hyper = { version = "1.5", features = ["server", "http1"] }
78+
bytes = { version = "1.10.1", optional = true }
79+
serde_path_to_error = { version = "0.1.17", optional = true }
80+
socket2 = "0.5.9"
81+
hyper = { version = "1.6", features = ["server", "http1"] }
8282
hyper-util = { version = "0.1", features = ["tokio", "server-auto", "http1"] }
83-
quick_cache = "0.6.9"
84-
mimalloc = "0.1.43"
83+
quick_cache = "0.6.12"
84+
mimalloc = "0.1.45"
8585

8686

8787
[profile.release]

frameworks/Rust/axum/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,4 @@ built with Tokio, Tower, and Hyper.
4040
- Use of PostgreSQL prepared statements cache (where supported).
4141
- Use of PostgreSQL arrays to execute multi-row database updates with a single `UPDATE` query.
4242
- This is permitted by the [test requirements](https://github.com/TechEmpower/FrameworkBenchmarks/wiki/Project-Information-Framework-Tests-Overview#database-updates), step (ix).
43-
- More performance improvements are to be expected in version 0.8:
44-
- https://github.com/tokio-rs/axum/issues/1827
43+
- Use of a fast PRNG

frameworks/Rust/axum/axum.dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM docker.io/rust:1.83-slim-bookworm AS builder
1+
FROM docker.io/rust:1.86-slim-bookworm AS builder
22

33
RUN apt-get update && apt-get install -y --no-install-recommends \
44
pkg-config libssl-dev \

frameworks/Rust/axum/src/common/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{env, str::FromStr};
22

33
use core::fmt::Debug;
4-
use rand::{distributions::Uniform, rngs::SmallRng, Rng};
4+
use rand::{distr::Uniform, rngs::SmallRng, Rng, RngCore};
55
pub mod models;
66
pub mod utils;
77

@@ -36,14 +36,14 @@ where
3636
/// Generate a single integer in the range 1 to 10,000 (inclusive)
3737
#[allow(dead_code)]
3838
#[inline(always)]
39-
pub fn random_id(rng: &mut SmallRng) -> i32 {
40-
rng.gen_range(1..10_001)
39+
pub fn random_id(rng: &mut impl RngCore) -> i32 {
40+
rng.random_range(1..=10_000)
4141
}
4242

4343
/// Generate an iterator of integers in the range 1 to 10,000 (inclusive)
4444
#[allow(dead_code)]
4545
#[inline(always)]
4646
pub fn random_ids(rng: &mut SmallRng, count: usize) -> impl Iterator<Item = i32> + use<'_> {
47-
rng.sample_iter(Uniform::new(1, 10_001))
47+
rng.sample_iter(Uniform::new_inclusive(1, 10_000).unwrap())
4848
.take(count)
4949
}

frameworks/Rust/axum/src/main_mongo.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use mongodb::{
2121
options::{ClientOptions, Compressor},
2222
Client,
2323
};
24-
use rand::{rngs::SmallRng, thread_rng, Rng, SeedableRng};
24+
use rand::{rngs::SmallRng, rng, SeedableRng};
2525
use yarte::Template;
2626
use mimalloc::MiMalloc;
2727

@@ -43,9 +43,7 @@ pub struct FortunesTemplate<'a> {
4343
}
4444

4545
async fn db(DatabaseConnection(db): DatabaseConnection) -> impl IntoResponse {
46-
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
47-
48-
let random_id = (rng.gen::<u32>() % 10_000 + 1) as i32;
46+
let random_id = random_id(&mut rng());
4947

5048
let world = find_world_by_id(db, random_id)
5149
.await
@@ -60,7 +58,7 @@ async fn queries(
6058
) -> impl IntoResponse {
6159
let q = parse_params(params);
6260

63-
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
61+
let mut rng = SmallRng::from_rng(&mut rng());
6462
let worlds = find_worlds(db, &mut rng, q).await;
6563
let results = worlds.expect("worlds could not be retrieved");
6664

@@ -73,7 +71,7 @@ async fn updates(
7371
) -> impl IntoResponse {
7472
let q = parse_params(params);
7573

76-
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
74+
let mut rng = SmallRng::from_rng(&mut rng());
7775

7876
let worlds = find_worlds(db.clone(), &mut rng, q)
7977
.await

frameworks/Rust/axum/src/main_mongo_raw.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@ use mongodb::{
3232
options::{ClientOptions, Compressor},
3333
Client,
3434
};
35-
use rand::{rngs::SmallRng, thread_rng, SeedableRng};
35+
use rand::{rngs::SmallRng, rng, SeedableRng};
3636

3737
async fn db(DatabaseConnection(db): DatabaseConnection) -> impl IntoResponse {
38-
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
39-
40-
let random_id = random_id(&mut rng);
38+
let random_id = random_id(&mut rng());
4139

4240
let world = find_world_by_id(db, random_id)
4341
.await
@@ -52,7 +50,7 @@ async fn queries(
5250
) -> impl IntoResponse {
5351
let q = parse_params(params);
5452

55-
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
53+
let mut rng = SmallRng::from_rng(&mut rng());
5654
let worlds = find_worlds(db, &mut rng, q).await;
5755
let results = worlds.expect("worlds could not be retrieved");
5856

@@ -65,7 +63,7 @@ async fn updates(
6563
) -> impl IntoResponse {
6664
let q = parse_params(params);
6765

68-
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
66+
let mut rng = SmallRng::from_rng(&mut rng());
6967

7068
let worlds = find_worlds(db.clone(), &mut rng, q)
7169
.await

frameworks/Rust/axum/src/main_pg.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use axum::{
55
extract::Query, http::StatusCode, response::IntoResponse, routing::get, Router,
66
};
77
use dotenv::dotenv;
8-
use rand::{rngs::SmallRng, thread_rng, SeedableRng};
8+
use rand::rng;
99
use yarte::Template;
1010
use mimalloc::MiMalloc;
1111

@@ -33,10 +33,9 @@ pub struct FortunesTemplate<'a> {
3333
}
3434

3535
async fn db(DatabaseConnection(conn): DatabaseConnection) -> impl IntoResponse {
36-
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
37-
36+
let id = random_id(&mut rng());
3837
let world = conn
39-
.fetch_world_by_id(random_id(&mut rng))
38+
.fetch_world_by_id(id)
4039
.await
4140
.expect("error loading world");
4241

frameworks/Rust/axum/src/main_pg_pool.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use common::simd_json::Json;
1313
use common::{random_ids, SELECT_ALL_FORTUNES, SELECT_WORLD_BY_ID, UPDATE_WORLDS};
1414
use dotenv::dotenv;
1515
use futures_util::{stream::FuturesUnordered, TryStreamExt};
16-
use rand::{rngs::SmallRng, thread_rng, SeedableRng};
16+
use rand::{rngs::SmallRng, rng, SeedableRng};
1717
use yarte::Template;
1818
use mimalloc::MiMalloc;
1919

@@ -38,8 +38,7 @@ pub struct FortunesTemplate<'a> {
3838
}
3939

4040
async fn db(DatabaseClient(client): DatabaseClient) -> impl IntoResponse {
41-
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
42-
let random_id = random_id(&mut rng);
41+
let random_id = random_id(&mut rng());
4342

4443
let select = &client.prepare_cached(SELECT_WORLD_BY_ID).await.unwrap();
4544
let world = fetch_world_by_id(&client, random_id, select)
@@ -55,7 +54,7 @@ async fn queries(
5554
) -> impl IntoResponse {
5655
let q = parse_params(params);
5756

58-
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
57+
let mut rng = SmallRng::from_rng(&mut rng());
5958
let select = &client.prepare_cached(SELECT_WORLD_BY_ID).await.unwrap();
6059
let future_worlds = FuturesUnordered::new();
6160

@@ -98,7 +97,7 @@ async fn updates(
9897
) -> impl IntoResponse {
9998
let q = parse_params(params);
10099

101-
let mut rng = SmallRng::from_entropy();
100+
let mut rng = SmallRng::from_rng(&mut rng());
102101
let select = &client.prepare_cached(SELECT_WORLD_BY_ID).await.unwrap();
103102
let update = &client.prepare_cached(UPDATE_WORLDS).await.unwrap();
104103

frameworks/Rust/axum/src/main_sqlx.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use axum::{
1313
};
1414
use dotenv::dotenv;
1515
use quick_cache::sync::Cache;
16-
use rand::{rngs::SmallRng, thread_rng, SeedableRng};
16+
use rand::{rngs::SmallRng, rng, SeedableRng};
1717
use sqlx::models::World;
1818
use yarte::Template;
1919
use mimalloc::MiMalloc;
@@ -42,10 +42,9 @@ pub struct FortunesTemplate<'a> {
4242
}
4343

4444
async fn db(State(AppState { db, .. }): State<AppState>) -> impl IntoResponse {
45-
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
46-
45+
let id = random_id(&mut rng());
4746
let world: World = ::sqlx::query_as(common::SELECT_WORLD_BY_ID)
48-
.bind(random_id(&mut rng))
47+
.bind(id)
4948
.fetch_one(&mut *db.acquire().await.unwrap())
5049
.await
5150
.expect("error loading world");
@@ -57,7 +56,7 @@ async fn queries(
5756
State(AppState { db, .. }): State<AppState>,
5857
Query(params): Query<Params>,
5958
) -> impl IntoResponse {
60-
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
59+
let mut rng = SmallRng::from_rng(&mut rng());
6160
let count = parse_params(params);
6261
let mut worlds: Vec<World> = Vec::with_capacity(count);
6362

@@ -100,7 +99,7 @@ async fn cache(
10099
Query(params): Query<Params>,
101100
) -> impl IntoResponse {
102101
let count = parse_params(params);
103-
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
102+
let mut rng = SmallRng::from_rng(&mut rng());
104103
let mut worlds: Vec<Option<World>> = Vec::with_capacity(count);
105104

106105
for id in random_ids(&mut rng, count) {

0 commit comments

Comments
 (0)