diff --git a/frontend/cache.lua b/frontend/cache.lua index 76c053992..74bb7d69d 100644 --- a/frontend/cache.lua +++ b/frontend/cache.lua @@ -43,7 +43,7 @@ function Cache:init() self.cache = lru.new(self.slots, nil, self.enable_eviction_cb) else -- Compute the amount of slots in the LRU based on the max size & the average item size - self.slots = math.floor(self.size / self.avg_itemsize) + self.slots = math.ceil(self.size / self.avg_itemsize) self.cache = lru.new(self.slots, self.size, self.enable_eviction_cb) end diff --git a/frontend/document/doccache.lua b/frontend/document/doccache.lua index 27d0f1064..db2c0a9d2 100644 --- a/frontend/document/doccache.lua +++ b/frontend/document/doccache.lua @@ -11,14 +11,38 @@ local function calcCacheMemSize() local min = DGLOBAL_CACHE_SIZE_MINIMUM local max = DGLOBAL_CACHE_SIZE_MAXIMUM local calc = Cache:_calcFreeMem() * (DGLOBAL_CACHE_FREE_PROPORTION or 0) - local size = math.min(max, math.max(min, calc)) + return math.min(max, math.max(min, calc)) +end + +local function computeCacheSize() + local size = calcCacheMemSize() + local mb_size = size / 1024 / 1024 + + -- If we end up with a not entirely ridiculous cache size, use that... + if mb_size >= 8 then + logger.dbg(string.format("Allocating a %dMB budget for the global document cache", mb_size)) + return size + else + return nil + end +end + +local function computeCacheSlots() + local size = calcCacheMemSize() + local mb_size = size / 1024 / 1024 - logger.dbg(string.format("Allocating a %dMB budget for the global document cache", size / 1024 / 1024)) - return size + --- ...otherwise, effectively disable the cache by making it single slot... + if mb_size < 8 then + logger.dbg(string.format("Setting up a minimal single slot global document cache")) + return 1 + else + return nil + end end local DocCache = Cache:new{ - size = calcCacheMemSize(), + slots = computeCacheSlots(), + size = computeCacheSize(), -- Average item size is a screen's worth of bitmap, mixed with a few much smaller tables (pgdim, pglinks, etc.), hence the / 3 avg_itemsize = math.floor(CanvasContext:getWidth() * CanvasContext:getHeight() * (CanvasContext.is_color_rendering_enabled and 4 or 1) / 3), -- Rely on CacheItem's eviction callback to free resources *immediately* on eviction.