armap

package module
v1.7.1 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2026 License: MIT Imports: 4 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

armap

MIT License GoDoc Go Report Card Releases

Map on Arena

features:

  • Generics support
  • Map and Set
  • 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(4*1024*1024) // 4MB buffer size
	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: arm64
pkg: github.com/octu0/armap
cpu: Apple M4 Max
BenchmarkGCSet
BenchmarkGCSet/golangmap
    armap_benchmark_test.go:92: min/avg/max/median = 17.572541ms/17.853349ms/19.087166ms/17.685667ms
    armap_benchmark_test.go:92: min/avg/max/median = 17.66425ms/19.539275ms/35.7945ms/17.7085ms
BenchmarkGCSet/golangmap-16         	       3	  65133944 ns/op
BenchmarkGCSet/armap
    armap_benchmark_test.go:120: min/avg/max/median = 211.792µs/250.008µs/320.25µs/235.042µs
    armap_benchmark_test.go:120: min/avg/max/median = 241.042µs/294.146µs/343.084µs/293.083µs
BenchmarkGCSet/armap-16             	       3	  35972708 ns/op
PASS

License

MIT, see LICENSE file for details.

Documentation

Index

Examples

Constants

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

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 int) Arena

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}
Example
a := NewArena(2 * 1024 * 1024) // 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) (val 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(2 * 1024 * 1024) // 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
	NewValue(func(*T)) *T
	MakeSlice(int, int) []T
	AppendSlice([]T, ...T) []T
	Clone(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
⌂ 换网址