mph

package module
v0.0.0-...-fa6e751 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2025 License: MIT Imports: 14 Imported by: 0

README

mph

Go Reference

mph is a Go package for that implements a minimal perfect hash table over strings. It uses the "Hash, displace, and compress" algorithm and the Murmur3 hash function.

Some quick benchmark results (this is on an i7-8700K):

  • Build constructs a minimal perfect hash table from a 102k word dictionary in 18ms (construction time is linear in the size of the input).

  • Lookups on that dictionary take about 30ns and are 27% faster than a map[string]uint32:

    BenchmarkTable-12               199293806               29.99 ns/op
    BenchmarkTableMap-12            145449822               40.92 ns/op
    

Documentation

Overview

Package mph implements a minimal perfect hash table over strings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ShardedTable

type ShardedTable struct {
	// contains filtered or unexported fields
}

func LoadShardedTableFromFile

func LoadShardedTableFromFile(filePath string) (*ShardedTable, error)

func NewShardedTable

func NewShardedTable(
	keyLen, prefBits, buffSzBytes int,
	mphDirPath string,
) (*ShardedTable, error)

func (*ShardedTable) Commit

func (st *ShardedTable) Commit(grp *errgroup.Group) error

func (*ShardedTable) DumpToFile

func (st *ShardedTable) DumpToFile(filePath string) error

func (*ShardedTable) GetCounts

func (st *ShardedTable) GetCounts() []uint

func (*ShardedTable) Lookup

func (st *ShardedTable) Lookup(s []byte) (n uint32, ok bool)

func (*ShardedTable) Put

func (st *ShardedTable) Put(key []byte) error

type Table

type Table struct {
	// contains filtered or unexported fields
}

A Table is an immutable hash table that provides constant-time lookups of key indices using a minimal perfect hash.

func Build

func Build(keys [][]byte) (*Table, error)

Build builds a Table from keys using the "Hash, displace, and compress" algorithm described in http://cmph.sourceforge.net/papers/esa09.pdf. Returns an error if duplicate keys are detected.

func BuildFromFile

func BuildFromFile(keysFile *os.File, keyLen int) (*Table, error)

func LoadFromFile

func LoadFromFile(filePath string) (*Table, error)

func LoadFromKeysFile

func LoadFromKeysFile(keysFile *os.File) (*Table, error)

func (*Table) DumpToFile

func (t *Table) DumpToFile(filePath string) error

func (*Table) DumpToKeysFile

func (t *Table) DumpToKeysFile() error

func (*Table) Lookup

func (t *Table) Lookup(s []byte) (n uint32, ok bool)

Lookup searches for s in t and returns its index and whether it was found.

Jump to

Keyboard shortcuts

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