Skip to content

v0.5.0

Choose a tag to compare

@dwisiswant0 dwisiswant0 released this 15 Nov 09:27
· 9 commits to master since this release
f0865f8

This release adds iterators that walk the cache in eviction order, and functions that select and convert entries when saving and loading. LoadFromFileOrNew now creates the cache with the capacity you pass it. The file format is unchanged: data saved by v0.4.0 loads in v0.5.0, and data saved by v0.5.0 loads in v0.4.0.

Warning

Breaking change:

  • LoadFromFileOrNew uses the maxEntries you pass, not the saved capacity. Before this release it used maxEntries only when the file failed to load, and otherwise returned a cache with the capacity it was saved with. Saving writes that capacity back, so a program that changed its cache size kept the old one on every run until the file was deleted. Now the cache always has maxEntries capacity. When the file holds more entries than that, the cache keeps the newest and counts the others as evictions in Stats. A missing or corrupted file still gives an empty cache. (#59, fixes #55)
  • A maxEntries that isn't positive is now an error even when the file loads. (#59)
  • To keep the saved capacity, call LoadFromFile and fall back to New(maxEntries) when it returns an error. LoadFromFile and LoadFrom still restore the saved capacity.

Added

  • AllFIFO, KeysFIFO and ValuesFIFO iterate oldest first, which is the order in which the cache evicts. Updating a key keeps its place; deleting a key and setting it again makes it the newest. All, Keys and Values keep their faster, unordered iteration. (#57, closes #54)
  • SaveToFunc saves the entries that a function selects, as the values it returns, oldest first. Use it to save part of a cache, or to save values that gob can't encode, such as structs with unexported fields, by converting them to a type it can. The gob check applies to the returned type instead of V. (#58, closes #56)
  • LoadFromFunc passes each saved entry, oldest first, to a function, such as the Set method of a cache you already have. It ignores the saved capacity and returns the first error that the function returns. It reads what SaveTo, SaveToFile and SaveToFunc write, and LoadFrom reads what SaveToFunc writes. (#58, closes #56)

Performance

  • SaveTo is 4.8% faster on 500k entries, and SaveToFileConcurrent with 8 workers is 3.9% faster, because the oldest-first sort moves smaller references. (#57)
  • Get, Set, Delete, All, Keys and Values run the same code as in v0.4.0, and their benchmarks show no change.

Trade-offs

  • AllFIFO, KeysFIFO and ValuesFIFO copy and sort every entry before they yield the first one, and hold memory in proportion to Len until the loop ends. On 500k entries a full loop takes about 8× as long as with All, and a loop that stops after 10 entries about 17,000× as long. (#57)
  • SaveToFunc keeps the values it saves in memory until it writes them, because the format puts the entry count first. (#58)
  • LoadFromFileOrNew with a maxEntries smaller than the file's entry count still decodes every entry, then evicts the extras. (#59)

Internal

  • Saving and the FIFO iterators share one oldest-first snapshot. Its workers take every n-th shard instead of reading shard numbers from a channel. (#57)
  • All save and load functions share one encoder and one decoder. (#58)
  • LoadFrom no longer adds the entry's index to an error from inserting it. (#58)

Full changelog: v0.4.0...v0.5.0