From 8b6db449e419cc6be2e89b53bd9fd98f219694ea Mon Sep 17 00:00:00 2001 From: Shashi Kant Date: Mon, 13 May 2024 13:41:25 +0000 Subject: [PATCH] fix: replacing `DefaultHasher` (#1926) Co-authored-by: Tushar Mathur --- Cargo.lock | 8 ++++++++ Cargo.toml | 4 +++- src/core/graphql/request_template.rs | 4 ++-- src/core/grpc/data_loader_request.rs | 6 +++--- src/core/grpc/request_template.rs | 4 ++-- src/core/http/data_loader_request.rs | 7 ++++--- src/core/http/request_template.rs | 4 ++-- tailcall-hasher/Cargo.toml | 7 +++++++ tailcall-hasher/src/lib.rs | 21 +++++++++++++++++++++ 9 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 tailcall-hasher/Cargo.toml create mode 100644 tailcall-hasher/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index b90d9e49fe..c6df003e56 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5128,6 +5128,7 @@ dependencies = [ "stripmargin", "strum_macros 0.26.2", "tailcall-fixtures", + "tailcall-hasher", "tailcall-macros", "tailcall-prettier", "tailcall-tracker", @@ -5209,6 +5210,13 @@ dependencies = [ "indenter", ] +[[package]] +name = "tailcall-hasher" +version = "0.1.0" +dependencies = [ + "fxhash", +] + [[package]] name = "tailcall-macros" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 36d4dda135..e3bdf2f73f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -153,6 +153,7 @@ tonic-types = "0.11.0" datatest-stable = "0.2.9" tokio-test = "0.4.4" base64 = "0.22.1" +tailcall-hasher = { path = "tailcall-hasher" } [dev-dependencies] tailcall-prettier = { path = "tailcall-prettier" } @@ -211,7 +212,8 @@ members = [ "tailcall-query-plan", "tailcall-fixtures", "tailcall-upstream-grpc", - "tailcall-tracker"] + "tailcall-tracker", + "tailcall-hasher"] # Boost execution_spec snapshot diffing performance [profile.dev.package] diff --git a/src/core/graphql/request_template.rs b/src/core/graphql/request_template.rs index e11872597b..b8bcdffb8d 100644 --- a/src/core/graphql/request_template.rs +++ b/src/core/graphql/request_template.rs @@ -1,11 +1,11 @@ #![allow(clippy::too_many_arguments)] -use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; use derive_setters::Setters; use hyper::HeaderMap; use reqwest::header::HeaderValue; +use tailcall_hasher::TailcallHasher; use crate::core::config::{GraphQLOperationType, KeyValue}; use crate::core::has_headers::HasHeaders; @@ -128,7 +128,7 @@ impl RequestTemplate { impl CacheKey for RequestTemplate { fn cache_key(&self, ctx: &Ctx) -> u64 { - let mut hasher = DefaultHasher::new(); + let mut hasher = TailcallHasher::default(); let graphql_query = self.render_graphql_query(ctx); graphql_query.hash(&mut hasher); hasher.finish() diff --git a/src/core/grpc/data_loader_request.rs b/src/core/grpc/data_loader_request.rs index aec499e15d..cbacf2e787 100644 --- a/src/core/grpc/data_loader_request.rs +++ b/src/core/grpc/data_loader_request.rs @@ -1,8 +1,8 @@ -use std::collections::hash_map::DefaultHasher; use std::collections::BTreeSet; use std::hash::{Hash, Hasher}; use anyhow::Result; +use tailcall_hasher::TailcallHasher; use super::request_template::RenderedRequestTemplate; @@ -28,11 +28,11 @@ impl Hash for DataLoaderRequest { impl PartialEq for DataLoaderRequest { fn eq(&self, other: &Self) -> bool { - let mut hasher_self = DefaultHasher::new(); + let mut hasher_self = TailcallHasher::default(); self.hash(&mut hasher_self); let hash_self = hasher_self.finish(); - let mut hasher_other = DefaultHasher::new(); + let mut hasher_other = TailcallHasher::default(); other.hash(&mut hasher_other); let hash_other = hasher_other.finish(); diff --git a/src/core/grpc/request_template.rs b/src/core/grpc/request_template.rs index 6902b46f7e..a7f4cb4241 100644 --- a/src/core/grpc/request_template.rs +++ b/src/core/grpc/request_template.rs @@ -1,4 +1,3 @@ -use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; use anyhow::Result; @@ -6,6 +5,7 @@ use derive_setters::Setters; use hyper::header::CONTENT_TYPE; use hyper::{HeaderMap, Method}; use reqwest::header::HeaderValue; +use tailcall_hasher::TailcallHasher; use url::Url; use super::request::create_grpc_request; @@ -108,7 +108,7 @@ impl RenderedRequestTemplate { impl CacheKey for RequestTemplate { fn cache_key(&self, ctx: &Ctx) -> u64 { - let mut hasher = DefaultHasher::new(); + let mut hasher = TailcallHasher::default(); let rendered_req = self.render(ctx).unwrap(); rendered_req.hash(&mut hasher); hasher.finish() diff --git a/src/core/http/data_loader_request.rs b/src/core/http/data_loader_request.rs index 0779add058..755a52b293 100644 --- a/src/core/http/data_loader_request.rs +++ b/src/core/http/data_loader_request.rs @@ -1,8 +1,9 @@ -use std::collections::hash_map::DefaultHasher; use std::collections::BTreeSet; use std::hash::{Hash, Hasher}; use std::ops::Deref; +use tailcall_hasher::TailcallHasher; + #[derive(Debug)] pub struct DataLoaderRequest(reqwest::Request, BTreeSet); @@ -41,11 +42,11 @@ impl Hash for DataLoaderRequest { impl PartialEq for DataLoaderRequest { fn eq(&self, other: &Self) -> bool { - let mut hasher_self = DefaultHasher::new(); + let mut hasher_self = TailcallHasher::default(); self.hash(&mut hasher_self); let hash_self = hasher_self.finish(); - let mut hasher_other = DefaultHasher::new(); + let mut hasher_other = TailcallHasher::default(); other.hash(&mut hasher_other); let hash_other = hasher_other.finish(); diff --git a/src/core/http/request_template.rs b/src/core/http/request_template.rs index 0ef8b50273..42f4c62870 100644 --- a/src/core/http/request_template.rs +++ b/src/core/http/request_template.rs @@ -1,10 +1,10 @@ use std::borrow::Cow; -use std::collections::hash_map::DefaultHasher; use std::hash::{Hash, Hasher}; use derive_setters::Setters; use hyper::HeaderMap; use reqwest::header::HeaderValue; +use tailcall_hasher::TailcallHasher; use url::Url; use crate::core::config::Encoding; @@ -226,7 +226,7 @@ impl TryFrom for RequestTemplate { impl CacheKey for RequestTemplate { fn cache_key(&self, ctx: &Ctx) -> u64 { - let mut hasher = DefaultHasher::new(); + let mut hasher = TailcallHasher::default(); let state = &mut hasher; self.method.hash(state); diff --git a/tailcall-hasher/Cargo.toml b/tailcall-hasher/Cargo.toml new file mode 100644 index 0000000000..9674e74221 --- /dev/null +++ b/tailcall-hasher/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "tailcall-hasher" +version = "0.1.0" +edition = "2021" + +[dependencies] +fxhash = "0.2.1" \ No newline at end of file diff --git a/tailcall-hasher/src/lib.rs b/tailcall-hasher/src/lib.rs new file mode 100644 index 0000000000..e84e773b0f --- /dev/null +++ b/tailcall-hasher/src/lib.rs @@ -0,0 +1,21 @@ +use std::hash::Hasher; + +use fxhash::FxHasher; + +/// A hasher that uses the FxHash algorithm. Currently it's a dumb wrapper +/// around `fxhash::FxHasher`. We could potentially add some custom logic here +/// in the future. +#[derive(Default)] +pub struct TailcallHasher { + hasher: FxHasher, +} + +impl Hasher for TailcallHasher { + fn finish(&self) -> u64 { + self.hasher.finish() + } + + fn write(&mut self, bytes: &[u8]) { + self.hasher.write(bytes) + } +}