2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00
koreader/frontend/document/tilecacheitem.lua
NiLuJe 21b067792d Cache: Rewrite based on lua-lru
Ought to be faster than our naive array-based approach.
Especially for the glyph cache, which has a solid amount of elements,
and is mostly cache hits.
(There are few things worse for performance in Lua than
table.remove @ !tail and table.insert @ !tail, which this was full of :/).

DocCache: New module that's now an actual Cache instance instead of a
weird hack. Replaces "Cache" (the instance) as used across Document &
co.
Only Cache instance with on-disk persistence.

ImageCache: Update to new Cache.

GlyphCache: Update to new Cache.
Also, actually free glyph bbs on eviction.
2021-05-05 20:37:33 +02:00

75 lines
1.9 KiB
Lua

local Blitbuffer = require("ffi/blitbuffer")
local CacheItem = require("cacheitem")
local Persist = require("persist")
local logger = require("logger")
local TileCacheItem = CacheItem:new{}
function TileCacheItem:onFree()
logger.dbg("TileCacheItem: free blitbuffer", self.bb)
self.bb:free()
end
--- @note: Perhaps one day we'll be able to teach bitser or string.buffer about custom structs with pointers to buffers,
--- so we won't have to do the BB tostring/fromstring dance anymore...
function TileCacheItem:totable()
local t = {
size = self.size,
pageno = self.pageno,
excerpt = self.excerpt,
persistent = self.persistent,
bb = {
w = self.bb.w,
h = self.bb.h,
stride = tonumber(self.bb.stride),
fmt = self.bb:getType(),
data = Blitbuffer.tostring(self.bb),
},
}
return t
end
function TileCacheItem:dump(filename)
logger.dbg("Dumping tile cache to", filename, self.excerpt)
local cache_file = Persist:new{
path = filename,
codec = "zstd",
}
local ok, size = cache_file:save(self:totable())
if ok then
return size
else
logger.warn("Failed to dump tile cache")
return nil
end
end
function TileCacheItem:fromtable(t)
self.size = t.size
self.pageno = t.pageno
self.excerpt = t.excerpt
self.persistent = t.persistent
self.bb = Blitbuffer.fromstring(t.bb.w, t.bb.h, t.bb.fmt, t.bb.data, t.bb.stride)
end
function TileCacheItem:load(filename)
local cache_file = Persist:new{
path = filename,
codec = "zstd",
}
local t = cache_file:load(filename)
if t then
self:fromtable(t)
logger.dbg("Loaded tile cache from", filename, self)
else
logger.warn("Failed to load tile cache from", filename)
end
end
return TileCacheItem