Skip to content

Commit

Permalink
perf: Replace hash usage with gxhash (#1028)
Browse files Browse the repository at this point in the history
* perf: Replace hash usage with gxhash

* Update ci.yml

* Update cloudbuild.yaml

* Update Dockerfile
  • Loading branch information
XAMPPRocky authored Oct 10, 2024
1 parent 1d6b455 commit ad5cf87
Show file tree
Hide file tree
Showing 12 changed files with 41 additions and 35 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true

env:
CARGO_TERM_COLOR: always
RUSTFLAGS: "-C target-feature=+aes,+vaes,+avx2"

jobs:
lint:
name: Lint
Expand Down
10 changes: 10 additions & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ strum_macros = "0.26"
cfg-if = "1.0.0"
libflate = "2.0.0"
form_urlencoded = "1.2.1"
gxhash = "3.4.1"

[dependencies.hyper-util]
version = "0.1"
Expand Down Expand Up @@ -236,4 +237,4 @@ schemars = { version = "0.8.15", features = ["bytes", "url"] }
url = { version = "2.4.1", features = ["serde"] }

[workspace.lints.clippy]
undocumented_unsafe_blocks = "deny"
undocumented_unsafe_blocks = "deny"
2 changes: 2 additions & 0 deletions build/build-image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ ARG RUST_TOOLCHAIN
ENV RUSTUP_HOME=/usr/local/rustup \
CARGO_HOME=/usr/local/cargo \
PATH=/usr/local/cargo/bin:$PATH \
CARGO_TERM_COLOR=always \
RUSTFLAGS="-C target-feature=+aes,+vaes,+avx2" \
LC_ALL=C.UTF-8 \
LANG=C.UTF-8

Expand Down
2 changes: 2 additions & 0 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ options:
- "CARGO_HOME=/workspace/.cargo"
- "REPOSITORY=${_REPOSITORY}"
- "BUILD_IMAGE_TAG=${_BUILD_IMAGE_TAG}"
- "CARGO_TERM_COLOR=always"
- 'RUSTFLAGS="-C target-feature=+aes,+vaes,+avx2"'
machineType: E2_HIGHCPU_32
dynamic_substitutions: true
timeout: 7200s
Expand Down
16 changes: 3 additions & 13 deletions src/components/proxy/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,7 @@ impl Hash for PipelineError {
}
}

pub struct SeahashBuilder;

impl std::hash::BuildHasher for SeahashBuilder {
type Hasher = seahash::SeaHasher;

fn build_hasher(&self) -> Self::Hasher {
seahash::SeaHasher::new()
}
}

pub type ErrorMap = std::collections::HashMap<PipelineError, u64, SeahashBuilder>;
pub type ErrorMap = gxhash::HashMap<PipelineError, u64>;

pub type ErrorSender = tokio::sync::mpsc::Sender<ErrorMap>;
//pub type ErrorReceiver = tokio::sync::mpsc::Receiver<ErrorMap>;
Expand All @@ -140,7 +130,7 @@ pub struct ErrorAccumulator {
impl ErrorAccumulator {
pub fn new(tx: ErrorSender) -> Self {
Self {
map: ErrorMap::with_hasher(SeahashBuilder),
map: <_>::default(),
tx,
oldest: Instant::now(),
}
Expand All @@ -160,7 +150,7 @@ impl ErrorAccumulator {
};

#[allow(clippy::mutable_key_type)]
let map = std::mem::replace(&mut self.map, ErrorMap::with_hasher(SeahashBuilder));
let map = std::mem::take(&mut self.map);
permit.send(map);
true
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/proxy/packet_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ pub async fn spawn_receivers(
let mut log_task = tokio::time::interval(std::time::Duration::from_secs(5));

#[allow(clippy::mutable_key_type)]
let mut pipeline_errors = super::error::ErrorMap::with_hasher(super::error::SeahashBuilder);
let mut pipeline_errors = super::error::ErrorMap::default();

#[allow(clippy::mutable_key_type)]
fn report(errors: &mut super::error::ErrorMap) {
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ impl Config {
crate::net::cluster::proto::FilterChain::try_from(&*self.filters.load())?,
);
let any = resource.try_encode()?;
let version = seahash::hash(&any.value);
let version = gxhash::gxhash64(&any.value, 0xdeadbeef);

let vstr = version.to_string();

Expand Down
2 changes: 1 addition & 1 deletion src/filters/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl ReadContext {
destinations: Vec::new(),
source,
contents,
metadata: DynamicMetadata::new(),
metadata: <_>::default(),
}
}
}
4 changes: 1 addition & 3 deletions src/filters/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
* limitations under the License.
*/

use std::collections::HashMap;

use crate::{
net::endpoint::{DynamicMetadata, EndpointAddress},
pool::PoolBuffer,
Expand Down Expand Up @@ -45,7 +43,7 @@ impl WriteContext {
source,
dest,
contents,
metadata: HashMap::new(),
metadata: <_>::default(),
}
}
}
25 changes: 13 additions & 12 deletions src/net/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

use std::{
collections::{hash_map::RandomState, BTreeSet},
collections::BTreeSet,
fmt,
sync::atomic::{AtomicU64, AtomicUsize, Ordering::Relaxed},
};
Expand All @@ -27,6 +27,7 @@ use serde::{Deserialize, Serialize};
use crate::net::endpoint::{Endpoint, EndpointAddress, Locality};

const SUBSYSTEM: &str = "cluster";
const HASH_SEED: i64 = 0xdeadbeef;

pub use crate::generated::quilkin::config::v1alpha1 as proto;

Expand Down Expand Up @@ -60,15 +61,15 @@ pub(crate) fn active_endpoints() -> &'static prometheus::IntGauge {
&ACTIVE_ENDPOINTS
}

pub type TokenAddressMap = std::collections::BTreeMap<u64, BTreeSet<EndpointAddress>>;
pub type TokenAddressMap = gxhash::HashMap<u64, gxhash::HashSet<EndpointAddress>>;

#[derive(Copy, Clone)]
pub struct Token(u64);

impl Token {
#[inline]
pub fn new(token: &[u8]) -> Self {
Self(seahash::hash(token))
Self(gxhash::gxhash64(token, HASH_SEED))
}
}

Expand Down Expand Up @@ -123,7 +124,7 @@ impl EndpointSet {
pub fn new(endpoints: BTreeSet<Endpoint>) -> Self {
let mut this = Self {
endpoints,
token_map: TokenAddressMap::new(),
token_map: <_>::default(),
hash: 0,
version: 0,
};
Expand All @@ -141,7 +142,7 @@ impl EndpointSet {
pub fn with_version(endpoints: BTreeSet<Endpoint>, hash: EndpointSetVersion) -> Self {
let mut this = Self {
endpoints,
token_map: TokenAddressMap::new(),
token_map: <_>::default(),
hash: hash.number(),
version: 1,
};
Expand Down Expand Up @@ -177,14 +178,14 @@ impl EndpointSet {
#[inline]
pub fn update(&mut self) -> TokenAddressMap {
use std::hash::{Hash, Hasher};
let mut hasher = seahash::SeaHasher::with_seeds(0, 1, 2, 3);
let mut token_map = TokenAddressMap::new();
let mut hasher = gxhash::GxHasher::with_seed(HASH_SEED);
let mut token_map = TokenAddressMap::default();

for ep in &self.endpoints {
ep.hash(&mut hasher);

for tok in &ep.metadata.known.tokens {
let hash = seahash::hash(tok);
let hash = gxhash::gxhash64(tok, HASH_SEED);
token_map
.entry(hash)
.or_default()
Expand All @@ -200,12 +201,12 @@ impl EndpointSet {
/// Creates a map of tokens -> address for the current set
#[inline]
pub fn build_token_map(&mut self) -> TokenAddressMap {
let mut token_map = TokenAddressMap::new();
let mut token_map = TokenAddressMap::default();

// This is only called on proxies, so calculate a token map
for ep in &self.endpoints {
for tok in &ep.metadata.known.tokens {
let hash = seahash::hash(tok);
let hash = gxhash::gxhash64(tok, HASH_SEED);
token_map
.entry(hash)
.or_default()
Expand Down Expand Up @@ -257,7 +258,7 @@ impl EndpointSet {
}

/// Represents a full snapshot of all clusters.
pub struct ClusterMap<S = RandomState> {
pub struct ClusterMap<S = gxhash::GxBuildHasher> {
map: DashMap<Option<Locality>, EndpointSet, S>,
token_map: DashMap<u64, Vec<EndpointAddress>>,
num_endpoints: AtomicUsize,
Expand All @@ -268,7 +269,7 @@ type DashMapRef<'inner, S> = dashmap::mapref::one::Ref<'inner, Option<Locality>,
type DashMapRefMut<'inner, S> =
dashmap::mapref::one::RefMut<'inner, Option<Locality>, EndpointSet, S>;

impl ClusterMap<RandomState> {
impl ClusterMap {
pub fn new() -> Self {
Self::default()
}
Expand Down
4 changes: 1 addition & 3 deletions src/net/endpoint/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,10 @@ pub mod build {
pub const GIT_COMMIT_HASH: Option<&str> = option_env!("GIT_COMMIT_HASH");
}

use std::{collections::HashMap, convert::TryFrom};

pub use symbol::{Key, Reference, Symbol};

/// Shared state between [`Filter`][crate::filters::Filter]s during processing for a single packet.
pub type DynamicMetadata = HashMap<Key, Value>;
pub type DynamicMetadata = gxhash::HashMap<Key, Value>;

pub const KEY: &str = "quilkin.dev";

Expand Down

0 comments on commit ad5cf87

Please sign in to comment.