KOPTInterface: Minor optimization when hashing the configurable status

Use a table & table.concat instead of individual concats.
And then use that same table for every hash-related operation.

(Nothing else uses the configurable hash function, otherwise I'd have
limited the table shenanigans to the function itself).
reviewable/pr7658/r1
NiLuJe 3 years ago
parent 48c474e245
commit 1ffbd8760d

@ -18,15 +18,13 @@ function Configurable:reset()
end end
end end
function Configurable:hash(sep) function Configurable:hash(list)
local hash = ""
for key, value in ffiUtil.orderedPairs(self) do for key, value in ffiUtil.orderedPairs(self) do
local value_type = type(value) local value_type = type(value)
if value_type == "number" or value_type == "string" then if value_type == "number" or value_type == "string" then
hash = hash..sep..value table.insert(list, value)
end end
end end
return hash
end end
function Configurable:loadDefaults(config_options) function Configurable:loadDefaults(config_options)

@ -150,12 +150,18 @@ function KoptInterface:createContext(doc, pageno, bbox)
return kc return kc
end end
function KoptInterface:getContextHash(doc, pageno, bbox) function KoptInterface:getContextHash(doc, pageno, bbox, hash_list)
local canvas_size = CanvasContext:getSize() local canvas_size = CanvasContext:getSize()
local canvas_size_hash = canvas_size.w.."|"..canvas_size.h table.insert(hash_list, doc.file)
local bbox_hash = bbox.x0.."|"..bbox.y0.."|"..bbox.x1.."|"..bbox.y1 table.insert(hash_list, doc.mod_time)
return doc.file.."|"..doc.mod_time.."|"..pageno.."|" table.insert(hash_list, pageno)
..doc.configurable:hash("|").."|"..bbox_hash.."|"..canvas_size_hash doc.configurable:hash(hash_list)
table.insert(hash_list, bbox.x0)
table.insert(hash_list, bbox.y0)
table.insert(hash_list, bbox.x1)
table.insert(hash_list, bbox.y1)
table.insert(hash_list, canvas_size.w)
table.insert(hash_list, canvas_size.h)
end end
function KoptInterface:getPageBBox(doc, pageno) function KoptInterface:getPageBBox(doc, pageno)
@ -182,8 +188,9 @@ function KoptInterface:getAutoBBox(doc, pageno)
x1 = native_size.w, x1 = native_size.w,
y1 = native_size.h, y1 = native_size.h,
} }
local context_hash = self:getContextHash(doc, pageno, bbox) local hash_list = { "autobbox" }
local hash = "autobbox|"..context_hash self:getContextHash(doc, pageno, bbox, hash_list)
local hash = table.concat(hash_list, "|")
local cached = DocCache:check(hash) local cached = DocCache:check(hash)
if not cached then if not cached then
local page = doc._document:openPage(pageno) local page = doc._document:openPage(pageno)
@ -211,8 +218,9 @@ Detect bbox within user restricted bbox.
function KoptInterface:getSemiAutoBBox(doc, pageno) function KoptInterface:getSemiAutoBBox(doc, pageno)
-- use manual bbox -- use manual bbox
local bbox = Document.getPageBBox(doc, pageno) local bbox = Document.getPageBBox(doc, pageno)
local context_hash = self:getContextHash(doc, pageno, bbox) local hash_list = { "semiautobbox" }
local hash = "semiautobbox|"..context_hash self:getContextHash(doc, pageno, bbox, hash_list)
local hash = table.concat(hash_list, "|")
local cached = DocCache:check(hash) local cached = DocCache:check(hash)
if not cached then if not cached then
local page = doc._document:openPage(pageno) local page = doc._document:openPage(pageno)
@ -247,9 +255,10 @@ immediately, or wait for the background thread with reflowed context.
--]] --]]
function KoptInterface:getCachedContext(doc, pageno) function KoptInterface:getCachedContext(doc, pageno)
local bbox = doc:getPageBBox(pageno) local bbox = doc:getPageBBox(pageno)
local context_hash = self:getContextHash(doc, pageno, bbox) local hash_list = { "kctx" }
local kctx_hash = "kctx|"..context_hash self:getContextHash(doc, pageno, bbox, hash_list)
local cached = DocCache:check(kctx_hash, ContextCacheItem) local hash = table.concat(hash_list, "|")
local cached = DocCache:check(hash, ContextCacheItem)
if not cached then if not cached then
-- If kctx is not cached, create one and get reflowed bmp in foreground. -- If kctx is not cached, create one and get reflowed bmp in foreground.
local kc = self:createContext(doc, pageno, bbox) local kc = self:createContext(doc, pageno, bbox)
@ -265,7 +274,7 @@ function KoptInterface:getCachedContext(doc, pageno)
local fullwidth, fullheight = kc:getPageDim() local fullwidth, fullheight = kc:getPageDim()
logger.dbg("reflowed page", pageno, "fullwidth:", fullwidth, "fullheight:", fullheight) logger.dbg("reflowed page", pageno, "fullwidth:", fullwidth, "fullheight:", fullheight)
self.last_context_size = fullwidth * fullheight + 3072 -- estimation self.last_context_size = fullwidth * fullheight + 3072 -- estimation
DocCache:insert(kctx_hash, ContextCacheItem:new{ DocCache:insert(hash, ContextCacheItem:new{
persistent = true, persistent = true,
size = self.last_context_size, size = self.last_context_size,
kctx = kc kctx = kc
@ -331,10 +340,11 @@ Inherited from common document interface.
function KoptInterface:renderReflowedPage(doc, pageno, rect, zoom, rotation, render_mode) function KoptInterface:renderReflowedPage(doc, pageno, rect, zoom, rotation, render_mode)
doc.render_mode = render_mode doc.render_mode = render_mode
local bbox = doc:getPageBBox(pageno) local bbox = doc:getPageBBox(pageno)
local context_hash = self:getContextHash(doc, pageno, bbox) local hash_list = { "renderpg" }
local renderpg_hash = "renderpg|"..context_hash self:getContextHash(doc, pageno, bbox, hash_list)
local hash = table.concat(hash_list, "|")
local cached = DocCache:check(renderpg_hash) local cached = DocCache:check(hash)
if not cached then if not cached then
-- do the real reflowing if kctx has not been cached yet -- do the real reflowing if kctx has not been cached yet
local kc = self:getCachedContext(doc, pageno) local kc = self:getCachedContext(doc, pageno)
@ -350,7 +360,7 @@ function KoptInterface:renderReflowedPage(doc, pageno, rect, zoom, rotation, ren
} }
tile.bb = kc:dstToBlitBuffer() tile.bb = kc:dstToBlitBuffer()
tile.size = tonumber(tile.bb.stride) * tile.bb.h + 512 -- estimation tile.size = tonumber(tile.bb.stride) * tile.bb.h + 512 -- estimation
DocCache:insert(renderpg_hash, tile) DocCache:insert(hash, tile)
return tile return tile
else else
return cached return cached
@ -365,10 +375,11 @@ Inherited from common document interface.
function KoptInterface:renderOptimizedPage(doc, pageno, rect, zoom, rotation, render_mode) function KoptInterface:renderOptimizedPage(doc, pageno, rect, zoom, rotation, render_mode)
doc.render_mode = render_mode doc.render_mode = render_mode
local bbox = doc:getPageBBox(pageno) local bbox = doc:getPageBBox(pageno)
local context_hash = self:getContextHash(doc, pageno, bbox) local hash_list = { "renderoptpg" }
local renderpg_hash = "renderoptpg|"..context_hash..zoom self:getContextHash(doc, pageno, bbox, hash_list)
local hash = table.concat(hash_list, "|")
local cached = DocCache:check(renderpg_hash, TileCacheItem) local cached = DocCache:check(hash, TileCacheItem)
if not cached then if not cached then
local page_size = Document.getNativePageDimensions(doc, pageno) local page_size = Document.getNativePageDimensions(doc, pageno)
local full_page_bbox = { local full_page_bbox = {
@ -397,7 +408,7 @@ function KoptInterface:renderOptimizedPage(doc, pageno, rect, zoom, rotation, re
tile.bb = kc:dstToBlitBuffer() tile.bb = kc:dstToBlitBuffer()
tile.size = tonumber(tile.bb.stride) * tile.bb.h + 512 -- estimation tile.size = tonumber(tile.bb.stride) * tile.bb.h + 512 -- estimation
kc:free() kc:free()
DocCache:insert(renderpg_hash, tile) DocCache:insert(hash, tile)
return tile return tile
else else
return cached return cached
@ -425,9 +436,10 @@ Inherited from common document interface.
--]] --]]
function KoptInterface:hintReflowedPage(doc, pageno, zoom, rotation, gamma, render_mode) function KoptInterface:hintReflowedPage(doc, pageno, zoom, rotation, gamma, render_mode)
local bbox = doc:getPageBBox(pageno) local bbox = doc:getPageBBox(pageno)
local context_hash = self:getContextHash(doc, pageno, bbox) local hash_list = { "kctx" }
local kctx_hash = "kctx|"..context_hash self:getContextHash(doc, pageno, bbox, hash_list)
local cached = DocCache:check(kctx_hash) local hash = table.concat(hash_list, "|")
local cached = DocCache:check(hash)
if not cached then if not cached then
local kc = self:createContext(doc, pageno, bbox) local kc = self:createContext(doc, pageno, bbox)
local page = doc._document:openPage(pageno) local page = doc._document:openPage(pageno)
@ -436,7 +448,7 @@ function KoptInterface:hintReflowedPage(doc, pageno, zoom, rotation, gamma, rend
kc:setPreCache() kc:setPreCache()
page:reflow(kc, 0) page:reflow(kc, 0)
page:close() page:close()
DocCache:insert(kctx_hash, ContextCacheItem:new{ DocCache:insert(hash, ContextCacheItem:new{
size = self.last_context_size or self.default_context_size, size = self.last_context_size or self.default_context_size,
kctx = kc, kctx = kc,
}) })
@ -492,11 +504,12 @@ Get text boxes in reflowed page via rectmaps in koptcontext.
--]] --]]
function KoptInterface:getReflowedTextBoxes(doc, pageno) function KoptInterface:getReflowedTextBoxes(doc, pageno)
local bbox = doc:getPageBBox(pageno) local bbox = doc:getPageBBox(pageno)
local context_hash = self:getContextHash(doc, pageno, bbox) local hash_list = { "rfpgboxes" }
local hash = "rfpgboxes|"..context_hash self:getContextHash(doc, pageno, bbox, hash_list)
local hash = table.concat(hash_list, "|")
local cached = DocCache:check(hash) local cached = DocCache:check(hash)
if not cached then if not cached then
local kctx_hash = "kctx|"..context_hash local kctx_hash = hash:gsub("^rfpgboxes|", "kctx|")
cached = DocCache:check(kctx_hash) cached = DocCache:check(kctx_hash)
if cached then if cached then
local kc = self:waitForContext(cached.kctx) local kc = self:waitForContext(cached.kctx)
@ -516,11 +529,12 @@ Get text boxes in native page via rectmaps in koptcontext.
--]] --]]
function KoptInterface:getNativeTextBoxes(doc, pageno) function KoptInterface:getNativeTextBoxes(doc, pageno)
local bbox = doc:getPageBBox(pageno) local bbox = doc:getPageBBox(pageno)
local context_hash = self:getContextHash(doc, pageno, bbox) local hash_list = { "nativepgboxes" }
local hash = "nativepgboxes|"..context_hash self:getContextHash(doc, pageno, bbox, hash_list)
local hash = table.concat(hash_list, "|")
local cached = DocCache:check(hash) local cached = DocCache:check(hash)
if not cached then if not cached then
local kctx_hash = "kctx|"..context_hash local kctx_hash = hash:gsub("^nativepgboxes|", "kctx|")
cached = DocCache:check(kctx_hash) cached = DocCache:check(kctx_hash)
if cached then if cached then
local kc = self:waitForContext(cached.kctx) local kc = self:waitForContext(cached.kctx)
@ -542,11 +556,12 @@ Done by OCR pre-processing in Tesseract and Leptonica.
--]] --]]
function KoptInterface:getReflowedTextBoxesFromScratch(doc, pageno) function KoptInterface:getReflowedTextBoxesFromScratch(doc, pageno)
local bbox = doc:getPageBBox(pageno) local bbox = doc:getPageBBox(pageno)
local context_hash = self:getContextHash(doc, pageno, bbox) local hash_list = { "scratchrfpgboxes" }
local hash = "scratchrfpgboxes|"..context_hash self:getContextHash(doc, pageno, bbox, hash_list)
local hash = table.concat(hash_list, "|")
local cached = DocCache:check(hash) local cached = DocCache:check(hash)
if not cached then if not cached then
local kctx_hash = "kctx|"..context_hash local kctx_hash = hash:gsub("^scratchrfpgboxes|", "kctx|")
cached = DocCache:check(kctx_hash) cached = DocCache:check(kctx_hash)
if cached then if cached then
local reflowed_kc = self:waitForContext(cached.kctx) local reflowed_kc = self:waitForContext(cached.kctx)
@ -615,8 +630,9 @@ Get page regions in native page via optical method.
function KoptInterface:getPageBlock(doc, pageno, x, y) function KoptInterface:getPageBlock(doc, pageno, x, y)
local kctx local kctx
local bbox = doc:getPageBBox(pageno) local bbox = doc:getPageBBox(pageno)
local context_hash = self:getContextHash(doc, pageno, bbox) local hash_list = { "pageblocks" }
local hash = "pageblocks|"..context_hash self:getContextHash(doc, pageno, bbox, hash_list)
local hash = table.concat(hash_list, "|")
local cached = DocCache:check(hash) local cached = DocCache:check(hash)
if not cached then if not cached then
local page_size = Document.getNativePageDimensions(doc, pageno) local page_size = Document.getNativePageDimensions(doc, pageno)
@ -660,11 +676,16 @@ Get word from OCR in reflew page.
function KoptInterface:getReflewOCRWord(doc, pageno, rect) function KoptInterface:getReflewOCRWord(doc, pageno, rect)
self.ocr_lang = doc.configurable.doc_language self.ocr_lang = doc.configurable.doc_language
local bbox = doc:getPageBBox(pageno) local bbox = doc:getPageBBox(pageno)
local context_hash = self:getContextHash(doc, pageno, bbox) local hash_list = { "rfocrword" }
local hash = "rfocrword|"..context_hash..rect.x..rect.y..rect.w..rect.h self:getContextHash(doc, pageno, bbox, hash_list)
table.insert(hash_list, rect.x)
table.insert(hash_list, rect.y)
table.insert(hash_list, rect.w)
table.insert(hash_list, rect.h)
local hash = table.concat(hash_list, "|")
local cached = DocCache:check(hash) local cached = DocCache:check(hash)
if not cached then if not cached then
local kctx_hash = "kctx|"..context_hash local kctx_hash = hash:gsub("^rfocrword|", "kctx|")
cached = DocCache:check(kctx_hash) cached = DocCache:check(kctx_hash)
if cached then if cached then
local kc = self:waitForContext(cached.kctx) local kc = self:waitForContext(cached.kctx)

@ -7,7 +7,7 @@ local lfs = require("libs/libkoreader-lfs")
local logger = require("logger") local logger = require("logger")
-- Date at which the last migration snippet was added -- Date at which the last migration snippet was added
local CURRENT_MIGRATION_DATE = 20210503 local CURRENT_MIGRATION_DATE = 20210508
-- Retrieve the date of the previous migration, if any -- Retrieve the date of the previous migration, if any
local last_migration_date = G_reader_settings:readSetting("last_migration_date", 0) local last_migration_date = G_reader_settings:readSetting("last_migration_date", 0)
@ -209,9 +209,10 @@ if last_migration_date < 20210414 then
end end
end end
-- DocCache, migration to Persist, https://github.com/koreader/koreader/pull/7624 -- 20210503: DocCache, migration to Persist, https://github.com/koreader/koreader/pull/7624
if last_migration_date < 20210503 then -- 20210508: DocCache, KOPTInterface hash fix, https://github.com/koreader/koreader/pull/7634
logger.info("Performing one-time migration for 20210503") if last_migration_date < 20210508 then
logger.info("Performing one-time migration for 20210503 & 20210508")
local DocCache = require("document/doccache") local DocCache = require("document/doccache")
DocCache:clearDiskCache() DocCache:clearDiskCache()

Loading…
Cancel
Save