#pragma once #include #include #include namespace ks { inline constexpr size_t HistoryThumbnailBudget = 1024 * 1024; inline constexpr size_t HistoryRichTextBudget = 256 * 1024; // Keep the visible working set even in unusually tall windows. Never evict a // visible preview to admit another visible preview: that creates a paint loop. template void trimHistoryPreviews(std::map &cache, const std::set &visible, size_t limit, size_t byteLimit, Weight weight) { size_t bytes = 0; for (const auto &[id, value] : cache) bytes += weight(value); for(auto it=cache.begin(); (cache.size()>limit || bytes>byteLimit) && it!=cache.end();) if(!visible.contains(it->first)) { bytes -= weight(it->second); it=cache.erase(it); } else ++it; } template void trimHistoryPreviews(std::map &cache, const std::set &visible, size_t limit=32) { trimHistoryPreviews(cache, visible, limit, 0, [](const T &) { return size_t{}; }); } }