-
Notifications
You must be signed in to change notification settings - Fork 11
⚡ Optimize Size() method to use lock-free atomic counter #205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -81,6 +81,8 @@ type ( | |||||||||||||
| expire int64 | ||||||||||||||
| maxKeyLength uint64 | ||||||||||||||
| maxWorkers int | ||||||||||||||
| elementOverhead uintptr | ||||||||||||||
| totalSize atomic.Uintptr | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| value[V any] struct { | ||||||||||||||
|
|
@@ -154,7 +156,9 @@ func New[V any](opts ...Option[V]) Gache[V] { | |||||||||||||
| }, opts...) { | ||||||||||||||
| opt(g) | ||||||||||||||
| } | ||||||||||||||
| g.expChan = make(chan kv[V], len(g.shards)*10) | ||||||||||||||
| g.expChan = make(chan kv[V], len(g.shards)*10) | ||||||||||||||
| g.elementOverhead = unsafe.Sizeof(value[V]{}) + unsafe.Sizeof(entry[V]{}) + 48 | ||||||||||||||
| g.totalSize.Store(unsafe.Sizeof(*g) + uintptr(len(g.shards))*unsafe.Sizeof(Map[string, value[V]]{})) | ||||||||||||||
|
Comment on lines
+159
to
+161
|
||||||||||||||
| return g | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -514,6 +518,8 @@ func (g *gache[V]) set(key string, val V, expire int64) { | |||||||||||||
| if loaded { | ||||||||||||||
| old.reset() | ||||||||||||||
| g.valPool.Put(old) | ||||||||||||||
| } else { | ||||||||||||||
| g.totalSize.Add(uintptr(len(key)) + g.elementOverhead) | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
|
|
@@ -563,6 +569,7 @@ func (g *gache[V]) Delete(key string) (v V, loaded bool) { | |||||||||||||
| } | ||||||||||||||
| v = val.val | ||||||||||||||
| val.mu.RUnlock() | ||||||||||||||
| g.totalSize.Add(^(uintptr(len(key)) + g.elementOverhead - 1)) | ||||||||||||||
| val.reset() | ||||||||||||||
| g.valPool.Put(val) | ||||||||||||||
| return v, true | ||||||||||||||
|
|
@@ -736,15 +743,7 @@ func (g *gache[V]) Len() (l int) { | |||||||||||||
| // gc.Set("k", "v") | ||||||||||||||
| // fmt.Printf("cache size: %d bytes\n", gc.Size()) | ||||||||||||||
| func (g *gache[V]) Size() (size uintptr) { | ||||||||||||||
| size += unsafe.Sizeof(g.expFuncEnabled) // bool | ||||||||||||||
| size += unsafe.Sizeof(g.expire) // int64 | ||||||||||||||
| size += unsafe.Sizeof(g.cancel) // atomic.Pointer[context.CancelFunc] | ||||||||||||||
| size += unsafe.Sizeof(g.expChan) // chan kv[V] | ||||||||||||||
| size += unsafe.Sizeof(g.expFunc) // func(context.Context, string, V) | ||||||||||||||
| for _, shard := range g.shards { | ||||||||||||||
| size += shard.Size() | ||||||||||||||
| } | ||||||||||||||
| return size | ||||||||||||||
| return g.totalSize.Load() | ||||||||||||||
|
||||||||||||||
| return g.totalSize.Load() | |
| size = unsafe.Sizeof(*g) | |
| for i := range g.shards { | |
| size += g.shards[i].Size() | |
| } | |
| return size |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Several newly added lines in this file appear to be mis-indented (e.g., the g.expChan assignment in New, the totalSize adjustment in Pop, and the Add call in SetWithExpireIfNotExists). Please run gofmt on gache.go to keep formatting consistent and avoid churn/CI lint failures.