Skip to content

Commit e2c4405

Browse files
perf: improvements to random number generation
1 parent 5e6cb1b commit e2c4405

File tree

7 files changed

+22
-32
lines changed

7 files changed

+22
-32
lines changed

frameworks/Rust/axum/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ built with Tokio, Tower, and Hyper.
2727
## Notable Points (both performance and build)
2828

2929
- Use of `async`.
30-
- Use of most recent versions of Rust, `axum` and dependencies.
30+
- Use of the most recent versions of Rust, `axum` and dependencies.
3131
- (Disabled by default) Compile-time swap-in of `simd-json` instead of `serde_json` for faster JSON serialization.
3232
- Release binaries are stripped and compiled with CPU native.
3333
- Sockets configured with `TCP_NODELAY` and to support an increased number of pending connections.
34+
- For very simple benchmarks, use of a separate, single-threaded Tokio runtime for each thread.
3435
- Server configured to serve HTTP/1 only, with no need for websockets.
3536
- Separation of build and deployment containers using multi-stage builds.
3637
- Deployment into Google's minimal `distroless-cc` container.

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,10 @@ pub fn random_id(rng: &mut SmallRng) -> i32 {
4040
rng.gen_range(1..10_001)
4141
}
4242

43-
/// Generate vector of integers in the range 1 to 10,000 (inclusive)
43+
/// Generate an iterator of integers in the range 1 to 10,000 (inclusive)
4444
#[allow(dead_code)]
4545
#[inline(always)]
46-
pub fn random_ids(rng: &mut SmallRng, count: usize) -> Vec<i32> {
46+
pub fn random_ids(rng: &mut SmallRng, count: usize) -> impl Iterator<Item = i32> + use<'_> {
4747
rng.sample_iter(Uniform::new(1, 10_001))
4848
.take(count)
49-
.collect()
5049
}

frameworks/Rust/axum/src/main_mongo.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ use axum::Json;
1414
#[cfg(feature = "simd-json")]
1515
use common::simd_json::Json;
1616
use common::{
17-
models::{FortuneInfo, World},
18-
random_ids,
17+
models::{FortuneInfo, World}, random_id
1918
};
2019
use dotenv::dotenv;
2120
use mongodb::{
@@ -58,9 +57,7 @@ async fn queries(
5857
let q = parse_params(params);
5958

6059
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
61-
let ids = random_ids(&mut rng, q);
62-
63-
let worlds = find_worlds(db, ids).await;
60+
let worlds = find_worlds(db, &mut rng, q).await;
6461
let results = worlds.expect("worlds could not be retrieved");
6562

6663
(StatusCode::OK, Json(results))
@@ -73,17 +70,14 @@ async fn updates(
7370
let q = parse_params(params);
7471

7572
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
76-
let ids = random_ids(&mut rng, q);
7773

78-
let worlds = find_worlds(db.clone(), ids)
74+
let worlds = find_worlds(db.clone(), &mut rng, q)
7975
.await
8076
.expect("worlds could not be retrieved");
8177
let mut updated_worlds: Vec<World> = Vec::with_capacity(q);
8278

8379
for mut world in worlds {
84-
let random_number = (rng.gen::<u32>() % 10_000 + 1) as i32;
85-
86-
world.random_number = random_number;
80+
world.random_number = random_id(&mut rng);
8781
updated_worlds.push(world);
8882
}
8983

frameworks/Rust/axum/src/main_mongo_raw.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ mod common;
22
mod mongo_raw;
33
mod server;
44

5-
use common::{models::World, random_id, random_ids};
5+
use common::{models::World, random_id};
66
use mongo_raw::database::{
77
find_world_by_id, find_worlds, update_worlds, DatabaseConnection,
88
};
@@ -27,7 +27,7 @@ use mongodb::{
2727
options::{ClientOptions, Compressor},
2828
Client,
2929
};
30-
use rand::{rngs::SmallRng, thread_rng, Rng, SeedableRng};
30+
use rand::{rngs::SmallRng, thread_rng, SeedableRng};
3131

3232
async fn db(DatabaseConnection(db): DatabaseConnection) -> impl IntoResponse {
3333
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
@@ -48,9 +48,7 @@ async fn queries(
4848
let q = parse_params(params);
4949

5050
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
51-
let ids = random_ids(&mut rng, q);
52-
53-
let worlds = find_worlds(db, ids).await;
51+
let worlds = find_worlds(db, &mut rng, q).await;
5452
let results = worlds.expect("worlds could not be retrieved");
5553

5654
(StatusCode::OK, Json(results))
@@ -64,16 +62,13 @@ async fn updates(
6462

6563
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
6664

67-
let ids = random_ids(&mut rng, q);
68-
let worlds = find_worlds(db.clone(), ids)
65+
let worlds = find_worlds(db.clone(), &mut rng, q)
6966
.await
7067
.expect("worlds could not be retrieved");
7168
let mut updated_worlds: Vec<World> = Vec::with_capacity(q);
7269

7370
for mut world in worlds {
74-
let random_number = (rng.gen::<u32>() % 10_000 + 1) as i32;
75-
76-
world.random_number = random_number;
71+
world.random_number = random_id(&mut rng);
7772
updated_worlds.push(world);
7873
}
7974

frameworks/Rust/axum/src/main_sqlx.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ async fn queries(
5555
) -> impl IntoResponse {
5656
let mut rng = SmallRng::from_rng(&mut thread_rng()).unwrap();
5757
let count = parse_params(params);
58-
let ids = random_ids(&mut rng, count);
5958
let mut worlds: Vec<World> = Vec::with_capacity(count);
6059

61-
for id in &ids {
60+
for id in random_ids(&mut rng, count) {
6261
let world: World = ::sqlx::query_as(common::SELECT_WORLD_BY_ID)
6362
.bind(id)
6463
.fetch_one(&mut *db.acquire().await.unwrap())

frameworks/Rust/axum/src/mongo/database.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use std::{convert::Infallible, io};
33
use axum::{async_trait, extract::FromRequestParts, http::request::Parts};
44
use futures_util::{stream::FuturesUnordered, StreamExt, TryStreamExt};
55
use mongodb::{bson::doc, Database};
6+
use rand::rngs::SmallRng;
67

7-
use crate::common::models::{Fortune, World};
8+
use crate::common::{models::{Fortune, World}, random_ids};
89

910
pub struct DatabaseConnection(pub Database);
1011

@@ -52,10 +53,10 @@ pub async fn find_world_by_id(db: Database, id: i32) -> Result<World, MongoError
5253
Ok(world)
5354
}
5455

55-
pub async fn find_worlds(db: Database, ids: Vec<i32>) -> Result<Vec<World>, MongoError> {
56+
pub async fn find_worlds(db: Database, rng: &mut SmallRng, count: usize) -> Result<Vec<World>, MongoError> {
5657
let future_worlds = FuturesUnordered::new();
5758

58-
for id in ids {
59+
for id in random_ids(rng, count) {
5960
future_worlds.push(find_world_by_id(db.clone(), id));
6061
}
6162

frameworks/Rust/axum/src/mongo_raw/database.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use mongodb::{
66
bson::{doc, RawDocumentBuf},
77
Database,
88
};
9+
use rand::rngs::SmallRng;
910

10-
use crate::common::models::World;
11+
use crate::common::{models::World, random_ids};
1112

1213
pub struct DatabaseConnection(pub Database);
1314

@@ -69,10 +70,10 @@ pub async fn find_world_by_id(db: Database, id: i32) -> Result<World, MongoError
6970
})
7071
}
7172

72-
pub async fn find_worlds(db: Database, ids: Vec<i32>) -> Result<Vec<World>, MongoError> {
73+
pub async fn find_worlds(db: Database, rng: &mut SmallRng, count: usize) -> Result<Vec<World>, MongoError> {
7374
let future_worlds = FuturesUnordered::new();
7475

75-
for id in ids {
76+
for id in random_ids(rng, count) {
7677
future_worlds.push(find_world_by_id(db.clone(), id));
7778
}
7879

0 commit comments

Comments
 (0)