concurrent_map

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2019 License: Apache-2.0 Imports: 2 Imported by: 0

README

ConcurrentMap for GO

The better performance thread-safe map in GO

After v1.9, normally, programmers have two options for thread-safe map. One is to build the thread-safe solution with syn.RWMutex. But, in many cases, especially, in the case the number of CPU cores is larger than 2, this option's performance is quite poor.

Another is to use the sync.map, which has been added to the sync package from v.1.9. Unfortunately, sync.map can not work well for all the cases, especially, the case of multi-threads writing. For more info, please, check the great video https://www.youtube.com/watch?v=C1EtfDnsdDs.

This project is to provide a thread-safe map which is Java ConcurrentMap's GO version. From the following benchmark you can see it is better in the multi-thread writing cases.

The following test is about 100 Goroutines writing and 100 Groutines reading. The test is executed on Macbook (macOS 10.13.2, 2 core (2.3G Intel Core i5), 8G LPDDR3)

image

FAQ

1 Why not provide the default hash function for partition?

Ans: As you known, the partition solution would impact the performance significantly. The proper partition solution balances the access to the different partitions and avoid of the hot partition. The access mode highly relates to your business. So, the better partition solution would just be designed according to your business.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConcurrentMap

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

ConcurrentMap is a thread safe map collection with better performance. The backend map entries are separated into the different partitions. Threads can access the different partitions safely without lock.

func CreateConcurrentMap

func CreateConcurrentMap(numOfPartitions int) *ConcurrentMap

CreateConcurrentMap is to create a ConcurrentMap with the setting number of the partitions

func (*ConcurrentMap) Del

func (m *ConcurrentMap) Del(key Partitionable)

Del is to delete the entries by the key

func (*ConcurrentMap) Get

func (m *ConcurrentMap) Get(key Partitionable) (interface{}, bool)

Get is to get the value by the key

func (*ConcurrentMap) Set

func (m *ConcurrentMap) Set(key Partitionable, v interface{})

Set is to store the KV entry to the map

type ConcurrentMapBenchmarkAdapter

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

func CreateConcurrentMapBenchmarkAdapter

func CreateConcurrentMapBenchmarkAdapter(numOfPartitions int) *ConcurrentMapBenchmarkAdapter

func (*ConcurrentMapBenchmarkAdapter) Del

func (m *ConcurrentMapBenchmarkAdapter) Del(key interface{})

func (*ConcurrentMapBenchmarkAdapter) Get

func (m *ConcurrentMapBenchmarkAdapter) Get(key interface{}) (interface{}, bool)

func (*ConcurrentMapBenchmarkAdapter) Set

func (m *ConcurrentMapBenchmarkAdapter) Set(key interface{}, value interface{})

type Int64Key

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

func I64Key

func I64Key(key int64) *Int64Key

StrKey is to convert a string to StringKey

func (*Int64Key) PartitionKey

func (i *Int64Key) PartitionKey() int64

PartitionID is created by string's hash

func (*Int64Key) Value

func (i *Int64Key) Value() interface{}

Value is the raw string

type Partitionable

type Partitionable interface {
	// Value is raw value of the key
	Value() interface{}

	// PartitionKey is used for getting the partition to store the entry with the key.
	// E.g. the key's hash could be used as its PartitionKey
	// The partition for the key is partitions[(PartitionKey % m.numOfBlockets)]
	//
	// 1 Why not provide the default hash function for partition?
	// Ans: As you known, the partition solution would impact the performance significantly.
	// The proper partition solution balances the access to the different partitions and
	// avoid of the hot partition. The access mode highly relates to your business.
	// So, the better partition solution would just be designed according to your business.
	PartitionKey() int64
}

Partitionable is the interface which should be implemented by key type. It is to define how to partition the entries.

type RWLockMap

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

func CreateRWLockMap

func CreateRWLockMap() *RWLockMap

func (*RWLockMap) Del

func (m *RWLockMap) Del(key interface{})

func (*RWLockMap) Get

func (m *RWLockMap) Get(key interface{}) (interface{}, bool)

func (*RWLockMap) Set

func (m *RWLockMap) Set(key interface{}, value interface{})

type StringKey

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

StringKey is for the string type key

func StrKey

func StrKey(key string) *StringKey

StrKey is to convert a string to StringKey

func (*StringKey) PartitionKey

func (s *StringKey) PartitionKey() int64

PartitionID is created by string's hash

func (*StringKey) Value

func (s *StringKey) Value() interface{}

Value is the raw string

type SyncMapBenchmarkAdapter

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

func CreateSyncMapBenchmarkAdapter

func CreateSyncMapBenchmarkAdapter() *SyncMapBenchmarkAdapter

func (*SyncMapBenchmarkAdapter) Del

func (m *SyncMapBenchmarkAdapter) Del(key interface{})

func (*SyncMapBenchmarkAdapter) Get

func (m *SyncMapBenchmarkAdapter) Get(key interface{}) (interface{}, bool)

func (*SyncMapBenchmarkAdapter) Set

func (m *SyncMapBenchmarkAdapter) Set(key interface{}, val interface{})

Jump to

Keyboard shortcuts

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