refactor: replace rand crate with fastrand
Replaces the `rand` crate with `fastrand` for improved performance in rate control and reflector selection.
This commit is contained in:
4 files changed
+12
-317
No files matched your search
@@ -1,8 +1,6 @@
|
||||
use crate::netlink::{Netlink, NetlinkError, Qdisc};
|
||||
use crate::{Config, ReflectorStats};
|
||||
use log::{debug, info, warn};
|
||||
use rand::seq::IndexedRandom;
|
||||
use rand::{RngExt, rng};
|
||||
use std::collections::HashMap;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
@@ -51,7 +49,7 @@ fn generate_initial_speeds(base_speed: f64, size: u32) -> Vec<f64> {
|
||||
let mut rates = Vec::new();
|
||||
|
||||
for _ in 0..size {
|
||||
rates.push((rng().random_range(0.0..1.0) as f64 * 0.2 + 0.75) * base_speed);
|
||||
rates.push((fastrand::f64() * 0.2 + 0.75) * base_speed);
|
||||
}
|
||||
|
||||
rates
|
||||
@@ -183,8 +181,7 @@ impl Ratecontroller {
|
||||
}
|
||||
|
||||
if state.delta_stat > delay_ms {
|
||||
let mut rng = rng();
|
||||
match state.safe_rates.choose(&mut rng) {
|
||||
match state.safe_rates.get(fastrand::usize(..state.safe_rates.len())) {
|
||||
Some(rnd_rate) => {
|
||||
state.next_rate =
|
||||
rnd_rate.min(0.9 * state.current_rate * state.load);
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
use crate::{Config, ReflectorStats};
|
||||
use log::{debug, info};
|
||||
use rand::seq::IndexedRandom;
|
||||
use rand::{RngExt, rng};
|
||||
use std::collections::HashMap;
|
||||
use std::net::IpAddr;
|
||||
use std::sync::mpsc::Receiver;
|
||||
@@ -24,8 +22,6 @@ impl ReflectorSelector {
|
||||
let baseline_sleep_time =
|
||||
Duration::from_secs_f64(self.config.tick_interval * std::f64::consts::PI);
|
||||
|
||||
let mut rng = rng();
|
||||
|
||||
// Initial wait of several seconds to allow some OWD data to build up
|
||||
sleep(baseline_sleep_time);
|
||||
|
||||
@@ -57,7 +53,7 @@ impl ReflectorSelector {
|
||||
}
|
||||
|
||||
for _ in 0..20 {
|
||||
let next_candidate = self.reflector_pool.choose(&mut rng).unwrap();
|
||||
let next_candidate = &self.reflector_pool[fastrand::usize(..self.reflector_pool.len())];
|
||||
if next_peers.contains(next_candidate) {
|
||||
continue;
|
||||
}
|
||||
@@ -110,7 +106,7 @@ impl ReflectorSelector {
|
||||
|
||||
// Shuffle the deck so we avoid overwhelming good reflectors (Fisher-Yates)
|
||||
for i in (1_usize..candidates.len()).rev() {
|
||||
let j = rng.random_range(0..(i + 1));
|
||||
let j = fastrand::usize(0..=i);
|
||||
candidates.swap(i, j);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user