cmap

package
v1.1.8-0...-5ee8a8c Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2021 License: Apache-2.0 Imports: 1 Imported by: 0

README

inwrap for concurrent_map https://github.com/easierway/concurrent_map


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

func I64Key

func I64Key(key int64) *concurrent_map.Int64Key

I64Key is to convert a int64 to Int64Key

func IntKey

func IntKey(key int) *concurrent_map.Int64Key

IntKey is to convert a int to Int64Key

func StrKey

func StrKey(key string) *concurrent_map.StringKey

StrKey is to convert a string to StringKey

Types

type CMap

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

CMap 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.

inwrap for concurrent_map.ConcurrentMap

func New

func New(numOfPartitions int) *CMap

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

inwrap for concurrent_map.CreateConcurrentMap

func (*CMap) Del

func (m *CMap) Del(key Partitionable)

Del is to delete the entries by the key

inwrap for concurrent_map.CreateConcurrentMap

func (*CMap) Get

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

Get is to get the value by the key

inwrap for concurrent_map.CreateConcurrentMap

func (*CMap) Set

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

Set is to store the KV entry to the map

inwrap for concurrent_map.CreateConcurrentMap

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.

inwrap for concurrent_map.Partitionable

Jump to

Keyboard shortcuts

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