Minor performance tweaks
Some checks failed
continuous-integration/drone/push Build is failing
continuous-integration/drone Build is failing

This commit is contained in:
2022-12-25 12:55:09 +01:00
parent 055fbfba78
commit 43bc811704
4 changed files with 23 additions and 28 deletions

View File

@@ -14,7 +14,6 @@ use std::sync::LazyLock;
/// free speed out of it. Note that StringKeys also compare case insensitive, so that for example
/// `charmander` == `Charmander`.
#[derive(Clone, Debug)]
pub struct StringKey {
/// The underlying reference counted string.
str: ArcStr,
@@ -29,12 +28,14 @@ static STRING_CACHE: LazyLock<RwLock<HashMap<u32, ArcStr>>> = LazyLock::new(|| R
static EMPTY: LazyLock<StringKey> = LazyLock::new(|| StringKey::new(""));
impl StringKey {
#[inline(always)]
/// Gets the hash of a string.
pub const fn get_hash(s: &str) -> u32 {
let mut crc: u32 = 0xffffffff;
let mut i: usize = 0;
let bytes = s.as_bytes();
while i < s.len() {
crc = (crc >> 8) ^ CRC_TABLE[((crc ^ (to_lower(s.as_bytes()[i]) as u32)) & 0xff) as usize];
crc = (crc >> 8) ^ CRC_TABLE[((crc ^ (to_lower(bytes[i]) as u32)) & 0xff) as usize];
i += 1;
}
crc ^ 0xffffffff
@@ -132,6 +133,7 @@ impl ValueIdentifiable for StringKey {
/// The difference in ascii characters to translate from uppercase to lowercase.
const CAPITAL_DIFF: u8 = b'a' - b'A';
#[inline(always)]
/// Converts a character to lowercased in a const safe way.
const fn to_lower(c: u8) -> u8 {
if c >= b'A' && c <= b'Z' {