From 7af8efae2f8ab475653a219db9cbf78f1e22dbaa Mon Sep 17 00:00:00 2001 From: Shunsuke Shibayama Date: Thu, 3 Oct 2024 19:35:05 +0900 Subject: [PATCH] perf: use `ahash` --- Cargo.lock | 51 ++++++++++++++++++++++++++++++++++++ crates/erg_common/Cargo.toml | 1 + crates/erg_common/fxhash.rs | 6 +++-- crates/erg_common/lib.rs | 2 +- 4 files changed, 57 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d2f4d460..55e97063 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,19 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" +[[package]] +name = "ahash" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e89da841a80418a9b391ebaea17f5c112ffaaa96f621d2c285b5174da76b9011" +dependencies = [ + "cfg-if", + "getrandom", + "once_cell", + "version_check", + "zerocopy", +] + [[package]] name = "autocfg" version = "1.4.0" @@ -130,6 +143,7 @@ dependencies = [ name = "erg_common" version = "0.6.45" dependencies = [ + "ahash", "backtrace-on-stack-overflow", "crossterm", "erg_proc_macros", @@ -184,6 +198,17 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "getrandom" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4567c8db10ae91089c99af84c68c38da3ec2f087c3f82960bcdbf3656b6f4d7" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "gimli" version = "0.31.0" @@ -666,6 +691,12 @@ dependencies = [ "serde", ] +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + [[package]] name = "w-boson" version = "0.1.0" @@ -896,3 +927,23 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.79", +] diff --git a/crates/erg_common/Cargo.toml b/crates/erg_common/Cargo.toml index f9f61c7c..2433ac9e 100644 --- a/crates/erg_common/Cargo.toml +++ b/crates/erg_common/Cargo.toml @@ -37,6 +37,7 @@ w-boson = { version = "0.1.0", optional = true } crossterm = { optional = true, version = "0.25.0" } parking_lot = "0.12" thread_local = "1.1" +ahash = "0.8" pyo3 = { workspace = true, optional = true } erg_proc_macros = { workspace = true } diff --git a/crates/erg_common/fxhash.rs b/crates/erg_common/fxhash.rs index a622e32f..95effd52 100644 --- a/crates/erg_common/fxhash.rs +++ b/crates/erg_common/fxhash.rs @@ -37,9 +37,11 @@ use core::mem::size_of; use core::ops::BitXor; use std::collections::{HashMap, HashSet}; +use ahash::AHasher; + /// Type alias for a hashmap using the `fx` hash algorithm. -pub type FxHashMap = HashMap>; -pub type FxHashSet = HashSet>; +pub type FxHashMap = HashMap>; +pub type FxHashSet = HashSet>; /// A speedy hash algorithm for use within rustc. The hashmap in liballoc /// by default uses SipHash which isn't quite as speedy as we want. In the diff --git a/crates/erg_common/lib.rs b/crates/erg_common/lib.rs index 41ae32e3..af3acfb0 100644 --- a/crates/erg_common/lib.rs +++ b/crates/erg_common/lib.rs @@ -103,7 +103,7 @@ pub fn fmt_indent(s: String, depth: usize) -> String { /// If you want to get a hash consisting of multiple objects, pass it as a tuple or array pub fn get_hash(t: &T) -> usize { - let mut s = fxhash::FxHasher::default(); + let mut s = ahash::AHasher::default(); t.hash(&mut s); let res = std::hash::Hasher::finish(&s); if cfg!(target_pointer_width = "64") {