Repository navigation
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
Setafter many deletes no longer stalls.Deleteleaves 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.Deletestays 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
Setafter deleting 60% of a million entries: about 50 ms before, about 3 µs now. - Delete-and-insert churn at a million entries: the slowest
Setdrops from about 150 ms to about 100 ms. What remains is one growth of the log's buffer. The averageSetis 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,Setreplacing 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,LoadFromFileandLoadFromFileOrNewbuild the cache withNew, so a cache made byNewWeightedloads back without its weight bound. One made withmath.MaxIntasmaxEntries, the documented way to bound a cache by weight alone, loads back with no bound at all. The docs forNewWeighted, the three load functions and the package, and the README, now say so and give the way to restore one: make a new cache withNewWeightedand the same arguments, then pass itsSettoLoadFromFunc. The newExampleLoadFromFunc_weightedrestores 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
GobEncodeorMarshalBinarymethod, such astime.Time. The struct inherits the method, so gob encodes only the embedded value.SaveToandLoadFromboth 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.SaveTonow describes this case and its fixes: give the embedded field a name, or, for values, convert them withSaveToFunc.SaveToFile,SaveToFileConcurrentandSaveToFuncpoint to it, andSaveToFuncadds 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 ownGobEncodeand save correctly. (#74, closes #77) -
The README's allocation claims are scoped. The README said
Getdoesn't allocate, which holds only for string keys. Hashing other key types boxes the key, so a struct key allocates once on everyGet, and so does anintkey of 256 or more. EverySetallocates the entry it stores, not only aSetthat 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 fromGet/256up: some of thoseGetcalls 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, sopushFrontis unchanged andpopstill inlines. When evictions have taken every slot a pass kept,popdrops the holes and ends the pass, and the next insert starts a new one if dead slots still outnumber live ones. (#72) TestCacheCompactsOrderLogInSmallStepschecks that an insert after mass deletes doesn't rewrite the whole log.TestSlotRingKeepsFIFOOrdernow 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