diff options
| author | 2023-06-21 20:08:48 +0100 | |
|---|---|---|
| committer | 2023-06-21 21:08:48 +0200 | |
| commit | 8e0043104d3792c33850f948f1b080acd44eda90 (patch) | |
| tree | 2bf7dca025024b6a6189a8634c33ec697346c4ae /vendor/codeberg.org/gruf/go-cache | |
| parent | [feature] Add partial text search for accounts + statuses (#1836) (diff) | |
| download | gotosocial-8e0043104d3792c33850f948f1b080acd44eda90.tar.xz | |
[performance] update go-cache library (#1917)
* update go-cache library
Signed-off-by: kim <grufwub@gmail.com>
* fix broken test after cache library upgrade
Signed-off-by: kim <grufwub@gmail.com>
* fix the webfinger test
Signed-off-by: kim <grufwub@gmail.com>
---------
Signed-off-by: kim <grufwub@gmail.com>
Diffstat (limited to 'vendor/codeberg.org/gruf/go-cache')
| -rw-r--r-- | vendor/codeberg.org/gruf/go-cache/v3/result/cache.go | 16 | ||||
| -rw-r--r-- | vendor/codeberg.org/gruf/go-cache/v3/ttl/ttl.go | 56 | 
2 files changed, 52 insertions, 20 deletions
| diff --git a/vendor/codeberg.org/gruf/go-cache/v3/result/cache.go b/vendor/codeberg.org/gruf/go-cache/v3/result/cache.go index 5b16712b6..74644b073 100644 --- a/vendor/codeberg.org/gruf/go-cache/v3/result/cache.go +++ b/vendor/codeberg.org/gruf/go-cache/v3/result/cache.go @@ -4,6 +4,7 @@ import (  	"context"  	"reflect"  	"time" +	_ "unsafe"  	"codeberg.org/gruf/go-cache/v3/ttl"  	"codeberg.org/gruf/go-errors/v2" @@ -385,7 +386,7 @@ func (c *Cache[Value]) store(res result[Value]) (evict func()) {  	// Store main entry under primary key, using evict hook if needed  	c.cache.Cache.SetWithHook(pnext, &ttl.Entry[int64, result[Value]]{ -		Expiry: time.Now().Add(c.cache.TTL), +		Expiry: c.expiry(),  		Key:    pnext,  		Value:  res,  	}, func(_ int64, item *ttl.Entry[int64, result[Value]]) { @@ -395,6 +396,19 @@ func (c *Cache[Value]) store(res result[Value]) (evict func()) {  	return evict  } +//go:linkname runtime_nanotime runtime.nanotime +func runtime_nanotime() uint64 + +// expiry returns an the next expiry time to use for an entry, +// which is equivalent to time.Now().Add(ttl), or zero if disabled. +func (c *Cache[Value]) expiry() uint64 { +	if ttl := c.cache.TTL; ttl > 0 { +		return runtime_nanotime() + +			uint64(c.cache.TTL) +	} +	return 0 +} +  type result[Value any] struct {  	// keys accessible under  	Keys cacheKeys diff --git a/vendor/codeberg.org/gruf/go-cache/v3/ttl/ttl.go b/vendor/codeberg.org/gruf/go-cache/v3/ttl/ttl.go index a5d9d6fc3..623a19910 100644 --- a/vendor/codeberg.org/gruf/go-cache/v3/ttl/ttl.go +++ b/vendor/codeberg.org/gruf/go-cache/v3/ttl/ttl.go @@ -3,6 +3,7 @@ package ttl  import (  	"sync"  	"time" +	_ "unsafe"  	"codeberg.org/gruf/go-maps"  ) @@ -11,7 +12,7 @@ import (  type Entry[Key comparable, Value any] struct {  	Key    Key  	Value  Value -	Expiry time.Time +	Expiry uint64  }  // Cache is the underlying Cache implementation, providing both the base Cache interface and unsafe access to underlying map to allow flexibility in building your own. @@ -67,7 +68,7 @@ func (c *Cache[K, V]) Start(freq time.Duration) (ok bool) {  	// Safely start  	c.Lock() -	if ok = c.stop == nil; ok { +	if ok = (c.stop == nil); ok {  		// Not yet running, schedule us  		c.stop = schedule(c.Sweep, freq)  	} @@ -83,7 +84,7 @@ func (c *Cache[K, V]) Stop() (ok bool) {  	// Safely stop  	c.Lock() -	if ok = c.stop != nil; ok { +	if ok = (c.stop != nil); ok {  		// We're running, cancel evicts  		c.stop()  		c.stop = nil @@ -96,23 +97,32 @@ func (c *Cache[K, V]) Stop() (ok bool) {  }  // Sweep attempts to evict expired items (with callback!) from cache. -func (c *Cache[K, V]) Sweep(now time.Time) { +func (c *Cache[K, V]) Sweep(_ time.Time) {  	var (  		// evicted key-values.  		kvs []kv[K, V]  		// hook func ptrs.  		evict func(K, V) + +		// get current nanoseconds. +		now = runtime_nanotime()  	)  	c.locked(func() { +		if c.TTL <= 0 { +			// sweep is +			// disabled +			return +		} +  		// Sentinel value  		after := -1  		// The cache will be ordered by expiry date, we iterate until we reach the index of  		// the youngest item that hsa expired, as all succeeding items will also be expired.  		c.Cache.RangeIf(0, c.Cache.Len(), func(i int, _ K, item *Entry[K, V]) bool { -			if now.After(item.Expiry) { +			if now > item.Expiry {  				after = i  				// evict all older items @@ -161,10 +171,6 @@ func (c *Cache[K, V]) SetInvalidateCallback(hook func(K, V)) {  // SetTTL: implements cache.Cache's SetTTL().  func (c *Cache[K, V]) SetTTL(ttl time.Duration, update bool) { -	if ttl < 0 { -		panic("ttl must be greater than zero") -	} -  	c.locked(func() {  		// Set updated TTL  		diff := ttl - c.TTL @@ -173,7 +179,7 @@ func (c *Cache[K, V]) SetTTL(ttl time.Duration, update bool) {  		if update {  			// Update existing cache entries with new expiry time  			c.Cache.Range(0, c.Cache.Len(), func(i int, _ K, item *Entry[K, V]) { -				item.Expiry = item.Expiry.Add(diff) +				item.Expiry += uint64(diff)  			})  		}  	}) @@ -198,8 +204,8 @@ func (c *Cache[K, V]) Get(key K) (V, bool) {  			return  		} -		// Update fetched item's expiry -		item.Expiry = time.Now().Add(c.TTL) +		// Update fetched's expiry +		item.Expiry = c.expiry()  		// Set value.  		v = item.Value @@ -234,7 +240,7 @@ func (c *Cache[K, V]) Add(key K, value V) bool {  		// Alloc new entry.  		new := c.alloc() -		new.Expiry = time.Now().Add(c.TTL) +		new.Expiry = c.expiry()  		new.Key = key  		new.Value = value @@ -290,12 +296,12 @@ func (c *Cache[K, V]) Set(key K, value V) {  			oldV = item.Value  			// Update the existing item. -			item.Expiry = time.Now().Add(c.TTL) +			item.Expiry = c.expiry()  			item.Value = value  		} else {  			// Alloc new entry.  			new := c.alloc() -			new.Expiry = time.Now().Add(c.TTL) +			new.Expiry = c.expiry()  			new.Key = key  			new.Value = value @@ -355,7 +361,7 @@ func (c *Cache[K, V]) CAS(key K, old V, new V, cmp func(V, V) bool) bool {  		oldV = item.Value  		// Update value + expiry. -		item.Expiry = time.Now().Add(c.TTL) +		item.Expiry = c.expiry()  		item.Value = new  		// Set hook func ptr. @@ -396,7 +402,7 @@ func (c *Cache[K, V]) Swap(key K, swp V) V {  		oldV = item.Value  		// Update value + expiry. -		item.Expiry = time.Now().Add(c.TTL) +		item.Expiry = c.expiry()  		item.Value = swp  		// Set hook func ptr. @@ -603,14 +609,26 @@ func (c *Cache[K, V]) free(e *Entry[K, V]) {  	var (  		zk K  		zv V -		zt time.Time  	) -	e.Expiry = zt +	e.Expiry = 0  	e.Key = zk  	e.Value = zv  	c.pool = append(c.pool, e)  } +//go:linkname runtime_nanotime runtime.nanotime +func runtime_nanotime() uint64 + +// expiry returns an the next expiry time to use for an entry, +// which is equivalent to time.Now().Add(ttl), or zero if disabled. +func (c *Cache[K, V]) expiry() uint64 { +	if ttl := c.TTL; ttl > 0 { +		return runtime_nanotime() + +			uint64(c.TTL) +	} +	return 0 +} +  type kv[K comparable, V any] struct {  	K K  	V V | 
