Skip to content
forked from cornelk/hashmap

A Golang lock-free thread-safe HashMap optimized for fastest read access.

License

Notifications You must be signed in to change notification settings

itsabgr/fastintmap

 
 

Repository files navigation

fastintmap

GoDoc Go Report Card GitHub GitHub go.mod Go version (branch) Go

Overview

A Golang lock-free thread-safe map (with numeric keys only) optimized for fastest read access

Usage

Set a value for a key in the map:

m := &HashMap{}
m.Set(123, "any")

Read a value for a key from the map:

amount, ok := m.Get(123)

Use the map to count URL requests:

var i int64
actual, _ := m.GetOrInsert(124312, &i)
counter := (actual).(*atomic2.Int64)
counter.Add(1) // increase counter
...
count := counter.Get() // read counter

Benefits over Golangs builtin map

  • Faster

  • thread-safe access without need of a(n extra) mutex

  • Compare-and-swap access for values

Technical details

  • Technical design decisions have been made based on benchmarks that are stored in an external repository: go-benchmark

  • The library uses a sorted doubly linked list and a slice as an index into that list.

  • It optimizes the slice access by circumventing the Golang size check when reading from the slice. Once a slice is allocated, the size of it does not change. The library limits the index into the slice, therefore the Golang size check is obsolete. When the slice reaches a defined fill rate, a bigger slice is allocated and all keys are recalculated and transferred into the new slice.

About

A Golang lock-free thread-safe HashMap optimized for fastest read access.

Resources

License

Stars

Watchers

Forks

Languages

  • Go 100.0%