fix(memory): 缩略图缓存不再收原图;超大数据视为缺失(v3.26.112)

111 实测 ready-2s 51MB(占位生效),但 30s 涨到 304MB 且 imgDataN=111 / imgDataMB=260MB:
生成失败/降级路径 remember(key, 原图dataURL) 把原图当缩略图缓存,画布于是拿 2.3MB base64 当图显示。

- image-display-thumb:>512KB dataURL 一律不入缩略图缓存(四处 remember 加保护),并记录 thumb-fail 证据
- image-thumb-pick:缓存里是超大 dataURL 时视为缺失 → 占位 + 重新排队
- 内存构成新增 imgBigN / imgMaxKB
This commit is contained in:
qiqi committed 2026-11-29 22:30:53 +08:00
1 parent 60cce3ccb1
commit 54f140bb9b
8 files changed
+40 -10

No files matched your search

+19 -4
View File
@@ -36,6 +36,21 @@
cache.set(key, val)
}
const bump = () => { try { if (typeof o.bump === 'function') o.bump() } catch (e) { /* ignore */ } }
// v3.26.112:大于 512KB 的 dataURL **绝不能**当缩略图缓存。
// 生成失败/降级路径原来会 remember(key, 原图 dataURL),画布随后把它当缩略图渲染,
// 结果就是每个 <img src> 都是 2MB 级 base64(Windows 实测 111 张 / 260MB)。
const tooBig = (x) => typeof x === 'string' && x.indexOf('data:') === 0 && x.length > 512 * 1024
// v3.26.112:缩略图生成失败的**原因**要留证据(每会话最多 20 条),否则只能看到"没有缩略图"。
let failLogs = 0
const noteFail = (why, key, v) => {
try {
if (failLogs >= 20) return
failLogs++
const S = global.IWBStorageFile
if (!S || typeof S.logLine !== 'function') return
S.logLine('thumb-fail ' + why + ' key=' + String(key || '').slice(0, 40) + ' src=' + String(v || '').slice(0, 32) + ' len=' + (typeof v === 'string' ? v.length : -1))
} catch (e) { /* ignore */ }
}
const save = (key, payload) => { try { if (typeof o.save === 'function') o.save(key, payload) } catch (e) { /* ignore */ } }
// ① 优先路径:字节 → bitmap → 缩放 → WebP Blob → objectURL
@@ -51,7 +66,7 @@
try {
if (!iw || !ih) return false
if (iw <= max && ih <= max) {
remember(key, v)
if (!tooBig(v)) remember(key, v)
return true
}
const scale = Math.min(max / Math.max(1, iw), max / Math.max(1, ih))
@@ -85,7 +100,7 @@
try {
const iw = img.naturalWidth || 0
const ih = img.naturalHeight || 0
if (iw <= max && ih <= max) { remember(key, v); resolve(true); return }
if (iw <= max && ih <= max) { if (!tooBig(v)) remember(key, v); resolve(true); return }
const scale = Math.min(max / Math.max(1, iw), max / Math.max(1, ih))
const cw = Math.max(1, Math.round(iw * scale))
const ch = Math.max(1, Math.round(ih * scale))
@@ -99,10 +114,10 @@
remember(key, small)
save(key, small)
resolve(true)
} catch (e) { remember(key, v); resolve(false) }
} catch (e) { noteFail('canvas', key, v); if (!tooBig(v)) remember(key, v); resolve(false) }
}
img.onerror = () => {
try { if (!(typeof o.isTransient === 'function' && o.isTransient(v))) remember(key, v) } catch (e) { /* ignore */ }
try { noteFail('img-error', key, v); if (!tooBig(v) && !(typeof o.isTransient === 'function' && o.isTransient(v))) remember(key, v) } catch (e) { /* ignore */ }
resolve(false)
}
img.src = v
+3 -1
View File
@@ -39,7 +39,9 @@
const ORIG_HARD_CAP = Math.max(1200, max * 2)
function srcForNode (v, renderedLongEdge, scale) {
const key = keyOf(v)
const thumb = key && cache ? cache.get(key) : ''
const raw = key && cache ? cache.get(key) : ''
// v3.26.112:缓存里可能是「被当缩略图存下来的原图 dataURL」(>512KB)——不可用,按缺失处理。
const thumb = (typeof raw === 'string' && raw.indexOf('data:') === 0 && raw.length > 512 * 1024) ? '' : raw
const dpr = (typeof global.devicePixelRatio === 'number' && global.devicePixelRatio > 0) ? global.devicePixelRatio : 1
const s = Number(scale)
const need = Math.max(1, Number(renderedLongEdge) || 0) * (Number.isFinite(s) && s > 0 ? s : 1) * dpr