Skip to content

Commit c32385f

Browse files
authored
[patch] Fix value type of generics map (#133)
* fix value type of generics map Signed-off-by: hlts2 <[email protected]> * delete unnecessary type declaration Signed-off-by: hlts2 <[email protected]> --------- Signed-off-by: hlts2 <[email protected]>
1 parent 2fcc8ab commit c32385f

File tree

2 files changed

+11
-15
lines changed

2 files changed

+11
-15
lines changed

gache_benchmark_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func BenchmarkGacheSetSmallDataNoTTL(b *testing.B) {
146146
}
147147

148148
func BenchmarkGacheSetSmallDataWithTTL(b *testing.B) {
149-
g := New[string](
149+
g := New(
150150
WithDefaultExpiration[string](ttl),
151151
)
152152
benchmark(b, smallData, ttl,
@@ -155,7 +155,7 @@ func BenchmarkGacheSetSmallDataWithTTL(b *testing.B) {
155155
}
156156

157157
func BenchmarkGacheSetBigDataNoTTL(b *testing.B) {
158-
g := New[string](
158+
g := New(
159159
WithDefaultExpiration[string](NoTTL),
160160
)
161161
benchmark(b, bigData, NoTTL,
@@ -164,7 +164,7 @@ func BenchmarkGacheSetBigDataNoTTL(b *testing.B) {
164164
}
165165

166166
func BenchmarkGacheSetBigDataWithTTL(b *testing.B) {
167-
g := New[string](
167+
g := New(
168168
WithDefaultExpiration[string](ttl),
169169
)
170170
benchmark(b, bigData, ttl,

map.go

+8-12
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,27 @@ package gache
33
import (
44
"sync"
55
"sync/atomic"
6+
"unsafe"
67
)
78

8-
type Map[K, V comparable] struct {
9+
type Map[K comparable, V any] struct {
910
mu sync.Mutex
1011
read atomic.Pointer[readOnly[K, V]]
1112
dirty map[K]*entry[V]
1213
misses int
1314
}
1415

15-
type readOnly[K, V comparable] struct {
16+
type readOnly[K comparable, V any] struct {
1617
m map[K]*entry[V]
1718
amended bool
1819
}
1920

20-
type entry[V comparable] struct {
21+
type entry[V any] struct {
2122
expunged atomic.Pointer[V]
2223
p atomic.Pointer[V]
2324
}
2425

25-
func newEntry[V comparable](v V) (e *entry[V]) {
26+
func newEntry[V any](v V) (e *entry[V]) {
2627
e = &entry[V]{}
2728
e.expunged.Store(new(V))
2829
e.p.Store(&v)
@@ -46,7 +47,6 @@ func (m *Map[K, V]) Load(key K) (value V, ok bool) {
4647
e, ok = read.m[key]
4748
if !ok && read.amended {
4849
e, ok = m.dirty[key]
49-
5050
m.missLocked()
5151
}
5252
m.mu.Unlock()
@@ -71,7 +71,7 @@ func (m *Map[K, V]) Store(key K, value V) {
7171

7272
func (e *entry[V]) tryCompareAndSwap(old, new V) (ok bool) {
7373
p := e.p.Load()
74-
if p == nil || p == e.expunged.Load() || *p != old {
74+
if p == nil || p == e.expunged.Load() || unsafe.Pointer(p) != unsafe.Pointer(&old) {
7575
return false
7676
}
7777

@@ -81,7 +81,7 @@ func (e *entry[V]) tryCompareAndSwap(old, new V) (ok bool) {
8181
return true
8282
}
8383
p = e.p.Load()
84-
if p == nil || p == e.expunged.Load() || *p != old {
84+
if p == nil || p == e.expunged.Load() || unsafe.Pointer(p) != unsafe.Pointer(&old) {
8585
return false
8686
}
8787
}
@@ -116,7 +116,6 @@ func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
116116
m.missLocked()
117117
} else {
118118
if !read.amended {
119-
120119
m.dirtyLocked()
121120
m.read.Store(&readOnly[K, V]{m: read.m, amended: true})
122121
}
@@ -253,7 +252,6 @@ func (m *Map[K, V]) CompareAndSwap(key K, old, new V) (swapped bool) {
253252
swapped = e.tryCompareAndSwap(old, new)
254253
} else if e, ok := m.dirty[key]; ok {
255254
swapped = e.tryCompareAndSwap(old, new)
256-
257255
m.missLocked()
258256
}
259257
return swapped
@@ -268,14 +266,13 @@ func (m *Map[K, V]) CompareAndDelete(key K, old V) (deleted bool) {
268266
e, ok = read.m[key]
269267
if !ok && read.amended {
270268
e, ok = m.dirty[key]
271-
272269
m.missLocked()
273270
}
274271
m.mu.Unlock()
275272
}
276273
for ok {
277274
p := e.p.Load()
278-
if p == nil || p == e.expunged.Load() || *p != old {
275+
if p == nil || p == e.expunged.Load() || unsafe.Pointer(p) != unsafe.Pointer(&old) {
279276
return false
280277
}
281278
if e.p.CompareAndSwap(p, nil) {
@@ -286,7 +283,6 @@ func (m *Map[K, V]) CompareAndDelete(key K, old V) (deleted bool) {
286283
}
287284

288285
func (m *Map[K, V]) Range(f func(key K, value V) bool) {
289-
290286
read := m.loadReadOnly()
291287
if read.amended {
292288

0 commit comments

Comments
 (0)