Skip to content

Commit

Permalink
chore: fold OriginKey hash into main hash to shrink ContextKey
Browse files Browse the repository at this point in the history
  • Loading branch information
tobz committed Jan 27, 2025
1 parent 0d1856f commit 0108709
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
20 changes: 9 additions & 11 deletions lib/saluki-context/src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<OriginKey>,
}

impl ContextKey {
pub fn origin_key(&self) -> Option<OriginKey> {
self.origin_key
}
hash: u64,
}
21 changes: 13 additions & 8 deletions lib/saluki-context/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -309,7 +309,9 @@ impl ContextResolver {
})
}

fn create_context_key<I, T>(&mut self, name: &str, tags: I, origin_info: Option<OriginInfo<'_>>) -> ContextKey
fn create_context_key<I, T>(
&mut self, name: &str, tags: I, origin_info: Option<OriginInfo<'_>>,
) -> (ContextKey, Option<OriginKey>)
where
I: IntoIterator<Item = T>,
T: AsRef<str>,
Expand All @@ -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<I, T>(&self, key: ContextKey, name: &str, tags: I) -> Option<Context>
fn create_context<I, T>(
&self, key: ContextKey, name: &str, tags: I, origin_key: Option<OriginKey>,
) -> Option<Context>
where
I: IntoIterator<Item = T>,
T: AsRef<str>,
Expand All @@ -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(),
Expand Down Expand Up @@ -371,14 +376,14 @@ impl ContextResolver {
I: IntoIterator<Item = T> + Clone,
T: AsRef<str>,
{
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.");

Expand Down

0 comments on commit 0108709

Please sign in to comment.