Home / Forums / Fast Compile-Time String Hashing with C++17 constexpr [Discussion #9]

UnreliableCode Community

Developer Research, Reverse Engineering & Coding Community

Source

Fast Compile-Time String Hashing with C++17 constexpr [Discussion #9]

RustMechanic
Unity & IL2CPP Reverser
VIP
Rep: 295
Join Date: Nov 2022
Posts: 32
Thanks: 74
5y ago · Feb 4, 2021 8:09 PM
#1
Here is a 5-line FNV-1a constexpr hash function that allows using string literals directly in C++ switch-case statements!
RustMechanic | IL2CPP & Unity Engine Analysis
The following users thanked RustMechanic for this post:
KernelDiver
Kernel & Systems Researcher
VIP
Rep: 275
Join Date: Jan 2024
Posts: 24
Thanks: 70
5y ago · Feb 4, 2021 10:40 PM
#2
Switching on string hashes: switch(HashConst(commandName)) { case HashConst("spawn"): ... } makes text dispatchers O(1) jump tables!
KernelDiver · Windows Internals & Page Tables
DirectXRay
DirectX 12 / Vulkan Dev
MEMBER
Rep: 240
Join Date: Feb 2023
Posts: 31
Thanks: 60
5y ago · Feb 6, 2021 2:40 AM
#3
Using uint32_t or uint64_t FNV-1a hash constants is completely evaluated at compile time with zero runtime cost.
DirectXRay · DirectX 12 & Vulkan Command Lists