fastintmap

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2022 License: Apache-2.0 Imports: 8 Imported by: 3

README

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.

Documentation

Index

Constants

View Source
const DefaultSize = 8

DefaultSize is the default size for a zero allocated map

View Source
const MaxFillRate = float64(0.5)

MaxFillRate is the maximum fill rate for the slice before a resize will happen.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map added in v1.0.0

type Map[T any] struct {
	// contains filtered or unexported fields
}

Map implements a read optimized hash map.

func New

func New[T any](size uintptr) *Map[T]

New returns a new Map[T] instance with a specific initialization size.

func (*Map[T]) Add added in v1.0.0

func (m *Map[T]) Add(key uintptr, value T) bool

Add sets the value under the specified key to the map if it does not exist yet. If a resizing operation is happening concurrently while calling Set, the item might show up in the map only after the resize operation is finished. Returns true if the item was inserted or false if it existed.

func (*Map[T]) CAS added in v1.0.0

func (m *Map[T]) CAS(key uintptr, from, to T) bool

CAS performs a compare and swap operation sets the value under the specified key to the map. An existing item for this key will be overwritten.

func (*Map[T]) Delete added in v1.0.0

func (m *Map[T]) Delete(hashedKey uintptr)

Delete deletes the hashed key from the map.

func (*Map[T]) FillRate added in v1.0.0

func (m *Map[T]) FillRate() float64

FillRate returns the fill rate of the map.

func (*Map[T]) Get added in v1.0.0

func (m *Map[T]) Get(key uintptr) (value T, ok bool)

Get retrieves an element from the map under given hashed key.

func (*Map[T]) GetOrAdd added in v1.0.0

func (m *Map[T]) GetOrAdd(key uintptr, value T) (actual T, loaded bool)

GetOrAdd returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*Map[T]) Grow added in v1.0.0

func (m *Map[T]) Grow(newSize uintptr)

Grow resizes the hashmap to a new size, gets rounded up to next power of 2. To double the size of the hashmap use newSize 0. This function returns immediately, the resize operation is done in a goroutine. No resizing is done in case of another resize operation already being in progress.

func (*Map[T]) Len added in v1.0.0

func (m *Map[T]) Len() int

Len returns the number of elements within the map.

func (*Map[T]) Set added in v1.0.0

func (m *Map[T]) Set(key uintptr, value T)

Set sets the value under the specified key to the map. An existing item for this key will be overwritten. If a resizing operation is happening concurrently while calling Set, the item might show up in the map only after the resize operation is finished.

func (*Map[T]) String added in v1.0.0

func (m *Map[T]) String() string

String returns the map as a string, only hashed keys are printed.

func (*Map[T]) Visit added in v1.0.0

func (m *Map[T]) Visit(fn func(key uintptr, value T) error) error

Visit visits the entries in key order, calling fn for each. if the fn returns non-nil error stops process and returns that error

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

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