go-hashmap, v1.2
Posted on

The library that implements a hash map with synchronized and concurrent wrappers.
Implementation of a concurrent hash map.
Change Log
- implementation of a concurrent hash map:
- use data sharding for concurrent access;
- use the synchronized implementation described above as one shard;
- support operations:
- getting of an item by a key;
- setting of an item by a key;
- deleting of an item by a key;
- remove the success flag from deleting methods.
Features
- implementation of a hash map:
- use the open addressing strategy for collision resolution;
- use the key interface for supporting custom types;
- support operations:
- getting of an item by a key;
- setting of an item by a key;
- deleting of an item by a key;
- implementation of a synchronized hash map:
- use the implementation described above as an inner map;
- use a mutex lock to access the inner map;
- support operations:
- getting of an item by a key;
- setting of an item by a key;
- deleting of an item by a key;
- implementation of a concurrent hash map:
- use data sharding for concurrent access;
- use the synchronized implementation described above as one shard;
- support operations:
- getting of an item by a key;
- setting of an item by a key;
- deleting of an item by a key.
Example
hashmap.ConcurrentHashMap:
package main
import (
"fmt"
"hash/fnv"
"io"
hashmap "github.com/thewizardplusplus/go-hashmap"
)
type StringKey string
func (key StringKey) Hash() int {
hash := fnv.New32()
io.WriteString(hash, string(key))
return int(hash.Sum32())
}
func (key StringKey) Equals(other interface{}) bool {
return key == other.(StringKey)
}
func main() {
timeZones := hashmap.NewConcurrentHashMap()
timeZones.Set(StringKey("EST"), -5*60*60)
timeZones.Set(StringKey("CST"), -6*60*60)
timeZones.Set(StringKey("MST"), -7*60*60)
estOffset, ok := timeZones.Get(StringKey("EST"))
fmt.Println(estOffset, ok)
// Output:
// -18000 true
}
Benchmarks
BenchmarkSynchronizedBuiltinMap/Get/1000/1-4 10000000 1403 ns/op 16 B/op 1 allocs/op
BenchmarkSynchronizedBuiltinMap/Get/1000/10-4 2000000 10086 ns/op 16 B/op 1 allocs/op
BenchmarkSynchronizedBuiltinMap/Get/1000/100-4 200000 67435 ns/op 16 B/op 1 allocs/op
BenchmarkSynchronizedBuiltinMap/Get/1000/1000-4 50000 385837 ns/op 18 B/op 1 allocs/op
BenchmarkSyncMap/Get/1000/1-4 10000000 1373 ns/op 16 B/op 1 allocs/op
BenchmarkSyncMap/Get/1000/10-4 2000000 9594 ns/op 16 B/op 1 allocs/op
BenchmarkSyncMap/Get/1000/100-4 200000 65672 ns/op 16 B/op 1 allocs/op
BenchmarkSyncMap/Get/1000/1000-4 50000 381484 ns/op 18 B/op 1 allocs/op
BenchmarkConcurrentHashMap/Get/1000/1-4 3000000 4145 ns/op 55 B/op 7 allocs/op
BenchmarkConcurrentHashMap/Get/1000/10-4 1000000 19554 ns/op 415 B/op 70 allocs/op
BenchmarkConcurrentHashMap/Get/1000/100-4 200000 100572 ns/op 4015 B/op 700 allocs/op
BenchmarkConcurrentHashMap/Get/1000/1000-4 20000 638640 ns/op 40076 B/op 6998 allocs/op
BenchmarkSynchronizedBuiltinMap/Set/1000/1-4 100000 118490 ns/op 16 B/op 1 allocs/op
BenchmarkSynchronizedBuiltinMap/Set/1000/10-4 5000 4004046 ns/op 34 B/op 1 allocs/op
BenchmarkSynchronizedBuiltinMap/Set/1000/100-4 300 41541398 ns/op 359 B/op 1 allocs/op
BenchmarkSynchronizedBuiltinMap/Set/1000/1000-4 30 460350938 ns/op 7329 B/op 58 allocs/op
BenchmarkSyncMap/Set/1000/1-4 50000 253130 ns/op 32002 B/op 2999 allocs/op
BenchmarkSyncMap/Set/1000/10-4 5000 3618081 ns/op 319901 B/op 29981 allocs/op
BenchmarkSyncMap/Set/1000/100-4 500 37144669 ns/op 3199366 B/op 299813 allocs/op
BenchmarkSyncMap/Set/1000/1000-4 30 537376345 ns/op 32073810 B/op 2999177 allocs/op
BenchmarkConcurrentHashMap/Set/1000/1-4 20000 598574 ns/op 48006 B/op 7997 allocs/op
BenchmarkConcurrentHashMap/Set/1000/10-4 5000 3239388 ns/op 479908 B/op 79962 allocs/op
BenchmarkConcurrentHashMap/Set/1000/100-4 500 32427188 ns/op 4799851 B/op 799630 allocs/op
BenchmarkConcurrentHashMap/Set/1000/1000-4 20 678751115 ns/op 48080325 B/op 7997485 allocs/op
BenchmarkSynchronizedBuiltinMap/Delete/1000/1-4 10000000 1392 ns/op 16 B/op 1 allocs/op
BenchmarkSynchronizedBuiltinMap/Delete/1000/10-4 2000000 9892 ns/op 16 B/op 1 allocs/op
BenchmarkSynchronizedBuiltinMap/Delete/1000/100-4 200000 68712 ns/op 16 B/op 1 allocs/op
BenchmarkSynchronizedBuiltinMap/Delete/1000/1000-4 50000 399065 ns/op 18 B/op 1 allocs/op
BenchmarkSyncMap/Delete/1000/1-4 10000000 1325 ns/op 16 B/op 1 allocs/op
BenchmarkSyncMap/Delete/1000/10-4 2000000 8876 ns/op 16 B/op 1 allocs/op
BenchmarkSyncMap/Delete/1000/100-4 200000 63251 ns/op 16 B/op 1 allocs/op
BenchmarkSyncMap/Delete/1000/1000-4 50000 385645 ns/op 18 B/op 1 allocs/op
BenchmarkConcurrentHashMap/Delete/1000/1-4 5000000 3149 ns/op 55 B/op 7 allocs/op
BenchmarkConcurrentHashMap/Delete/1000/10-4 1000000 16714 ns/op 415 B/op 70 allocs/op
BenchmarkConcurrentHashMap/Delete/1000/100-4 200000 94336 ns/op 4016 B/op 700 allocs/op
BenchmarkConcurrentHashMap/Delete/1000/1000-4 30000 575415 ns/op 40134 B/op 6999 allocs/op
Repository
Link: https://github.com/thewizardplusplus/go-hashmap/tree/v1.2.
Content: code.
License: MIT.