fix(memory): 图片字节冷读加 4 路并发闸门(v3.26.110)

Windows 实测:启动后 usedJSHeapSize 34MB → 440~557MB,而画板数据只有 0.5MB。
根因:首帧渲染时对 111 个图片引用同时发起读盘,每个 1~6MB 读成 Uint8Array 再 copy 进 Blob。
改为 getBlob 冷读 4 路并发(缓存命中不排队),堆峰值随并发走而不随画布图片数走;stats 增加读取并发观测。
This commit is contained in:
qiqi committed 2026-11-29 21:52:53 +08:00
1 parent 870508a33f
commit 004227f074
6 files changed
+37 -6

No files matched your search

+22 -2
View File
@@ -113,13 +113,33 @@
cacheSet(key, blob)
return { ok: true, key: key, deduped: false, bytes: blob.size }
}
// v3.26.110(内存):读盘并发闸门。首次渲染画布会同时解析几十~上百个引用,
// 每个 getBlob 都从磁盘读 1~6MB 成 Uint8Array(V8 堆),再 copy 进 Blob ⇒
// 111 张时实测 usedJSHeapSize 从 34MB 冲到 440~557MB(Windows 日志)。
// 这里把「冷读」串到 4 路并发:堆峰值随并发走,而不是随画布图片数走。
const READ_MAX = Math.max(1, Math.floor(Number(opts.readConcurrency) || 4))
let readActive = 0
const readWaiters = []
const withReadSlot = (fn) => new Promise((resolve, reject) => {
const run = () => {
readActive++
Promise.resolve().then(fn).then(resolve, reject).finally(() => {
readActive--
const nx = readWaiters.shift()
if (nx) nx()
})
}
if (readActive < READ_MAX) run()
else readWaiters.push(run)
})
async function getBlob(key) {
const k = isRef(key) ? keyOf(key) : String(key || '')
if (!k) return null
const hit = cacheGet(k)
if (hit !== undefined) return hit
try {
const v = await idb.get(k)
const v = await withReadSlot(() => idb.get(k))
const b = (v && v.ok === false) ? null : v
// 淘汰后回读 IDB 并重新入缓存(cacheSet 在禁用时为 no-op)⇒ 对外行为与未淘汰一致
if (b && typeof b.size === 'number') { cacheSet(k, b); return b }
@@ -154,7 +174,7 @@
}
// 可观测(新增字段,不改既有语义):Blob 缓存当前条目数 / 生效上限(禁用时为 0)。
// size() 仍是「Blob 缓存条目数」,与 blobCacheSize 同源。
function stats() { return { blobCacheSize: memBlobs.size, blobCacheMax: blobCacheMax } }
function stats() { return { blobCacheSize: memBlobs.size, blobCacheMax: blobCacheMax, readActive: readActive, readQueued: readWaiters.length, readMax: READ_MAX } }
return { put, getBlob, resolveDataUrl, resolveUrl, revokeAll, del, stats, refOf, isRef, keyOf, hashOf, PREFIX, size: () => memBlobs.size }
}
global.IWBImageBlobStore = Object.freeze({ PREFIX, refOf, isRef, keyOf, hashOf, dataUrlToBlob, blobToDataUrl, create })