Skip to content

Commit

Permalink
Revert "[patch] Fix value type of generics map (#133)" (#135)
Browse files Browse the repository at this point in the history
This reverts commit c32385f.
  • Loading branch information
kpango authored Jul 10, 2023
1 parent 90144d1 commit 833b06d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
6 changes: 3 additions & 3 deletions gache_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func BenchmarkGacheSetSmallDataNoTTL(b *testing.B) {
}

func BenchmarkGacheSetSmallDataWithTTL(b *testing.B) {
g := New(
g := New[string](
WithDefaultExpiration[string](ttl),
)
benchmark(b, smallData, ttl,
Expand All @@ -155,7 +155,7 @@ func BenchmarkGacheSetSmallDataWithTTL(b *testing.B) {
}

func BenchmarkGacheSetBigDataNoTTL(b *testing.B) {
g := New(
g := New[string](
WithDefaultExpiration[string](NoTTL),
)
benchmark(b, bigData, NoTTL,
Expand All @@ -164,7 +164,7 @@ func BenchmarkGacheSetBigDataNoTTL(b *testing.B) {
}

func BenchmarkGacheSetBigDataWithTTL(b *testing.B) {
g := New(
g := New[string](
WithDefaultExpiration[string](ttl),
)
benchmark(b, bigData, ttl,
Expand Down
17 changes: 7 additions & 10 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@ package gache
import (
"sync"
"sync/atomic"
"unsafe"
)

type Map[K comparable, V any] struct {
type Map[K, V comparable] struct {
mu sync.Mutex
read atomic.Pointer[readOnly[K, V]]
dirty map[K]*entry[V]
misses int
}

type readOnly[K comparable, V any] struct {
type readOnly[K, V comparable] struct {
m map[K]*entry[V]
amended bool
}

type entry[V any] struct {
type entry[V comparable] struct {
expunged atomic.Pointer[V]
p atomic.Pointer[V]
}

func newEntry[V any](v V) (e *entry[V]) {
func newEntry[V comparable](v V) (e *entry[V]) {
e = &entry[V]{}
e.expunged.Store(new(V))
e.p.Store(&v)
Expand All @@ -42,7 +41,6 @@ func (m *Map[K, V]) Load(key K) (value V, ok bool) {
e, ok := read.m[key]
if !ok && read.amended {
m.mu.Lock()

read = m.loadReadOnly()
e, ok = read.m[key]
if !ok && read.amended {
Expand Down Expand Up @@ -71,7 +69,7 @@ func (m *Map[K, V]) Store(key K, value V) {

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

Expand All @@ -81,7 +79,7 @@ func (e *entry[V]) tryCompareAndSwap(old, new V) (ok bool) {
return true
}
p = e.p.Load()
if p == nil || p == e.expunged.Load() || unsafe.Pointer(p) != unsafe.Pointer(&old) {
if p == nil || p == e.expunged.Load() || *p != old {
return false
}
}
Expand Down Expand Up @@ -123,7 +121,6 @@ func (m *Map[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
actual, loaded = value, false
}
m.mu.Unlock()

return actual, loaded
}

Expand Down Expand Up @@ -272,7 +269,7 @@ func (m *Map[K, V]) CompareAndDelete(key K, old V) (deleted bool) {
}
for ok {
p := e.p.Load()
if p == nil || p == e.expunged.Load() || unsafe.Pointer(p) != unsafe.Pointer(&old) {
if p == nil || p == e.expunged.Load() || *p != old {
return false
}
if e.p.CompareAndSwap(p, nil) {
Expand Down

0 comments on commit 833b06d

Please sign in to comment.