Documentation
¶
Overview ¶
Package agilehash provides a blazingly fast, platform-independent hashing algorithm heavily optimized for keys under 1 KB, making it ideal for Redis-replacement storage backends, high-performance key-value caches, and HPC server engines.
Key Features ¶
Ultra-Low Latency Sub-1 KB Keys: Sub-1.8 ns latency for 8–16B keys and over 330 Million key hashes/sec per CPU core on modern x86-64 hardware.
Native 128-Bit Dual Finalization ([Hash128], [Hash128Micro]): Compute single-pass 128-bit hashes with zero-collision guarantees at ~3.33 ns latency (~45% faster than dual-seed hashing).
3-Tiered Dispatch Architecture: Lean inlined fast-paths for small keys (1–16B and 17–112B) to eliminate stack register spilling and preserve CPU L1 instruction cache.
AMD64 RIP-Relative Assembly: Direct 32-bit RIP-relative constant loads in assembly (secrets+offset(SB)), freeing registers and maximizing decode bandwidth.
Fixed O(1) Memory Streaming Hasher: Hasher provides incremental hashing with zero heap allocations after New and deferred block eviction.
Variants ¶
This package provides three hash variants optimized for different use cases:
Hash/HashWithSeed: Default general-purpose variant. Uses 7 parallel mixing lanes processing 112 bytes per iteration.
HashMicro/HashMicroWithSeed: Tailored for cache-sensitive HPC/server workloads and Redis-like key-value caching (keys <= 1KB). Uses 5 parallel lanes with 80-byte blocks, maximizing throughput on medium-sized key distributions.
HashNano/HashNanoWithSeed: Optimized for mobile and embedded systems with minimal code size. Uses 3 parallel lanes, fastest for inputs up to 48 bytes.
Native 128-Bit Hashing ¶
For distributed storage backends requiring 128-bit hash keys:
- [Hash128]/[Hash128WithSeed]: Computes a single-pass 128-bit hash output (h1, h2).
- [Hash128Micro]/[Hash128MicroWithSeed]: Ultra-fast single-pass 128-bit hashing for server/HPC keys (<= 1KB).
Performance & Thread Safety ¶
All stateless hash functions (Hash, HashMicro, HashNano, [Hash128], [Hash128Micro]) are safe for concurrent use by multiple goroutines.
The Hasher type is stateful and NOT safe for concurrent write operations; each goroutine must maintain its own Hasher instance.
Index ¶
- Constants
- func Hash(data []byte) uint64
- func HashComparable[T comparable](v T) uint64
- func HashComparableWithSeed[T comparable](v T, seed uint64) uint64
- func HashMicro(data []byte) uint64
- func HashMicroWithSeed(data []byte, seed uint64) uint64
- func HashNano(data []byte) uint64
- func HashNanoWithSeed(data []byte, seed uint64) uint64
- func HashString(s string) uint64
- func HashStringMicro(s string) uint64
- func HashStringMicroWithSeed(s string, seed uint64) uint64
- func HashStringNano(s string) uint64
- func HashStringNanoWithSeed(s string, seed uint64) uint64
- func HashStringWithSeed(s string, seed uint64) uint64
- func HashWithSeed(data []byte, seed uint64) uint64
- type Hasher
- func (h *Hasher) BlockSize() int
- func (h *Hasher) Reset()
- func (h *Hasher) Size() int
- func (h *Hasher) Sum(b []byte) []byte
- func (h *Hasher) Sum32() uint32
- func (h *Hasher) Sum64() uint64
- func (h *Hasher) Write(p []byte) (n int, err error)
- func (h *Hasher) WriteComparable(v any)
- func (h *Hasher) WriteString(s string) (n int, err error)
Constants ¶
const ( Secret0 uint64 = 0x2d358dccaa6c78a5 Secret1 uint64 = 0x8bb84b93962eacc9 Secret2 uint64 = 0x4b33a62ed433d4a3 Secret3 uint64 = 0x4d5a2da51de1aa47 Secret4 uint64 = 0xa0761d6478bd642f Secret5 uint64 = 0xe7037ed1a0b428db Secret6 uint64 = 0x90ed1765281c388c Secret7 uint64 = 0xaaaaaaaaaaaaaaaa )
Secret constants used by the core agilehash mixing algorithm.
const DefaultBlockSize = 112
DefaultBlockSize is the default block size (112 bytes) used by Hasher.
const DefaultSeed uint64 = 0
DefaultSeed is the default 64-bit seed value (0) used by agilehash.
Variables ¶
This section is empty.
Functions ¶
func Hash ¶
Hash computes a 64-bit agilehash of the input data using the default seed (0). This is optimized with a precomputed seed constant to skip one multiply.
func HashComparable ¶
func HashComparable[T comparable](v T) uint64
HashComparable returns the hash of comparable value v using the default seed (0).
This is not compatible with Hash or HashWithSeed because it encodes type information and traverses values via reflection; it also randomizes floating-point NaNs (so results are not deterministic when v contains NaNs) and hashes pointer-like values by address, making results process-specific.
func HashComparableWithSeed ¶
func HashComparableWithSeed[T comparable](v T, seed uint64) uint64
HashComparableWithSeed returns the hash of comparable value v using seed.
This is not compatible with Hash or HashWithSeed because it encodes type information and traverses values via reflection; it also randomizes floating-point NaNs (so results are not deterministic when v contains NaNs) and hashes pointer-like values by address, making results process-specific.
func HashMicro ¶
HashMicro computes a hash using the Micro variant, optimized for HPC/server applications.
~16% faster for inputs up to 512 bytes, may be slower for inputs above 1KB.
func HashMicroWithSeed ¶
HashMicroWithSeed computes a hash using the Micro variant with a custom seed.
func HashNano ¶
HashNano computes a hash using the Nano variant, optimized for mobile or embedded.
~13% faster for inputs up to 48 bytes, may be slower for larger inputs.
func HashNanoWithSeed ¶
HashNanoWithSeed computes a hash using the Nano variant with a custom seed.
func HashString ¶
HashString computes a 64-bit agilehash of the input string using the default seed (0).
func HashStringMicro ¶
HashStringMicro computes a hash of the input string using the Micro variant.
func HashStringMicroWithSeed ¶
HashStringMicroWithSeed computes a hash of the input string using the Micro variant with a custom seed.
func HashStringNano ¶
HashStringNano computes a hash of the input string using the Nano variant.
func HashStringNanoWithSeed ¶
HashStringNanoWithSeed computes a hash of the input string using the Nano variant with a custom seed.
func HashStringWithSeed ¶
HashStringWithSeed computes a 64-bit agilehash of the input string using the provided seed.
func HashWithSeed ¶
HashWithSeed computes a 64-bit agilehash of the input data using the provided seed.
Types ¶
type Hasher ¶
type Hasher struct {
// contains filtered or unexported fields
}
Hasher implements hash.Hash32 and hash.Hash64 for streaming hash computation.
Note: For memory-efficiency with large inputs, consider using Hash directly.
func NewWithSeed ¶
NewWithSeed creates a new Hasher with the given seed.
func (*Hasher) WriteComparable ¶
WriteComparable adds a comparable value to the running hash.
func (*Hasher) WriteString ¶
WriteString adds more data to the running hash from a string.
This method allows Hasher to implement io.StringWriter.