mph

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2024 License: MIT Imports: 3 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-mph

minimal perfect hash functions

godoc: https://pkg.go.dev/github.com/liennie/go-mph

Changes

  • some memory optimizations, making New run faster due to less allocations
  • using a second hash instead of xorshiftmult, making New more robust against collisions, but also making it slower and making Query slightly slower
  • sanity checks and duplicate key check

Benchmarks

Four versions were benchmarked: without change, with memory optimizations, with a second hash instead of xorshiftmult and with duplicate key detection. Two key sets were used: small with 26 keys and big with 1 094 068 keys.

goos: linux
goarch: amd64
pkg: github.com/liennie/go-mph
cpu: 13th Gen Intel(R) Core(TM) i7-1365U
New (small)
                        │ sec/op       vs base         │
original.new.small.txt    3.590µ ± 2%
optimized.new.small.txt   2.745µ ± 2%  -23.55% (n=100)
hash.new.small.txt        2.626µ ± 2%  -26.85% (n=100)
dedup.new.small.txt       2.738µ ± 2%  -23.73% (n=100)

                        │ B/op          vs base         │
original.new.small.txt    2.508Ki ± 0%
optimized.new.small.txt   2.039Ki ± 0%  -18.69% (n=100)
hash.new.small.txt        2.039Ki ± 0%  -18.69% (n=100)
dedup.new.small.txt       2.039Ki ± 0%  -18.69% (n=100)

                        │ allocs/op   vs base         │
original.new.small.txt    47.00 ± 0%
optimized.new.small.txt   19.00 ± 0%  -59.57% (n=100)
hash.new.small.txt        19.00 ± 0%  -59.57% (n=100)
dedup.new.small.txt       19.00 ± 0%  -59.57% (n=100)
New (big)
                      │ sec/op       vs base         │
original.new.big.txt    300.0m ± 1%
optimized.new.big.txt   201.3m ± 1%  -32.92% (n=100)
hash.new.big.txt        284.7m ± 1%  -5.10% (n=100)
dedup.new.big.txt       299.6m ± 1%  ~ (p=0.761 n=100)

                      │ B/op          vs base         │
original.new.big.txt    172.8Mi ± 0%
optimized.new.big.txt   93.66Mi ± 0%  -45.79% (n=100)
hash.new.big.txt        93.66Mi ± 0%  -45.79% (n=100)
dedup.new.big.txt       93.66Mi ± 0%  -45.79% (n=100)

                      │ allocs/op    vs base         │
original.new.big.txt    1.496M ± 0%
optimized.new.big.txt   440.6k ± 0%  -70.55% (n=100)
hash.new.big.txt        440.6k ± 0%  -70.55% (n=100)
dedup.new.big.txt       440.6k ± 0%  -70.55% (n=100)
Query (small)
                          │ sec/op       vs base                 │
original.query.small.txt    125.5n ± 0%
optimized.query.small.txt   125.3n ± 0%  ~ (p=0.673 n=100)
hash.query.small.txt        156.0n ± 0%  +24.25% (p=0.000 n=100)
dedup.query.small.txt       156.4n ± 0%  +24.61% (p=0.000 n=100)
Query (big)
                        │ sec/op       vs base                │
original.query.big.txt    69.68m ± 1%
optimized.query.big.txt   68.25m ± 1%  -2.05% (p=0.000 n=100)
hash.query.big.txt        74.41m ± 1%  +6.79% (p=0.000 n=100)
dedup.query.big.txt       72.79m ± 0%  +4.47% (p=0.000 n=100)

Documentation

Overview

Package mph implements a hash/displace minimal perfect hash function.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Table

type Table struct {
	Values []uint32
	Seeds  []int32
}

Table stores the values for the hash function

func New

func New(keys []string) *Table

New constructs a minimal perfect hash function for the set of keys which returns the index of item in the keys array. It panics if there are duplicate keys. The caller is responsible for deduplicating the keys before calling New.

func (*Table) Query

func (t *Table) Query(k string) uint32

Query looks up an entry in the table and return the index.

Jump to

Keyboard shortcuts

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