Skip to content

Commit cbfd51d

Browse files
feat: avoid creating SmallRng when only one number is required
1 parent fb90555 commit cbfd51d

File tree

10 files changed

+15
-23
lines changed

10 files changed

+15
-23
lines changed

frameworks/Rust/axum/Cargo.lock

Lines changed: 1 addition & 1 deletion
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "axum-techempower"
3-
version = "0.2.1"
3+
version = "0.3.0"
44
authors = ["Dragos Varovici <[email protected]>"]
55
edition = "2024"
66

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/src/common/mod.rs

Lines changed: 2 additions & 2 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::{distr::Uniform, rngs::SmallRng, Rng};
4+
use rand::{distr::Uniform, rngs::SmallRng, Rng, RngCore};
55
pub mod models;
66
pub mod utils;
77

@@ -36,7 +36,7 @@ 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 {
39+
pub fn random_id(rng: &mut impl RngCore) -> i32 {
4040
rng.random_range(1..=10_000)
4141
}
4242

frameworks/Rust/axum/src/main_mongo.rs

Lines changed: 2 additions & 4 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, 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 rng());
47-
48-
let random_id = rng.random_range(1..=10_000) as i32;
46+
let random_id = random_id(&mut rng());
4947

5048
let world = find_world_by_id(db, random_id)
5149
.await

frameworks/Rust/axum/src/main_mongo_raw.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ use mongodb::{
3535
use rand::{rngs::SmallRng, rng, SeedableRng};
3636

3737
async fn db(DatabaseConnection(db): DatabaseConnection) -> impl IntoResponse {
38-
let mut rng = SmallRng::from_rng(&mut rng());
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

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, 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 rng());
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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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 rng());
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)

frameworks/Rust/axum/src/main_sqlx.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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 rng());
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");

frameworks/Rust/axum/src/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn set_socket_options(addr: SocketAddr) -> io::Result<tokio::net::TcpListener> {
3737
}
3838

3939
/// Build an Axum server with consistent configuration, using the high-level API exposed
40-
/// by Axum 0.7. This is intended for convenience and intentionally does not provide much
40+
/// by Axum 0.8. This is intended for convenience and intentionally does not provide much
4141
/// customisability.
4242
#[allow(dead_code)]
4343
pub async fn serve(app: Router<()>, port: Option<u16>) {

0 commit comments

Comments
 (0)