armap

package module
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2024 License: MIT Imports: 5 Imported by: 0

README

armap

MIT License GoDoc Go Report Card Releases

HashMap on Arena

features:

  • Generics support
  • Map and Set, LinkedList types
  • Minimal GC overhead map implements
  • comparable key hash function uses maphash

Installation

go get github.com/octu0/armap

Example

package main

import (
	"fmt"

	"github.com/octu0/armap"
)

func main() {
	a := armap.NewArena(1024*1024, 4) // 1MB buffer size * 4
	defer a.Release() // release memory

	m := armap.NewMap[string, string](a, armap.WithCapacity(1000))

	m.Set("hello", "world1")
	v, ok := m.Get("hello")
	fmt.Println(v, ok) // => world1 true

	m.Set("hello", "world2")
	v, ok = m.Get("hello")
	fmt.Println(v, ok) // => world2 true

	m.Clear() // reset map, reuse memory

	v, ok = m.Get("hello")
	fmt.Println(v, ok) // => "" false
}

GC Benchmark

average GC time improved by ~200x.

$ go test -run=BenchmarkGC -bench=BenchmarkGC -benchtime=3x -v .
goos: darwin
goarch: amd64
pkg: github.com/octu0/armap
cpu: Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
BenchmarkGCSet
BenchmarkGCSet/golangmap
    armap_benchmark_test.go:92: min/avg/max/median = 59.526209ms/83.101306ms/270.682439ms/60.953085ms
    armap_benchmark_test.go:92: min/avg/max/median = 59.485567ms/67.009433ms/125.832941ms/59.875661ms
BenchmarkGCSet/golangmap-8         	       3	 223366544 ns/op
BenchmarkGCSet/armap
    armap_benchmark_test.go:117: min/avg/max/median = 216.152µs/377.403µs/507.442µs/377.907µs
    armap_benchmark_test.go:117: min/avg/max/median = 291.639µs/354.231µs/576.287µs/314.844µs
BenchmarkGCSet/armap-8             	       3	1353591775 ns/op
PASS

License

MIT, see LICENSE file for details.

Documentation

Index

Examples

Constants

View Source
const (
	AppName string = "armap"
	Version string = "1.6.0"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Arena added in v1.2.0

type Arena interface {
	Reset()
	Release()
	// contains filtered or unexported methods
}

func NewArena added in v1.2.0

func NewArena(bufferSize, bufferCount int) Arena

type LinkedList added in v1.2.0

type LinkedList[K comparable, V any] struct {
	// contains filtered or unexported fields
}
Example
a := NewArena(1024*1024, 2) // 2MB arena size
defer a.Release()
l := NewLinkedList[string, string](a)

l.Push("hello1", "world1")
v, ok := l.Get("hello1")
fmt.Println(v, ok)

l.Push("hello2", "world2")
v, ok = l.Get("hello2")
fmt.Println(v, ok)

l.Scan(func(key string, value string) bool {
	fmt.Println(key, value)
	return true
})

l.Clear()

_, ok = l.Get("hello1")
fmt.Println(ok)
Output:

world1 true
world2 true
hello2 world2
hello1 world1
false

func NewLinkedList added in v1.2.0

func NewLinkedList[K comparable, V any](arena Arena) *LinkedList[K, V]

func (*LinkedList[K, V]) Clear added in v1.2.0

func (l *LinkedList[K, V]) Clear()

func (*LinkedList[K, V]) Delete added in v1.2.0

func (l *LinkedList[K, V]) Delete(key K) (old V, found bool)

func (*LinkedList[K, V]) DeleteAll added in v1.3.0

func (l *LinkedList[K, V]) DeleteAll()

func (*LinkedList[K, V]) Get added in v1.2.0

func (l *LinkedList[K, V]) Get(key K) (old V, found bool)

func (*LinkedList[K, V]) Len added in v1.2.0

func (l *LinkedList[K, V]) Len() int

func (*LinkedList[K, V]) Push added in v1.2.0

func (l *LinkedList[K, V]) Push(key K, value V) (old V, found bool)

func (*LinkedList[K, V]) Scan added in v1.2.0

func (l *LinkedList[K, V]) Scan(iter func(K, V) bool)

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}
Example
a := NewArena(1024*1024, 2) // 2MB arena size
defer a.Release()
m := NewMap[string, string](a, WithCapacity(1000))

m.Set("hello", "world1")
v, ok := m.Get("hello")
fmt.Println(v, ok)

m.Set("hello", "world2")
v, ok = m.Get("hello")
fmt.Println(v, ok)

m.Clear()

_, ok = m.Get("hello")
fmt.Println(ok)
Output:

world1 true
world2 true
false

func NewMap added in v1.1.0

func NewMap[K comparable, V any](arena Arena, funcs ...OptionFunc) *Map[K, V]

func (*Map[K, V]) Clear

func (m *Map[K, V]) Clear()

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(key K) (old V, found bool)

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(key K) (old V, found bool)

func (*Map[K, V]) Len

func (m *Map[K, V]) Len() int

func (*Map[K, V]) Scan

func (m *Map[K, V]) Scan(iter func(K, V) bool)

func (*Map[K, V]) Set

func (m *Map[K, V]) Set(key K, value V) (old V, found bool)

type OptionFunc added in v1.1.0

type OptionFunc func(*option)

func WithCapacity added in v1.2.0

func WithCapacity(size int) OptionFunc

func WithLoadFactor added in v1.2.0

func WithLoadFactor(rate float64) OptionFunc

type Set added in v1.1.0

type Set[K comparable] struct {
	// contains filtered or unexported fields
}
Example
a := NewArena(1024*1024, 2) // 2MB arena size
defer a.Release()
s := NewSet[string](a, WithCapacity(1000))

ok := s.Add("foo")
fmt.Println("exists foo =", ok)
ok = s.Add("bar")
fmt.Println("exists bar =", ok)

ok = s.Contains("foo")
fmt.Println("contains foo =", ok)

ok = s.Add("foo")
fmt.Println("exists foo =", ok)

s.Clear()

ok = s.Add("foo")
fmt.Println("exists foo =", ok)
Output:

exists foo = false
exists bar = false
contains foo = true
exists foo = true
exists foo = false

func NewSet added in v1.1.0

func NewSet[K comparable](arena Arena, funcs ...OptionFunc) *Set[K]

func (*Set[K]) Add added in v1.1.0

func (s *Set[K]) Add(key K) bool

func (*Set[K]) Clear added in v1.1.0

func (s *Set[K]) Clear()

func (*Set[K]) Contains added in v1.1.0

func (s *Set[K]) Contains(key K) bool

func (*Set[K]) Delete added in v1.1.0

func (s *Set[K]) Delete(key K) bool

func (*Set[K]) Len added in v1.1.0

func (s *Set[K]) Len() int

func (*Set[K]) Scan added in v1.1.0

func (s *Set[K]) Scan(iter func(K) bool)

type TypeArena added in v1.2.0

type TypeArena[T any] interface {
	New() *T
	NativeNew() *T
	NewValue(func(*T)) *T
	NativeNewValue(func(*T)) *T
	MakeSlice(int, int) []T
	NativeMakeSlice(int, int) []T
	AppendSlice([]T, ...T) []T
	Reset()
	Release()
}

func NewTypeArena added in v1.2.0

func NewTypeArena[T any](a Arena) TypeArena[T]

Jump to

Keyboard shortcuts

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