Skip to content

v0.4.0

Choose a tag to compare

@dwisiswant0 dwisiswant0 released this 14 Nov 20:18
· 12 commits to master since this release
c9fdde8

This release fixes several FIFO eviction and persistence bugs, cuts memory use, and makes Get lock-free. The exported API is unchanged.

Warning

Behavior changes:

  • Saving rejects types it can't fully encode. SaveTo, SaveToFile and SaveToFileConcurrent now return an error, before writing anything, when K or V contains fields that gob drops: unexported struct fields, or chan and func fields. Before this release these values saved without an error and came back with those fields zeroed or nil. The error names the field and its path. To keep saving such a type, export its fields or implement gob.GobEncoder or encoding.BinaryMarshaler. The error isn't exported, so errors.Is can't match it. (#39, fixes #30)
  • Known gap: a struct that embeds a type that encodes itself, such as time.Time, still loses its other fields without an error.

Fixed

  • Set, GetOrSet and SetIfAbsent no longer return ErrEvictionFailed on a full cache when a concurrent Delete or GetAndDelete removes the entries being evicted. (#52, fixes #46)
  • A key that is deleted and then set again is no longer evicted ahead of older keys. (#35, fixes #28)
  • A cache restored with LoadFrom, LoadFromFile or LoadFromFileOrNew now evicts in the original insertion order. Snapshots are written oldest entry first. (#38, fixes #29)
  • SaveToFile and SaveToFileConcurrent flush the temporary file to disk before renaming it over the target. After a crash the target holds either the old snapshot or the complete new one, never a partial file. (#40, fixes #32)
  • Repeated Set and Delete calls on a cache below capacity no longer grow memory without bound. Two million Set/Delete pairs used to retain about 95 MiB. (#36, fixes #27)
  • New no longer allocates for maxEntries up front. Storage grows as entries are stored, so maxEntries works purely as a cap. An empty New(1_000_000) used to take about 108 MB. (#37, fixes #31)
  • Reset now releases the memory of the eviction queue, as documented. (#48, fixes #44)

Performance

  • Lock-free Get. Get no longer takes a shard lock or writes shared memory, and its hit and miss counters are spread across CPUs. Concurrent Gets of one key on 18 CPUs take 2.3 ns each instead of 112 ns. Inserting into a full cache is 44% faster. (#50, closes #45)
  • Eviction no longer copies the queue. The eviction queue is now a ring buffer. Set on a full cache of 4M entries takes about 240 ns instead of 1.7 µs. (#49, fixes #43)

Trade-offs

  • Updating an existing key now allocates (48 B for a string key and value) and takes 11–20 ns longer, which is about twice as long for short keys. (#50)
  • A Get miss takes about 5 ns longer. (#50)
  • Filling an empty cache is 7–23% slower, with occasional pauses while storage grows, since New no longer pays for it up front. (#37)
  • Saving 500k small entries takes about 1.85× as long, because entries are sorted into insertion order. (#38)
  • SaveToFile and SaveToFileConcurrent take longer because of the added disk flush. (#40)

Documentation

  • Fixed out-of-date mentions of snappy, map[K]V, ring buffers and "lock-free" atomic operations. The docs now say which operations serialize on which lock. (#42, closes #34)
  • The README benchmarks were rerun on an Apple M5 Pro with GOMAXPROCS=4. The previous numbers came from an AMD EPYC 7763, so the two sets can't be compared directly. (#50)

Internal

  • Removed an error channel in save that nothing ever sent on. (#41, closes #33)

Full changelog: v0.3.1...v0.4.0