From 0108709beaa594e5dc178f747ce6f493afe9247a Mon Sep 17 00:00:00 2001 From: Toby Lawrence Date: Mon, 27 Jan 2025 11:52:50 -0500 Subject: [PATCH] chore: fold OriginKey hash into main hash to shrink ContextKey --- lib/saluki-context/src/hash.rs | 20 +++++++++----------- lib/saluki-context/src/resolver.rs | 21 +++++++++++++-------- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/saluki-context/src/hash.rs b/lib/saluki-context/src/hash.rs index 327a3019..d208bd09 100644 --- a/lib/saluki-context/src/hash.rs +++ b/lib/saluki-context/src/hash.rs @@ -60,9 +60,11 @@ where seen.clear(); let mut hasher = ahash::AHasher::default(); + + // Hash the metric name. name.hash(&mut hasher); - // Hash the tags individually and XOR their hashes together, which allows us to be order-oblivious: + // Hash the metric tags individually and XOR their hashes together, which allows us to be order-oblivious: let mut combined_tags_hash = 0; for tag in tags { @@ -78,19 +80,15 @@ where hasher.write_u64(combined_tags_hash); - let metric_key = hasher.finish(); + // Finally, hash the origin key. + if let Some(origin_key) = origin_key { + origin_key.hash(&mut hasher); + } - ContextKey { metric_key, origin_key } + ContextKey { hash: hasher.finish() } } #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] pub struct ContextKey { - metric_key: u64, - origin_key: Option, -} - -impl ContextKey { - pub fn origin_key(&self) -> Option { - self.origin_key - } + hash: u64, } diff --git a/lib/saluki-context/src/resolver.rs b/lib/saluki-context/src/resolver.rs index 6ef6f442..0eadf680 100644 --- a/lib/saluki-context/src/resolver.rs +++ b/lib/saluki-context/src/resolver.rs @@ -11,7 +11,7 @@ use crate::{ context::{Context, ContextInner}, expiry::{Expiration, ExpirationBuilder, ExpiryCapableLifecycle}, hash::{hash_context_with_seen, new_fast_hashset, ContextKey, FastHashSet}, - origin::{OriginInfo, OriginTags, OriginTagsResolver}, + origin::{OriginInfo, OriginKey, OriginTags, OriginTagsResolver}, tags::TagSet, }; @@ -309,7 +309,9 @@ impl ContextResolver { }) } - fn create_context_key(&mut self, name: &str, tags: I, origin_info: Option>) -> ContextKey + fn create_context_key( + &mut self, name: &str, tags: I, origin_info: Option>, + ) -> (ContextKey, Option) where I: IntoIterator, T: AsRef, @@ -321,10 +323,14 @@ impl ContextResolver { .as_ref() .and_then(|resolver| origin_info.and_then(|info| resolver.resolve_origin_key(info))); - hash_context_with_seen(name, tags, origin_key, &mut self.hash_seen_buffer) + let context_key = hash_context_with_seen(name, tags, origin_key, &mut self.hash_seen_buffer); + + (context_key, origin_key) } - fn create_context(&self, key: ContextKey, name: &str, tags: I) -> Option + fn create_context( + &self, key: ContextKey, name: &str, tags: I, origin_key: Option, + ) -> Option where I: IntoIterator, T: AsRef, @@ -340,8 +346,7 @@ impl ContextResolver { // Collect any enriched tags based on the origin key of the context, if any. let origin_tags = match self.origin_tags_resolver.as_ref() { - Some(resolver) => key - .origin_key() + Some(resolver) => origin_key .map(|key| OriginTags::from_resolved(key, Arc::clone(resolver))) .unwrap_or_else(OriginTags::empty), None => OriginTags::empty(), @@ -371,14 +376,14 @@ impl ContextResolver { I: IntoIterator + Clone, T: AsRef, { - let context_key = self.create_context_key(name, tags.clone(), origin_info); + let (context_key, origin_key) = self.create_context_key(name, tags.clone(), origin_info); match self.context_cache.get(&context_key) { Some(context) => { self.stats.resolved_existing_context_total().increment(1); self.expiration.mark_entry_accessed(context_key); Some(context) } - None => match self.create_context(context_key, name, tags) { + None => match self.create_context(context_key, name, tags, origin_key) { Some(context) => { debug!(?context_key, ?context, "Resolved new context.");