├── LICENSE ├── README.md ├── concurrent_map.go ├── concurrent_map_benchmark_adapter.go ├── concurrent_map_test.go ├── int64_key.go ├── map_benchmark.png ├── map_benchmark_test.go ├── rwlock_map_benchmark_adapter.go ├── string_key.go └── sync_map_benchmark_adapter.go /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ConcurrentMap for GO 2 | ## The better performance thread-safe map in GO 3 | 4 | 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. 5 | 6 | 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. 7 | 8 | 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. 9 | 10 | 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) 11 | 12 | ![image](https://github.com/easierway/concurrent_map/blob/master/map_benchmark.png) 13 | 14 | ### FAQ 15 | 1 Why not provide the default hash function for partition? 16 | 17 | 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. 18 | -------------------------------------------------------------------------------- /concurrent_map.go: -------------------------------------------------------------------------------- 1 | package concurrent_map 2 | 3 | import ( 4 | "sync" 5 | ) 6 | 7 | // ConcurrentMap is a thread safe map collection with better performance. 8 | // The backend map entries are separated into the different partitions. 9 | // Threads can access the different partitions safely without lock. 10 | type ConcurrentMap struct { 11 | partitions []*innerMap 12 | numOfBlockets int 13 | } 14 | 15 | // Partitionable is the interface which should be implemented by key type. 16 | // It is to define how to partition the entries. 17 | type Partitionable interface { 18 | // Value is raw value of the key 19 | Value() interface{} 20 | 21 | // PartitionKey is used for getting the partition to store the entry with the key. 22 | // E.g. the key's hash could be used as its PartitionKey 23 | // The partition for the key is partitions[(PartitionKey % m.numOfBlockets)] 24 | // 25 | // 1 Why not provide the default hash function for partition? 26 | // Ans: As you known, the partition solution would impact the performance significantly. 27 | // The proper partition solution balances the access to the different partitions and 28 | // avoid of the hot partition. The access mode highly relates to your business. 29 | // So, the better partition solution would just be designed according to your business. 30 | PartitionKey() int64 31 | } 32 | 33 | type innerMap struct { 34 | m map[interface{}]interface{} 35 | lock sync.RWMutex 36 | } 37 | 38 | func createInnerMap() *innerMap { 39 | return &innerMap{ 40 | m: make(map[interface{}]interface{}), 41 | } 42 | } 43 | 44 | func (im *innerMap) get(key Partitionable) (interface{}, bool) { 45 | keyVal := key.Value() 46 | im.lock.RLock() 47 | v, ok := im.m[keyVal] 48 | im.lock.RUnlock() 49 | return v, ok 50 | } 51 | 52 | func (im *innerMap) set(key Partitionable, v interface{}) { 53 | keyVal := key.Value() 54 | im.lock.Lock() 55 | im.m[keyVal] = v 56 | im.lock.Unlock() 57 | } 58 | 59 | func (im *innerMap) del(key Partitionable) { 60 | keyVal := key.Value() 61 | im.lock.Lock() 62 | delete(im.m, keyVal) 63 | im.lock.Unlock() 64 | } 65 | 66 | // CreateConcurrentMap is to create a ConcurrentMap with the setting number of the partitions 67 | func CreateConcurrentMap(numOfPartitions int) *ConcurrentMap { 68 | var partitions []*innerMap 69 | for i := 0; i < numOfPartitions; i++ { 70 | partitions = append(partitions, createInnerMap()) 71 | } 72 | return &ConcurrentMap{partitions, numOfPartitions} 73 | } 74 | 75 | func (m *ConcurrentMap) getPartition(key Partitionable) *innerMap { 76 | partitionID := key.PartitionKey() % int64(m.numOfBlockets) 77 | return (*innerMap)(m.partitions[partitionID]) 78 | } 79 | 80 | // Get is to get the value by the key 81 | func (m *ConcurrentMap) Get(key Partitionable) (interface{}, bool) { 82 | return m.getPartition(key).get(key) 83 | } 84 | 85 | // Set is to store the KV entry to the map 86 | func (m *ConcurrentMap) Set(key Partitionable, v interface{}) { 87 | im := m.getPartition(key) 88 | im.set(key, v) 89 | } 90 | 91 | // Del is to delete the entries by the key 92 | func (m *ConcurrentMap) Del(key Partitionable) { 93 | im := m.getPartition(key) 94 | im.del(key) 95 | } 96 | -------------------------------------------------------------------------------- /concurrent_map_benchmark_adapter.go: -------------------------------------------------------------------------------- 1 | package concurrent_map 2 | 3 | type ConcurrentMapBenchmarkAdapter struct { 4 | cm *ConcurrentMap 5 | } 6 | 7 | func (m *ConcurrentMapBenchmarkAdapter) Set(key interface{}, value interface{}) { 8 | m.cm.Set(StrKey(key.(string)), value) 9 | } 10 | 11 | func (m *ConcurrentMapBenchmarkAdapter) Get(key interface{}) (interface{}, bool) { 12 | return m.cm.Get(StrKey(key.(string))) 13 | } 14 | 15 | func (m *ConcurrentMapBenchmarkAdapter) Del(key interface{}) { 16 | m.cm.Del(StrKey(key.(string))) 17 | } 18 | 19 | func CreateConcurrentMapBenchmarkAdapter(numOfPartitions int) *ConcurrentMapBenchmarkAdapter { 20 | conMap := CreateConcurrentMap(numOfPartitions) 21 | return &ConcurrentMapBenchmarkAdapter{conMap} 22 | } 23 | -------------------------------------------------------------------------------- /concurrent_map_test.go: -------------------------------------------------------------------------------- 1 | package concurrent_map 2 | 3 | import ( 4 | "math/rand" 5 | "strconv" 6 | "testing" 7 | "time" 8 | ) 9 | 10 | func TestBasicOPs(t *testing.T) { 11 | for i := 0; i < 10; i++ { 12 | testV := rand.Intn(1000) 13 | m := CreateConcurrentMap(99) 14 | v, ok := m.Get(StrKey("Hello")) 15 | if v != nil || ok != false { 16 | t.Error("init/get failed") 17 | } 18 | m.Set(StrKey("Hello"), testV) 19 | v, ok = m.Get(StrKey("Hello")) 20 | if v.(int) != testV || ok != true { 21 | t.Error("set/get failed.") 22 | } 23 | m.Del(StrKey("Hello")) 24 | v, ok = m.Get(StrKey("Hello")) 25 | if v != nil || ok != false { 26 | t.Error("del failed") 27 | } 28 | } 29 | } 30 | 31 | func TestInt64KeyBasicOPs(t *testing.T) { 32 | for i := 0; i < 10; i++ { 33 | testV := rand.Int63n(1024) 34 | cm := CreateConcurrentMap(99) 35 | var key int64 = 1023 36 | v, ok := cm.Get(I64Key(key)) 37 | if v != nil || ok != false { 38 | t.Error("init/get failed") 39 | } 40 | cm.Set(I64Key(key), testV) 41 | v, ok = cm.Get(I64Key(key)) 42 | if v.(int64) != testV || ok != true { 43 | t.Error("set/get failed.") 44 | } 45 | cm.Del(I64Key(key)) 46 | v, ok = cm.Get(I64Key(key)) 47 | if v != nil || ok != false { 48 | t.Error("del failed") 49 | } 50 | } 51 | } 52 | 53 | func TestInCurrentEnv(t *testing.T) { 54 | m := CreateConcurrentMap(99) 55 | go func() { 56 | 57 | for i := 0; i < 100; i++ { 58 | if v, ok := m.Get(StrKey(strconv.Itoa(i))); ok { 59 | if v != i*i { 60 | t.Error("Fail") 61 | } 62 | } else { 63 | if v != nil { 64 | t.Error("Fail") 65 | } 66 | } 67 | 68 | } 69 | 70 | }() 71 | go func() { 72 | for i := 0; i < 100; i++ { 73 | m.Set(StrKey(strconv.Itoa(i)), i*i) 74 | } 75 | 76 | }() 77 | time.Sleep(time.Second * 1) 78 | } 79 | -------------------------------------------------------------------------------- /int64_key.go: -------------------------------------------------------------------------------- 1 | package concurrent_map 2 | 3 | type Int64Key struct { 4 | value int64 5 | } 6 | 7 | // PartitionID is created by string's hash 8 | func (i *Int64Key) PartitionKey() int64 { 9 | return i.value 10 | } 11 | 12 | // Value is the raw string 13 | func (i *Int64Key) Value() interface{} { 14 | return i.value 15 | } 16 | 17 | // StrKey is to convert a string to StringKey 18 | func I64Key(key int64) *Int64Key { 19 | return &Int64Key{key} 20 | } 21 | -------------------------------------------------------------------------------- /map_benchmark.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/easierway/concurrent_map/7073b0dd7e9577513a78225491cb4a68e6d62a86/map_benchmark.png -------------------------------------------------------------------------------- /map_benchmark_test.go: -------------------------------------------------------------------------------- 1 | package concurrent_map 2 | 3 | import ( 4 | "strconv" 5 | "sync" 6 | "testing" 7 | ) 8 | 9 | type Map interface { 10 | Set(key interface{}, val interface{}) 11 | Get(key interface{}) (interface{}, bool) 12 | Del(key interface{}) 13 | } 14 | 15 | func benchmarkMap(b *testing.B, hm Map) { 16 | for i := 0; i < b.N; i++ { 17 | var wg sync.WaitGroup 18 | for i := 0; i < 100; i++ { 19 | wg.Add(1) 20 | go func() { 21 | for i := 0; i < 100000; i++ { 22 | hm.Set(strconv.Itoa(i), i*i) 23 | hm.Set(strconv.Itoa(i), i*i) 24 | hm.Del(strconv.Itoa(i)) 25 | } 26 | wg.Done() 27 | }() 28 | } 29 | for i := 0; i < 100; i++ { 30 | wg.Add(1) 31 | go func() { 32 | for i := 0; i < 100000; i++ { 33 | hm.Get(strconv.Itoa(i)) 34 | } 35 | wg.Done() 36 | }() 37 | } 38 | wg.Wait() 39 | } 40 | } 41 | 42 | func BenchmarkSyncmap(b *testing.B) { 43 | b.Run("map with RWLock", func(b *testing.B) { 44 | hm := CreateRWLockMap() 45 | benchmarkMap(b, hm) 46 | }) 47 | 48 | b.Run("sync.map", func(b *testing.B) { 49 | hm := CreateSyncMapBenchmarkAdapter() 50 | benchmarkMap(b, hm) 51 | }) 52 | 53 | b.Run("concurrent map", func(b *testing.B) { 54 | superman := CreateConcurrentMapBenchmarkAdapter(199) 55 | benchmarkMap(b, superman) 56 | }) 57 | } 58 | -------------------------------------------------------------------------------- /rwlock_map_benchmark_adapter.go: -------------------------------------------------------------------------------- 1 | package concurrent_map 2 | 3 | import "sync" 4 | 5 | type RWLockMap struct { 6 | m map[interface{}]interface{} 7 | lock sync.RWMutex 8 | } 9 | 10 | func (m *RWLockMap) Get(key interface{}) (interface{}, bool) { 11 | m.lock.RLock() 12 | v, ok := m.m[key] 13 | m.lock.RUnlock() 14 | return v, ok 15 | } 16 | 17 | func (m *RWLockMap) Set(key interface{}, value interface{}) { 18 | m.lock.Lock() 19 | m.m[key] = value 20 | m.lock.Unlock() 21 | } 22 | 23 | func (m *RWLockMap) Del(key interface{}) { 24 | m.lock.Lock() 25 | delete(m.m, key) 26 | m.lock.Unlock() 27 | } 28 | 29 | func CreateRWLockMap() *RWLockMap { 30 | m := make(map[interface{}]interface{}, 0) 31 | return &RWLockMap{m: m} 32 | } 33 | -------------------------------------------------------------------------------- /string_key.go: -------------------------------------------------------------------------------- 1 | package concurrent_map 2 | 3 | import "unsafe" 4 | 5 | // StringKey is for the string type key 6 | type StringKey struct { 7 | value string 8 | } 9 | 10 | const ( 11 | c1_32 uint32 = 0xcc9e2d51 12 | c2_32 uint32 = 0x1b873593 13 | ) 14 | 15 | func hash(str string) uint32 { 16 | data := ([]byte)(str) 17 | var h1 uint32 = 37 18 | 19 | nblocks := len(data) / 4 20 | var p uintptr 21 | if len(data) > 0 { 22 | p = uintptr(unsafe.Pointer(&data[0])) 23 | } 24 | 25 | p1 := p + uintptr(4*nblocks) 26 | for ; p < p1; p += 4 { 27 | k1 := *(*uint32)(unsafe.Pointer(p)) 28 | k1 *= c1_32 29 | k1 = (k1 << 15) | (k1 >> 17) // rotl32(k1, 15) 30 | k1 *= c2_32 31 | 32 | h1 ^= k1 33 | h1 = (h1 << 13) | (h1 >> 19) // rotl32(h1, 13) 34 | h1 = h1*5 + 0xe6546b64 35 | } 36 | 37 | tail := data[nblocks*4:] 38 | 39 | var k1 uint32 40 | switch len(tail) & 3 { 41 | case 3: 42 | k1 ^= uint32(tail[2]) << 16 43 | fallthrough 44 | case 2: 45 | k1 ^= uint32(tail[1]) << 8 46 | fallthrough 47 | case 1: 48 | k1 ^= uint32(tail[0]) 49 | k1 *= c1_32 50 | k1 = (k1 << 15) | (k1 >> 17) // rotl32(k1, 15) 51 | k1 *= c2_32 52 | h1 ^= k1 53 | } 54 | 55 | h1 ^= uint32(len(data)) 56 | 57 | h1 ^= h1 >> 16 58 | h1 *= 0x85ebca6b 59 | h1 ^= h1 >> 13 60 | h1 *= 0xc2b2ae35 61 | h1 ^= h1 >> 16 62 | 63 | return (h1 << 24) | (((h1 >> 8) << 16) & 0xFF0000) | (((h1 >> 16) << 8) & 0xFF00) | (h1 >> 24) 64 | } 65 | 66 | // PartitionID is created by string's hash 67 | func (s *StringKey) PartitionKey() int64 { 68 | return int64(hash(s.value)) 69 | } 70 | 71 | // Value is the raw string 72 | func (s *StringKey) Value() interface{} { 73 | return s.value 74 | } 75 | 76 | // StrKey is to convert a string to StringKey 77 | func StrKey(key string) *StringKey { 78 | return &StringKey{key} 79 | } 80 | -------------------------------------------------------------------------------- /sync_map_benchmark_adapter.go: -------------------------------------------------------------------------------- 1 | package concurrent_map 2 | 3 | import "sync" 4 | 5 | func CreateSyncMapBenchmarkAdapter() *SyncMapBenchmarkAdapter { 6 | return &SyncMapBenchmarkAdapter{} 7 | } 8 | 9 | type SyncMapBenchmarkAdapter struct { 10 | m sync.Map 11 | } 12 | 13 | func (m *SyncMapBenchmarkAdapter) Set(key interface{}, val interface{}) { 14 | m.m.Store(key, val) 15 | } 16 | 17 | func (m *SyncMapBenchmarkAdapter) Get(key interface{}) (interface{}, bool) { 18 | return m.m.Load(key) 19 | } 20 | 21 | func (m *SyncMapBenchmarkAdapter) Del(key interface{}) { 22 | m.m.Delete(key) 23 | } 24 | --------------------------------------------------------------------------------