Skip to content

Commit

Permalink
fix: replacing DefaultHasher (#1926)
Browse files Browse the repository at this point in the history
Co-authored-by: Tushar Mathur <tusharmath@gmail.com>
  • Loading branch information
shashitnak and tusharmath committed May 13, 2024
1 parent ad2fbad commit 8b6db44
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 13 deletions.
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)
}
}

1 comment on commit 8b6db44

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Running 30s test @ http://localhost:8000/graphql

4 threads and 100 connections

Thread Stats Avg Stdev Max +/- Stdev
Latency 7.21ms 3.92ms 193.51ms 80.16%
Req/Sec 3.54k 202.58 3.92k 93.50%

422199 requests in 30.01s, 2.12GB read

Requests/sec: 14068.97

Transfer/sec: 72.21MB

Please sign in to comment.