Skip to content

Latest commit

 

History

History
56 lines (39 loc) · 2.01 KB

README.md

File metadata and controls

56 lines (39 loc) · 2.01 KB

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.