Skip to content

v0.6.0

Choose a tag to compare

@dwisiswant0 dwisiswant0 released this 16 Nov 14:24
· 4 commits to master since this release
3623559

This release adds NewWeighted, which bounds a cache by the total weight of its entries, such as the bytes of their values, as well as by entry count. Caches made by New behave as before, but each entry now stores a weight, so some of their writes are slightly slower (see Performance). The file format is unchanged: weights aren't saved, and v0.6.0 reads and writes the same data as v0.5.1.

// At most 1,000 entries whose values add up to at most 64 MiB.
cache, err := fastcache.NewWeighted(1000, 64<<20, func(_ string, v []byte) int {
	return len(v)
})

Added

  • NewWeighted(maxEntries, maxWeight, weigh) returns a cache that holds at most maxEntries entries whose weights add up to at most maxWeight. You supply weigh, a func(K, V) int, such as one that returns the length of the value. (#71, closes #70)
    • When a new or heavier value doesn't fit, the oldest entries are evicted until it does. A Set that makes a value heavier evicts only other entries, so its key stays in the cache and keeps its place in eviction order.
    • Each entry keeps the weight it was stored with, so the total can't drift if data that a value points to changes later.
    • To bound a cache by weight alone, pass math.MaxInt as maxEntries. Storage is allocated as entries are stored, not up front.
  • ErrInvalidWeight is returned by Set, GetOrSet and SetIfAbsent in a weighted cache when a value's weight is negative or greater than maxWeight. The cache is left unchanged. GetOrSet and SetIfAbsent check the weight only when the key is absent. (#71)
  • ErrInvalidMaxWeight is returned by NewWeighted when maxWeight isn't positive. NewWeighted also returns an error when weigh is nil, and the same errors as New for an invalid maxEntries. (#71)

Saving and loading weighted caches

  • Weighted caches save like any other cache. Their weights aren't saved.
  • LoadFrom, LoadFromFile and LoadFromFileOrNew return caches bounded only by entry count. To load saved entries into a weighted cache, pass its Set to LoadFromFunc, which stops with ErrInvalidWeight at the first saved value heavier than maxWeight. (#71)

Performance

Each entry now stores its weight, which makes it 8 bytes larger, in caches made by New too. Measured against v0.5.1 on an Apple M5 Pro:

  • Get: 1.5% slower (about 0.15 ns), with no change in allocations.
  • Updates of [string, []byte], the type the README benchmarks: 3% slower, with no change in memory.
  • Small types such as [string, string] and [int, int]: the extra 8 bytes move each entry up an allocation size class. Updates take 8% to 10% longer and allocate 16 B more, and each stored entry takes 16 B more memory. Inserts into a full [int, int] cache take 9% longer.
  • Benchmark suite (GOMAXPROCS=4): the geomean is within 0.05% of v0.5.1's (48.52 ns against 48.50 ns), and the fork is still 2.4× faster than the original fastcache and otter. Set is 3% to 8% slower from 16 to 1,024 keys, SetGet 1% to 8% slower up to 2,048 keys, and SetParallel 5% to 6% slower at 1 and 16 keys. From 4,096 keys up there is no measurable change.

Trade-offs

  • In a weighted cache, an update that makes a value heavier locks the cache-wide FIFO queue, as adding a key does, because it may have to evict. Updates that add no weight still lock only the key's shard. (#71)
  • The benchmark suite measures only caches made by New, not NewWeighted.

Documentation

  • The README lists weighted capacity among the features and in the comparison with the original fastcache, and no longer lists "No size-based limits" under Limitations. (#71)
  • The README's benchmark table was rerun on this release with the same settings. Its geomean now reads 48.5 ns against 116.2 ns for otter and 118.4 ns for the original. The changed GetParallel ranges reflect run-to-run spread, not the new code. (#71)

Internal

  • Each entry records its weight, and the cache keeps a running total that inserts, updates, deletes, evictions and Reset adjust. Eviction can skip the entry being updated and return it to the front of the queue. (#71)

Full changelog: v0.5.1...v0.6.0