concurrentmap

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2021 License: Apache-2.0 Imports: 4 Imported by: 0

README

concurrentmap

A concurrent safe map

Usage

  1. install:
go get github.com/chenquan/concurrentmap
  1. import the package:
import "github.com/chenquan/concurrentmap"

Example

package main

import (
	"fmt"
	"github.com/chenquan/concurrentmap"
)

func main() {
	concurrentMap := concurrentmap.New(concurrentmap.WithBucket(32))
	concurrentMap.Store("1", 1)
	if v, ok := concurrentMap.Load("1"); ok {
		fmt.Printf(" v = %v \n", v)
		concurrentMap.Delete("1")
	}
}

BenchMark

goos: windows
goarch: amd64
pkg: github.com/chenquan/concurrentmap
cpu: Intel(R) Core(TM) i5-8265UC CPU @ 1.60GHz
BenchmarkLoadMostlyHits
BenchmarkLoadMostlyHits/*concurrentmap_test.DeepCopyMap
BenchmarkLoadMostlyHits/*concurrentmap_test.DeepCopyMap-8         	44331153	        22.76 ns/op
BenchmarkLoadMostlyHits/*concurrentmap_test.RWMutexMap
BenchmarkLoadMostlyHits/*concurrentmap_test.RWMutexMap-8          	22793493	        55.05 ns/op
BenchmarkLoadMostlyHits/*concurrentmap.Map
BenchmarkLoadMostlyHits/*concurrentmap.Map-8            	38800419	        32.13 ns/op
BenchmarkLoadMostlyMisses
BenchmarkLoadMostlyMisses/*concurrentmap_test.DeepCopyMap
BenchmarkLoadMostlyMisses/*concurrentmap_test.DeepCopyMap-8       	75652347	        15.80 ns/op
BenchmarkLoadMostlyMisses/*concurrentmap_test.RWMutexMap
BenchmarkLoadMostlyMisses/*concurrentmap_test.RWMutexMap-8        	23163026	        52.01 ns/op
BenchmarkLoadMostlyMisses/*concurrentmap.Map
BenchmarkLoadMostlyMisses/*concurrentmap.Map-8          	49910176	        24.74 ns/op
BenchmarkLoadOrStoreBalanced
BenchmarkLoadOrStoreBalanced/*concurrentmap_test.DeepCopyMap
    concurrentmap_bench_test.go:91: DeepCopyMap has quadratic running time.
--- SKIP: BenchmarkLoadOrStoreBalanced/*concurrentmap_test.DeepCopyMap
BenchmarkLoadOrStoreBalanced/*concurrentmap_test.RWMutexMap
BenchmarkLoadOrStoreBalanced/*concurrentmap_test.RWMutexMap-8     	 3307788	       325.0 ns/op
BenchmarkLoadOrStoreBalanced/*concurrentmap.Map
BenchmarkLoadOrStoreBalanced/*concurrentmap.Map-8       	 5357139	       238.0 ns/op
BenchmarkLoadOrStoreUnique
BenchmarkLoadOrStoreUnique/*concurrentmap_test.DeepCopyMap
    concurrentmap_bench_test.go:123: DeepCopyMap has quadratic running time.
--- SKIP: BenchmarkLoadOrStoreUnique/*concurrentmap_test.DeepCopyMap
BenchmarkLoadOrStoreUnique/*concurrentmap_test.RWMutexMap
BenchmarkLoadOrStoreUnique/*concurrentmap_test.RWMutexMap-8       	 1898727	       658.3 ns/op
BenchmarkLoadOrStoreUnique/*concurrentmap.Map
BenchmarkLoadOrStoreUnique/*concurrentmap.Map-8         	 3510092	       539.9 ns/op
BenchmarkLoadOrStoreCollision
BenchmarkLoadOrStoreCollision/*concurrentmap_test.DeepCopyMap
BenchmarkLoadOrStoreCollision/*concurrentmap_test.DeepCopyMap-8   	 2618749	       463.2 ns/op
BenchmarkLoadOrStoreCollision/*concurrentmap_test.RWMutexMap
BenchmarkLoadOrStoreCollision/*concurrentmap_test.RWMutexMap-8    	16902312	        69.10 ns/op
BenchmarkLoadOrStoreCollision/*concurrentmap.Map
BenchmarkLoadOrStoreCollision/*concurrentmap.Map-8      	30768519	        36.28 ns/op
BenchmarkLoadAndDeleteBalanced
BenchmarkLoadAndDeleteBalanced/*concurrentmap_test.DeepCopyMap
    concurrentmap_bench_test.go:155: DeepCopyMap has quadratic running time.
--- SKIP: BenchmarkLoadAndDeleteBalanced/*concurrentmap_test.DeepCopyMap
BenchmarkLoadAndDeleteBalanced/*concurrentmap_test.RWMutexMap
BenchmarkLoadAndDeleteBalanced/*concurrentmap_test.RWMutexMap-8   	11556963	       102.4 ns/op
BenchmarkLoadAndDeleteBalanced/*concurrentmap.Map
BenchmarkLoadAndDeleteBalanced/*concurrentmap.Map-8     	33335554	        32.02 ns/op
BenchmarkLoadAndDeleteUnique
BenchmarkLoadAndDeleteUnique/*concurrentmap_test.DeepCopyMap
    concurrentmap_bench_test.go:183: DeepCopyMap has quadratic running time.
--- SKIP: BenchmarkLoadAndDeleteUnique/*concurrentmap_test.DeepCopyMap
BenchmarkLoadAndDeleteUnique/*concurrentmap_test.RWMutexMap
BenchmarkLoadAndDeleteUnique/*concurrentmap_test.RWMutexMap-8     	 9121527	       126.5 ns/op
BenchmarkLoadAndDeleteUnique/*concurrentmap.Map
BenchmarkLoadAndDeleteUnique/*concurrentmap.Map-8       	33346764	        34.52 ns/op
BenchmarkLoadAndDeleteCollision
BenchmarkLoadAndDeleteCollision/*concurrentmap_test.DeepCopyMap
BenchmarkLoadAndDeleteCollision/*concurrentmap_test.DeepCopyMap-8 	 6161821	       196.5 ns/op
BenchmarkLoadAndDeleteCollision/*concurrentmap_test.RWMutexMap
BenchmarkLoadAndDeleteCollision/*concurrentmap_test.RWMutexMap-8  	17646928	        66.53 ns/op
BenchmarkLoadAndDeleteCollision/*concurrentmap.Map
BenchmarkLoadAndDeleteCollision/*concurrentmap.Map-8    	95049313	        13.13 ns/op
BenchmarkRange
BenchmarkRange/*concurrentmap_test.DeepCopyMap
BenchmarkRange/*concurrentmap_test.DeepCopyMap-8                  	514242840	         2.338 ns/op
BenchmarkRange/*concurrentmap_test.RWMutexMap
BenchmarkRange/*concurrentmap_test.RWMutexMap-8                   	   18858	     68343 ns/op
BenchmarkRange/*concurrentmap.Map
BenchmarkRange/*concurrentmap.Map-8                     	  178810	      7135 ns/op
BenchmarkAdversarialAlloc
BenchmarkAdversarialAlloc/*concurrentmap_test.DeepCopyMap
BenchmarkAdversarialAlloc/*concurrentmap_test.DeepCopyMap-8       	 1000000	      1134 ns/op
BenchmarkAdversarialAlloc/*concurrentmap_test.RWMutexMap
BenchmarkAdversarialAlloc/*concurrentmap_test.RWMutexMap-8        	12437710	        99.90 ns/op
BenchmarkAdversarialAlloc/*concurrentmap.Map
BenchmarkAdversarialAlloc/*concurrentmap.Map-8          	18650221	        85.02 ns/op
BenchmarkAdversarialDelete
BenchmarkAdversarialDelete/*concurrentmap_test.DeepCopyMap
BenchmarkAdversarialDelete/*concurrentmap_test.DeepCopyMap-8      	 5495078	       499.1 ns/op
BenchmarkAdversarialDelete/*concurrentmap_test.RWMutexMap
BenchmarkAdversarialDelete/*concurrentmap_test.RWMutexMap-8       	12245209	       106.4 ns/op
BenchmarkAdversarialDelete/*concurrentmap.Map
BenchmarkAdversarialDelete/*concurrentmap.Map-8         	28265620	        40.69 ns/op
BenchmarkDeleteCollision
BenchmarkDeleteCollision/*concurrentmap_test.DeepCopyMap
BenchmarkDeleteCollision/*concurrentmap_test.DeepCopyMap-8        	 5664558	       183.2 ns/op
BenchmarkDeleteCollision/*concurrentmap_test.RWMutexMap
BenchmarkDeleteCollision/*concurrentmap_test.RWMutexMap-8         	16901740	        69.52 ns/op
BenchmarkDeleteCollision/*concurrentmap.Map
BenchmarkDeleteCollision/*concurrentmap.Map-8           	101102217	        13.05 ns/op
PASS

Process finished with the exit code 0


License

Apache License 2.0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToBytes

func ToBytes(v string) (b []byte)

Types

type AllocationBucketFunc

type AllocationBucketFunc interface {
	F(op *Options, k string) int
	Name() string
	Init(options *Options) bucket
}

type AvgBucket

type AvgBucket struct {
}

func (*AvgBucket) F

func (a *AvgBucket) F(op *Options, k string) int

func (*AvgBucket) Init

func (a *AvgBucket) Init(op *Options) (b bucket)

func (*AvgBucket) Name

func (a *AvgBucket) Name() string

type Hash

type Hash func(data []byte) uint32

type Map

type Map struct {
	// contains filtered or unexported fields
}

Map implement a concurrent map

func New

func New(options ...Option) *Map

func (*Map) Delete

func (m *Map) Delete(k string)

func (*Map) Load

func (m *Map) Load(k string) (interface{}, bool)

func (*Map) LoadAndDelete

func (m *Map) LoadAndDelete(k string) (value interface{}, loaded bool)

func (*Map) LoadOrStore

func (m *Map) LoadOrStore(k string, v interface{}) (actual interface{}, loaded bool)

func (*Map) Range

func (m *Map) Range(f func(k string, v interface{}) bool)

func (*Map) Store

func (m *Map) Store(k string, v interface{})

type Option

type Option interface {
	Apply(options *Options)
}

func WithAllocationBucket

func WithAllocationBucket(f AllocationBucketFunc) Option

func WithBucket

func WithBucket(bucketSize uint32) Option

func WithHash

func WithHash(hash Hash) Option

type Options

type Options struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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