David Chou
david74.chou @ facebook
david74.chou @ medium
david7482 @ github
Dave Cheney, GoCon Spring 2018
template<
class Key,
class T,
class Hash = std::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Allocator = std::allocator< std::pair<const Key, T> >
> class unordered_map;Class HashMap<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<K,V>
Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values
var m map[string]intGo uses HashMap
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| key | value |
|---|---|
Hashmap
Bucket: 3
| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| key | value |
|---|---|
| pkg/errors | 2903 |
| spf13/cobra | 7136 |
| golang/go | 40260 |
Hashmap
Bucket: 3
"golang/go"
HashFunction
78356113
Mask
template<
class Key,
class T,
class Hash = std::hash<Key>,
class KeyEqual = std::equal_to<Key>,
class Allocator = std::allocator< std::pair<const Key, T> >
> class unordered_map;| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| key | value |
|---|---|
| pkg/errors | 2903 |
| spf13/cobra | 7136 |
| golang/go | 40260 |
Hashmap
Bucket: 3
"golang/go"
std::hash<key>
78356113
Mask
std::equal_to<key>
Class HashMap<K,V>
java.lang.Object
java.util.AbstractMap<K,V>
java.util.HashMap<K,V>
Type Parameters:
K - the type of keys maintained by this map
V - the type of mapped values| 0 |
| 1 |
| 2 |
| 3 |
| 4 |
| 5 |
| 6 |
| 7 |
| key | value | next |
|---|---|---|
| pkg/errors | 2903 |
Hashmap
Bucket: 3
"golang/go"
key.hashCode()
78356113
Mask
| spf13/cobra | 7136 |
| golang/go | 40260 | null |
key.equals()
v := m["key"] → runtime.mapaccess1(m, ”key", &v)
v, ok := m["key"] → runtime.mapaccess2(m, ”key”, &v, &ok)
m["key"] = 9001 → runtime.mapinsert(m, ”key", 9001)
delete(m, "key") → runtime.mapdelete(m, “key”)
func mapaccess1(t *maptype, h *hmap, key unsafe.Pointer) unsafe.Pointer
map[string]int → var mt1 maptype{...}
map[string]http.Header → var mt2 maptype{...}
map[structA]structB → var mt3 maptype{...}
type maptype struct {
typ _type
key *_type
elem *_type
bucket *_type // internal type representing a hash bucket
hmap *_type // internal type representing a hmap
keysize uint8 // size of key slot
indirectkey bool // store ptr to key instead of key itself
valuesize uint8 // size of value slot
indirectvalue bool // store ptr to value instead of value itself
bucketsize uint16 // size of bucket
reflexivekey bool // true if k==k for all keys
needkeyupdate bool // true if we need to update key on overwrite
}type _type struct {
size uintptr
alg *typeAlg
...
}type typeAlg struct {
// function for hashing objects of this type
// (ptr to object, seed) -> hash
hash func(unsafe.Pointer, uintptr) uintptr
// function for comparing objects of this type
// (ptr to object A, ptr to object B) -> ==?
equal func(unsafe.Pointer, unsafe.Pointer) bool
}map<K0,V0>
map<K0,V0>
map<K0,V0>
map<K0,V0>
Compile Time
map<K,V>
Run Time
Object0
Object0
Object0
Object0
map<K,V>
Compile Time
maptype0
maptype0
maptype0
maptype0