fasthash

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 27, 2020 License: MIT Imports: 2 Imported by: 0

README

fasthash CircleCI Go Report Card GoDoc

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

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)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HashBytes32 added in v1.0.2

func HashBytes32(f func() hash.Hash32) func([]byte) uint32

HashBytes32 makes a hashing function from the hashing algorithm returned by f.

func HashBytes64 added in v1.0.2

func HashBytes64(f func() hash.Hash64) func([]byte) uint64

HashBytes64 makes a hashing function from the hashing algorithm returned by f.

func HashString32

func HashString32(f func() hash.Hash32) func(string) uint32

HashString32 makes a hashing function from the hashing algorithm returned by f.

func HashString64

func HashString64(f func() hash.Hash64) func(string) uint64

HashString64 makes a hashing function from the hashing algorithm returned by f.

func HashUint32

func HashUint32(f func() hash.Hash32) func(uint32) uint32

HashUint32 makes a hashing function from the hashing algorithm return by f.

func HashUint64

func HashUint64(f func() hash.Hash64) func(uint64) uint64

HashUint64 makes a hashing function from the hashing algorithm return by f.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL