From 061a0ea6dd3edd1b68158affebc7f8fc2183dbbc Mon Sep 17 00:00:00 2001 From: David Finkel Date: Mon, 9 Dec 2019 16:16:43 -0500 Subject: [PATCH] CacheType: convert to uint8 We only need ~2 bits for the types of cache. `int` is 64-bits on 64-bit platforms, which is significant overkill. Convert CacheType to a uint8 and move it to the end of the `cache` struct so it only affects alignment, but not the actual size. (we can pack in other small values at the end later as well) --- cachetype_string.go | 2 +- galaxycache.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cachetype_string.go b/cachetype_string.go index 7a93c371..abc5c2ee 100644 --- a/cachetype_string.go +++ b/cachetype_string.go @@ -19,7 +19,7 @@ var _CacheType_index = [...]uint8{0, 9, 17, 31} func (i CacheType) String() string { i -= 1 - if i < 0 || i >= CacheType(len(_CacheType_index)-1) { + if i >= CacheType(len(_CacheType_index)-1) { return "CacheType(" + strconv.FormatInt(int64(i+1), 10) + ")" } return _CacheType_name[_CacheType_index[i]:_CacheType_index[i+1]] diff --git a/galaxycache.go b/galaxycache.go index 3211a453..a0171146 100644 --- a/galaxycache.go +++ b/galaxycache.go @@ -592,7 +592,7 @@ func (g *Galaxy) populateCache(ctx context.Context, key string, value *valWithSt } // CacheType represents a type of cache. -type CacheType int +type CacheType uint8 const ( // MainCache is the cache for items that this peer is the @@ -627,12 +627,12 @@ func (g *Galaxy) CacheStats(which CacheType) CacheStats { // and counts the size of all keys and values. Candidate cache only // utilizes the lru.Cache and mutex, not the included stats. type cache struct { - ctype CacheType mu sync.Mutex nbytes int64 // of all keys and values lru *lru.Cache nhit, nget int64 nevict int64 // number of evictions + ctype CacheType } func (c *cache) stats() CacheStats {