Go package porting the standard hashing algorithms to a more efficient implementation.
Go to file
2023-07-03 15:06:35 -04:00
.circleci use circleci 2.0 2018-02-16 15:15:24 -08:00
fasthashtest Merge remote-tracking branch 'origin/master' into svls/bytes-hash-funcs-for-fnv 2020-04-21 10:52:58 -07:00
fnv1 added loop unrolling optimization for bytes, added go.mod file 2020-04-21 11:04:13 -07:00
fnv1a added loop unrolling optimization for bytes, added go.mod file 2020-04-21 11:04:13 -07:00
jody Fix segfault in jody hash (#11) 2020-05-27 11:23:56 -07:00
.gitignore add circle.yml 2017-06-12 17:36:43 -07:00
fasthash32.go Added AddBytes and HashBytes functions to all the hash functions 2019-04-02 13:53:10 -07:00
fasthash.go Added AddBytes and HashBytes functions to all the hash functions 2019-04-02 13:53:10 -07:00
go.mod added loop unrolling optimization for bytes, added go.mod file 2020-04-21 11:04:13 -07:00
LICENSE Initial commit 2017-06-12 16:13:16 -07:00
README.md Add pause to README.md 2023-07-03 15:06:35 -04:00

fasthash CircleCI Go Report Card GoDoc

Go package porting the standard hashing algorithms to a more efficient implementation.

Note


Segment has paused maintenance on this project, but may return it to an active status in the future. Issues and pull requests from external contributors are not being considered, although internal contributions may appear from time to time. The project remains available under its open source license for anyone to use.

Motivations

Go has great support for hashing algorithms in the standard library, but the APIs are all exposed as interfaces, which means passing strings or byte slices to those require dynamic memory allocations. Hashing a string typically requires 2 allocations, one for the Hash value, and one to covert the string to a byte slice.

This package attempts to solve this issue by exposing functions that implement string hashing algorithms and don't require dynamic memory alloations.

Testing

To ensure consistency between the fasthash package and the standard library, all tests must be implemented to run against the standard hash functions and validate that both packages produced the same results.

Benchmarks

The implementations also have to prove that they are more efficient in terms of CPU and memory usage than the functions found in the standard library.
Here's an example with fnv-1a:

BenchmarkHash64/standard_hash_function 20000000   105.0 ns/op   342.31 MB/s   56 B/op   2 allocs/op
BenchmarkHash64/hash_function          50000000    38.6 ns/op   932.35 MB/s    0 B/op   0 allocs/op

Usage Example: FNV-1a

package main

import (
    "fmt"

    "github.com/segmentio/fasthash/fnv1a"
)

func main() {
    // Hash a single string.
    h1 := fnv1a.HashString64("Hello World!")
    fmt.Println("FNV-1a hash of 'Hello World!':", h1)

    // Incrementally compute a hash value from a sequence of strings.
    h2 := fnv1a.Init64
    h2 = fnv1a.AddString64(h2, "A")
    h2 = fnv1a.AddString64(h2, "B")
    h2 = fnv1a.AddString64(h2, "C")
    fmt.Println("FNV-1a hash of 'ABC':", h2)
}