Skip to content

Commit

Permalink
Use faster Rng in RandomIdGenerator
Browse files Browse the repository at this point in the history
Represents a minimum a 11%-22% improvement in relevant benchmarks.

Fixes #808
  • Loading branch information
hdost committed Jun 9, 2023
1 parent 06d17f7 commit da365a2
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
3 changes: 2 additions & 1 deletion opentelemetry-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ once_cell = "1.10"
ordered-float = "3.4.0"
percent-encoding = { version = "2.0", optional = true }
rand = { version = "0.8", default-features = false, features = ["std", "std_rng"], optional = true }
rand_pcg = { version = "0.3", default-features = false, optional = true }
regex = { version = "1.0", optional = true }
serde = { version = "1.0", features = ["derive", "rc"], optional = true }
serde_json = { version = "1", optional = true }
Expand All @@ -41,7 +42,7 @@ pprof = { version = "0.11.1", features = ["flamegraph", "criterion"] }

[features]
default = ["trace"]
trace = ["opentelemetry_api/trace", "crossbeam-channel", "rand", "async-trait", "percent-encoding"]
trace = ["opentelemetry_api/trace", "crossbeam-channel", "rand", "rand_pcg", "async-trait", "percent-encoding"]
jaeger_remote_sampler = ["trace", "opentelemetry-http", "http", "serde", "serde_json", "url"]
logs = ["opentelemetry_api/logs", "crossbeam-channel", "async-trait", "serde_json"]
metrics = ["opentelemetry_api/metrics", "regex", "async-trait"]
Expand Down
5 changes: 3 additions & 2 deletions opentelemetry-sdk/src/trace/id_generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
pub(super) mod aws;

use opentelemetry_api::trace::{SpanId, TraceId};
use rand::{rngs, Rng};
use rand::{thread_rng, Rng, SeedableRng};
use rand_pcg::Pcg64Mcg;
use std::cell::RefCell;
use std::fmt;

Expand Down Expand Up @@ -35,5 +36,5 @@ impl IdGenerator for RandomIdGenerator {

thread_local! {
/// Store random number generator for each thread
static CURRENT_RNG: RefCell<rngs::ThreadRng> = RefCell::new(rngs::ThreadRng::default());
static CURRENT_RNG: RefCell<Pcg64Mcg> = RefCell::new(Pcg64Mcg::from_rng(thread_rng()).unwrap());
}

0 comments on commit da365a2

Please sign in to comment.