Skip to content

v0.6.1

Latest

Choose a tag to compare

@dwisiswant0 dwisiswant0 released this 17 Nov 19:23
995bea4

This release removes a pause of tens of milliseconds from the first insert after many deletes. It also corrects three docs: how to restore a weighted cache, a case where saving silently drops struct fields, and the README's allocation claims. The exported API and the file format are unchanged: file.go changed only in doc comments, so v0.6.1 reads and writes the same data as v0.6.0. The fix makes inserts into a full cache slower on the CPU it was measured on (see Performance).

Fixed

  • The first Set after many deletes no longer stalls. Delete leaves its slot in the cache's FIFO order log. Once dead slots outnumbered live entries by more than 1,024, the next insert checked every slot in the log while holding the cache-wide queue lock. At a million entries that insert took tens of milliseconds, and every other insert of a new key waited behind it. Compaction now runs a few slots per insert, so the time an insert spends on the log no longer depends on the cache size. Delete stays O(1) amortized, and eviction order is unchanged. (#72, fixes #76)

Performance

Measured against v0.6.0 on a 16-vCPU AMD EPYC 7763 with Go 1.27.4:

  • First Set after deleting 60% of a million entries: about 50 ms before, about 3 µs now.
  • Delete-and-insert churn at a million entries: the slowest Set drops from about 150 ms to about 100 ms. What remains is one growth of the log's buffer. The average Set is about 15% slower.
  • Inserts into a full cache with no deletes: about 16% slower at a million entries and about 4% at 64K, although no compaction runs in that workload. Each changed line applied alone to v0.6.0 costs nothing, so this looks like a microarchitectural effect rather than added work. The exact cause hasn't been found.
  • Memory: at a million entries under churn, the log is longer while a pass runs, which costs one more growth step. Its peak goes from 2.10M to 2.40M slots, and the heap from 158 MiB to 171 MiB.
  • Everything else: the other 15 benchmarks in the same comparison, including Get, Delete, Set replacing a value and loading, showed no change.

Trade-offs

  • A workload that never deletes pays the full-cache insert cost and gets nothing from the fix.
  • The slowdown was measured only on that Zen 3 machine. Laptop runs on Apple Silicon were too noisy to confirm or rule it out.

Documentation

  • Restoring a weighted cache. LoadFrom, LoadFromFile and LoadFromFileOrNew build the cache with New, so a cache made by NewWeighted loads back without its weight bound. One made with math.MaxInt as maxEntries, the documented way to bound a cache by weight alone, loads back with no bound at all. The docs for NewWeighted, the three load functions and the package, and the README, now say so and give the way to restore one: make a new cache with NewWeighted and the same arguments, then pass its Set to LoadFromFunc. The new ExampleLoadFromFunc_weighted restores a weighted cache from a file and shows that it still evicts. (#73)

    // LoadFromFile would drop the weight bound.
    cache, err := fastcache.NewWeighted(math.MaxInt, 64<<20, weigh)
    if err != nil {
    	return err
    }
    
    f, err := os.Open(path)
    if err != nil {
    	return err
    }
    defer f.Close()
    
    if err := fastcache.LoadFromFunc(f, cache.Set); err != nil {
    	cache.Reset() // Drop the entries loaded before the error.
    	return err
    }
  • Saving can't detect fields dropped from a struct that embeds a type with a GobEncode or MarshalBinary method, such as time.Time. The struct inherits the method, so gob encodes only the embedded value. SaveTo and LoadFrom both return nil, and the struct's other fields load back as zero values. In a key, entries whose keys differed only in those fields load back as one entry. The save docs promised an error for any field gob would drop. SaveTo now describes this case and its fixes: give the embedded field a name, or, for values, convert them with SaveToFunc. SaveToFile, SaveToFileConcurrent and SaveToFunc point to it, and SaveToFunc adds that neither K nor S should embed such a type. The check itself can't be fixed: reflection can't tell an inherited method from one the struct declares, so it would also reject structs that declare their own GobEncode and save correctly. (#74, closes #77)

  • The README's allocation claims are scoped. The README said Get doesn't allocate, which holds only for string keys. Hashing other key types boxes the key, so a struct key allocates once on every Get, and so does an int key of 256 or more. Every Set allocates the entry it stores, not only a Set that replaces a value. The features list, the comparison table and the benchmark highlights now say so. The table's cell for the original fastcache now reads "Get with nil dst: 1 per hit. Set: 0". The highlights also explain why its allocs/op shows 0 from Get/256 up: some of those Get calls miss, and the average, below 1, is truncated to an integer. (#75)

Internal

  • The order log is compacted in place, at most compactStep (8) slots per insert. The ring counts a pass's holes and unchecked slots from the newest end, so pushFront is unchanged and pop still inlines. When evictions have taken every slot a pass kept, pop drops the holes and ends the pass, and the next insert starts a new one if dead slots still outnumber live ones. (#72)
  • TestCacheCompactsOrderLogInSmallSteps checks that an insert after mass deletes doesn't rewrite the whole log. TestSlotRingKeepsFIFOOrder now runs compaction passes between pushes and pops, and the churn test bounds how much the log can grow while a pass runs. (#72)

Full changelog: v0.6.0...v0.6.1