34/37
Part 6 of 6 · Follow-upsLearn and do

When values vary in size, bound memory in bytes, not only in entries

capacity counts entries, but a cache of 1,000 small strings and a cache of 1,000 images are not the same size. When values vary, keep a running total of their sizes, and evict until the total fits a byte budget.

Count the structure too. At 100,000 entries the measured overhead per entry was 84 to 180 bytes before any value is stored. And the stale heap entries from the sweep add up if nothing rebuilds the heap.

In the editor, write evict_to_fit(sizes, budget): pop the least recently used entries until the total size fits budget, and return their keys. sizes maps each key to its size in bytes.