Skip to content

Commit fd94caa

Browse files
committed
add hashbrown and ahash as optional dependency
1 parent d191cf7 commit fd94caa

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

opentelemetry-sdk/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ rust-version = "1.65"
1212
[dependencies]
1313
opentelemetry = { version = "0.21", path = "../opentelemetry/" }
1414
opentelemetry-http = { version = "0.10", path = "../opentelemetry-http", optional = true }
15+
ahash = { version = "0.8", optional = true }
1516
async-std = { workspace = true, features = ["unstable"], optional = true }
1617
async-trait = { workspace = true, optional = true }
1718
crossbeam-channel = { version = "0.5", optional = true }
1819
futures-channel = "0.3"
1920
futures-executor = { workspace = true }
2021
futures-util = { workspace = true, features = ["std", "sink", "async-await-macro"] }
22+
hashbrown = { version = "0.14", optional = true }
2123
once_cell = { workspace = true }
2224
ordered-float = { workspace = true }
2325
percent-encoding = { version = "2.0", optional = true }
@@ -54,6 +56,7 @@ testing = ["opentelemetry/testing", "trace", "metrics", "logs", "rt-async-std",
5456
rt-tokio = ["tokio", "tokio-stream"]
5557
rt-tokio-current-thread = ["tokio", "tokio-stream"]
5658
rt-async-std = ["async-std"]
59+
use_hashbrown = ["hashbrown", "ahash"]
5760

5861
[[bench]]
5962
name = "context"

opentelemetry-sdk/src/metrics/internal/sum.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
use std::sync::atomic::{AtomicBool, Ordering};
22
use std::{
3-
collections::{hash_map::Entry, HashMap},
43
sync::{Arc, Mutex},
54
time::SystemTime,
65
};
76

87
use crate::attributes::AttributeSet;
98
use crate::metrics::data::{self, Aggregation, DataPoint, Temporality};
109
use opentelemetry::{global, metrics::MetricsError};
11-
use std::collections::hash_map::DefaultHasher;
1210
use std::hash::{Hash, Hasher};
1311

12+
#[cfg(feature = "use_hashbrown")]
13+
use ahash::AHasher;
14+
#[cfg(feature = "use_hashbrown")]
15+
use hashbrown::{hash_map::Entry, HashMap};
16+
17+
#[cfg(not(feature = "use_hashbrown"))]
18+
use std::collections::{
19+
hash_map::{DefaultHasher, Entry},
20+
HashMap,
21+
};
22+
1423
use super::{
1524
aggregate::{is_under_cardinality_limit, STREAM_OVERFLOW_ATTRIBUTE_SET},
1625
AtomicTracker, Number,
@@ -48,7 +57,11 @@ impl<T: Number<T>> ValueMap<T> {
4857

4958
// Hash function to determine the bucket
5059
fn hash_to_bucket(key: &AttributeSet) -> u8 {
60+
#[cfg(not(feature = "use_hashbrown"))]
5161
let mut hasher = DefaultHasher::new();
62+
#[cfg(feature = "use_hashbrown")]
63+
let mut hasher = AHasher::default();
64+
5265
key.hash(&mut hasher);
5366
// Use the 8 least significant bits directly, avoiding the modulus operation.
5467
hasher.finish() as u8

stress/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ ctrlc = "3.2.5"
2424
lazy_static = "1.4.0"
2525
num_cpus = "1.15.0"
2626
opentelemetry = { path = "../opentelemetry", features = ["metrics", "logs", "trace"] }
27-
opentelemetry_sdk = { path = "../opentelemetry-sdk", features = ["metrics", "logs", "trace"] }
27+
opentelemetry_sdk = { path = "../opentelemetry-sdk", features = ["metrics", "logs", "trace", "use_hashbrown"] }
2828
opentelemetry-appender-tracing = { path = "../opentelemetry-appender-tracing"}
2929
rand = { version = "0.8.4", features = ["small_rng"] }
3030
tracing = { workspace = true, features = ["std"]}

0 commit comments

Comments
 (0)