From 493ec396fab32d9e8859e34ad497fdb0a910a33c Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 17:49:11 -0700 Subject: lint: Turn on forcetypeassert --- lib/containers/lru.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/containers/lru.go') diff --git a/lib/containers/lru.go b/lib/containers/lru.go index a235a12..12446b0 100644 --- a/lib/containers/lru.go +++ b/lib/containers/lru.go @@ -1,4 +1,4 @@ -// Copyright (C) 2022 Luke Shumaker +// Copyright (C) 2022-2023 Luke Shumaker // // SPDX-License-Identifier: GPL-2.0-or-later @@ -44,6 +44,7 @@ func (c *LRUCache[K, V]) Get(key K) (value V, ok bool) { c.init() _value, ok := c.inner.Get(key) if ok { + //nolint:forcetypeassert // Typed wrapper around untyped lib. value = _value.(V) } return value, ok @@ -53,6 +54,7 @@ func (c *LRUCache[K, V]) Keys() []K { untyped := c.inner.Keys() typed := make([]K, len(untyped)) for i := range untyped { + //nolint:forcetypeassert // Typed wrapper around untyped lib. typed[i] = untyped[i].(K) } return typed @@ -65,6 +67,7 @@ func (c *LRUCache[K, V]) Peek(key K) (value V, ok bool) { c.init() _value, ok := c.inner.Peek(key) if ok { + //nolint:forcetypeassert // Typed wrapper around untyped lib. value = _value.(V) } return value, ok -- cgit v1.2.3-2-g168b From a06a7fb2d5bbf1ca5659de06fc9e975666bdcf9f Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sun, 1 Jan 2023 19:33:03 -0700 Subject: lint: Turn on gofumpt All edits to .go files are made by `tools/bin/golangci-lint run --fix ./...`, not by me as a human. --- lib/containers/lru.go | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'lib/containers/lru.go') diff --git a/lib/containers/lru.go b/lib/containers/lru.go index 12446b0..bfda361 100644 --- a/lib/containers/lru.go +++ b/lib/containers/lru.go @@ -36,10 +36,12 @@ func (c *LRUCache[K, V]) Add(key K, value V) { c.init() c.inner.Add(key, value) } + func (c *LRUCache[K, V]) Contains(key K) bool { c.init() return c.inner.Contains(key) } + func (c *LRUCache[K, V]) Get(key K) (value V, ok bool) { c.init() _value, ok := c.inner.Get(key) @@ -49,6 +51,7 @@ func (c *LRUCache[K, V]) Get(key K) (value V, ok bool) { } return value, ok } + func (c *LRUCache[K, V]) Keys() []K { c.init() untyped := c.inner.Keys() @@ -59,10 +62,12 @@ func (c *LRUCache[K, V]) Keys() []K { } return typed } + func (c *LRUCache[K, V]) Len() int { c.init() return c.inner.Len() } + func (c *LRUCache[K, V]) Peek(key K) (value V, ok bool) { c.init() _value, ok := c.inner.Peek(key) @@ -72,10 +77,12 @@ func (c *LRUCache[K, V]) Peek(key K) (value V, ok bool) { } return value, ok } + func (c *LRUCache[K, V]) Purge() { c.init() c.inner.Purge() } + func (c *LRUCache[K, V]) Remove(key K) { c.init() c.inner.Remove(key) -- cgit v1.2.3-2-g168b From b261c2a4f5f028c9d83cef208ccc7d829f841bad Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 31 Dec 2022 11:59:09 -0700 Subject: tree-wide: Annotate values that I might want to be tuning --- lib/containers/lru.go | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) (limited to 'lib/containers/lru.go') diff --git a/lib/containers/lru.go b/lib/containers/lru.go index bfda361..aa372ed 100644 --- a/lib/containers/lru.go +++ b/lib/containers/lru.go @@ -5,45 +5,30 @@ package containers import ( - "sync" - lru "github.com/hashicorp/golang-lru" ) // LRUCache is a least-recently-used(ish) cache. A zero LRUCache is -// usable and has a cache size of 128 items; use NewLRUCache to set a -// different size. +// not usable; it must be initialized with NewLRUCache. type LRUCache[K comparable, V any] struct { - initOnce sync.Once - inner *lru.ARCCache + inner *lru.ARCCache } func NewLRUCache[K comparable, V any](size int) *LRUCache[K, V] { c := new(LRUCache[K, V]) - c.initOnce.Do(func() { - c.inner, _ = lru.NewARC(size) - }) + c.inner, _ = lru.NewARC(size) return c } -func (c *LRUCache[K, V]) init() { - c.initOnce.Do(func() { - c.inner, _ = lru.NewARC(128) - }) -} - func (c *LRUCache[K, V]) Add(key K, value V) { - c.init() c.inner.Add(key, value) } func (c *LRUCache[K, V]) Contains(key K) bool { - c.init() return c.inner.Contains(key) } func (c *LRUCache[K, V]) Get(key K) (value V, ok bool) { - c.init() _value, ok := c.inner.Get(key) if ok { //nolint:forcetypeassert // Typed wrapper around untyped lib. @@ -53,7 +38,6 @@ func (c *LRUCache[K, V]) Get(key K) (value V, ok bool) { } func (c *LRUCache[K, V]) Keys() []K { - c.init() untyped := c.inner.Keys() typed := make([]K, len(untyped)) for i := range untyped { @@ -64,12 +48,10 @@ func (c *LRUCache[K, V]) Keys() []K { } func (c *LRUCache[K, V]) Len() int { - c.init() return c.inner.Len() } func (c *LRUCache[K, V]) Peek(key K) (value V, ok bool) { - c.init() _value, ok := c.inner.Peek(key) if ok { //nolint:forcetypeassert // Typed wrapper around untyped lib. @@ -79,12 +61,10 @@ func (c *LRUCache[K, V]) Peek(key K) (value V, ok bool) { } func (c *LRUCache[K, V]) Purge() { - c.init() c.inner.Purge() } func (c *LRUCache[K, V]) Remove(key K) { - c.init() c.inner.Remove(key) } -- cgit v1.2.3-2-g168b