Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replacing DefaultHasher #1926

Merged
merged 13 commits into from
May 13, 2024
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down Expand Up @@ -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]
Expand Down
4 changes: 2 additions & 2 deletions src/core/graphql/request_template.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -128,7 +128,7 @@ impl RequestTemplate {

impl<Ctx: PathGraphql + HasHeaders + GraphQLOperationContext> CacheKey<Ctx> 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()
Expand Down
6 changes: 3 additions & 3 deletions src/core/grpc/data_loader_request.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions src/core/grpc/request_template.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

use anyhow::Result;
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;
Expand Down Expand Up @@ -108,7 +108,7 @@ impl RenderedRequestTemplate {

impl<Ctx: PathString + HasHeaders> CacheKey<Ctx> 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()
Expand Down
7 changes: 4 additions & 3 deletions src/core/http/data_loader_request.rs
Original file line number Diff line number Diff line change
@@ -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<String>);

Expand Down Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions src/core/http/request_template.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -226,7 +226,7 @@ impl TryFrom<Endpoint> for RequestTemplate {

impl<Ctx: PathString + HasHeaders> CacheKey<Ctx> 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);
Expand Down
7 changes: 7 additions & 0 deletions tailcall-hasher/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "tailcall-hasher"
version = "0.1.0"
edition = "2021"

[dependencies]
fxhash = "0.2.1"
21 changes: 21 additions & 0 deletions tailcall-hasher/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
Loading