From 3bbf1ea85d222e44d00a7562abf18512aeb45440 Mon Sep 17 00:00:00 2001 From: qiqi Date: Tue, 17 Nov 2026 15:36:20 +0800 Subject: [PATCH] =?UTF-8?q?perf(image):=20=E5=9B=BE=E5=BA=93=E6=B8=85?= =?UTF-8?q?=E5=8D=95=E5=8E=BB=20base64=EF=BC=88367KB=E2=86=921.6KB?= =?UTF-8?q?=EF=BC=8C=E6=8C=89=20thumbHash=20=E8=AF=BB=E6=96=87=E4=BB=B6?= =?UTF-8?q?=EF=BC=89+=20=E7=94=BB=E5=B8=83=E6=98=BE=E7=A4=BA=E7=BC=A9?= =?UTF-8?q?=E7=95=A5=E5=9B=BE=20768px=20=E8=90=BD=E7=9B=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app.js | 47 ++++++++++++++++++++- src/gallery-hydration.js | 9 +++- src/storage-file.js | 89 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 140 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index 7790be4..39a6247 100644 --- a/app.js +++ b/app.js @@ -1654,7 +1654,7 @@ const RuntimeConstants = (typeof window !== 'undefined' && window.IWBRuntimeConstants) || { outputLimit: 10000, galleryRanges: {} } const GalleryHydration = typeof window !== 'undefined' && window.IWBGalleryHydration const galleryHydrator = GalleryHydration && typeof GalleryHydration.create === 'function' - ? GalleryHydration.create({ get: (key) => IDB.get(key) }) + ? GalleryHydration.create({ get: (key) => IDB.get(key), thumb: (hash) => { const F = (typeof window !== 'undefined') ? window.IWBStorageFile : null; return (F && typeof F.galleryThumb === 'function') ? F.galleryThumb(hash) : Promise.resolve('') } }) : null const ModularOutputStore = typeof window !== 'undefined' && window.IWBStorageOutputs && ModularLocalStore && ModularIndexedStore ? window.IWBStorageOutputs.create({ local: ModularLocalStore, indexedDb: ModularIndexedStore, key: OUT_KEY, prefix: 'out_', limit: RuntimeConstants.outputLimit, hydrate: galleryHydrator && ((items) => galleryHydrator.hydrate(items)) }) @@ -4643,6 +4643,48 @@ const tbCenterSeed = (type, refs) => { previewUrls.clear(); previewPending.clear() } + // 显示缩略图落盘(768px):下次启动直接读文件,不用再解码原图 + const DISPLAY_THUMB_HASH_MAX = 512 + const displayThumbSaved = new Set() + function wStoreMod () { + const F = (typeof window !== 'undefined') ? window.IWBStorageFile : null + return (F && typeof F.readDisplayThumb === 'function') ? F : null + } + function wSaveDisplayThumb (key, dataUrl) { + const F = wStoreMod() + if (!F || !key || !dataUrl) return + if (displayThumbSaved.has(key) || displayThumbSaved.size > 2000) return + displayThumbSaved.add(key) + wSha256(String(key)).then(function (hex) { + try { F.writeDisplayThumb(hex.slice(0, 16), dataUrl) } catch (e) { /* ignore */ } + }).catch(function () { /* ignore */ }) + } + async function wPreloadDisplayThumbs (nodes) { + const F = wStoreMod() + if (!F || !Array.isArray(nodes) || !nodes.length) return 0 + const keys = [] + for (const n of nodes) { + const d = n && n.data + if (!d) continue + const v = wResolveImg(d.data) || d.url + if (!v || typeof v !== 'string' || v.indexOf('data:') === 0) continue + const key = wDisplayThumbKey(v) + if (!key || displayThumbs.has(key) || keys.indexOf(key) >= 0) continue + keys.push(key) + if (keys.length >= DISPLAY_THUMB_HASH_MAX) break + } + let n = 0 + for (const key of keys) { + try { + const hex = await wSha256(String(key)) + const dataUrl = await F.readDisplayThumb(hex.slice(0, 16)) + if (dataUrl) { displayThumbs.set(key, dataUrl); displayThumbSaved.add(key); n++ } + } catch (e) { /* 单张失败忽略 */ } + } + if (n && typeof wThumbBump === 'function') { try { wThumbBump() } catch (e) { /* ignore */ } } + return n + } + function wDisplayThumbOf(v) { // C3:结果项 / 图片节点的 data 可能是引用对象(hydrate 直出)→ 先解析成显示 URL if (v && typeof v === 'object') v = wResolveImg(v) @@ -4679,6 +4721,7 @@ const tbCenterSeed = (type, refs) => { const small = cv.toDataURL('image/jpeg', 0.78) if (displayThumbs.size >= DISPLAY_THUMB_CACHE_MAX) { const first = displayThumbs.keys().next().value; if (first !== undefined) displayThumbs.delete(first) } wRememberThumb(small) + try { wSaveDisplayThumb(key, small) } catch (e) { /* ignore */ } displayThumbPending.delete(key) if (typeof wThumbBump === 'function') { try { wThumbBump() } catch (e) { /* ignore */ } } } catch (e) { displayThumbPending.delete(key); wRememberThumb(v) } // 失败也记下:不再每次渲染重复解码整图 @@ -6173,6 +6216,8 @@ const tbCenterSeed = (type, refs) => { refs.syncing = true // 切换/同步载入不触发自动保存(避免轮询-保存死循环) setSel([]); setSelEdge(null) openSession(id) // S3:先激活 session,再从它加载历史(session 化) + // 显示缩略图预热(768px 文件已存在就直接命中,省掉一次 4K 解码) + try { setTimeout(function () { wPreloadDisplayThumbs(ns).catch(function () { /* ignore */ }) }, 0) } catch (e) { /* ignore */ } const sess = sessionsStore.get(id) if (sess) { sess.nodes = ns; sess.edges = es; sess.groups = gs; sess.dirty = false } // S6:装载即同步 session 并清脏(装载不算本地编辑) refs.history = (sess && sess.history) ? sess.history : { stack: [], idx: -1 } // S3:历史随 session(不再全局清空) diff --git a/src/gallery-hydration.js b/src/gallery-hydration.js index 43448d3..7fb34aa 100644 --- a/src/gallery-hydration.js +++ b/src/gallery-hydration.js @@ -4,6 +4,8 @@ function createHydrator(options) { const opt = options || {} const get = typeof opt.get === 'function' ? opt.get : (() => Promise.resolve(null)) + // 文件存储:清单只留 thumbHash,缩略图按需从 images/thumbs/ 读 + const thumb = typeof opt.thumb === 'function' ? opt.thumb : null let sequence = 0 let disposed = false async function hydrate(items, requestId) { @@ -11,7 +13,12 @@ if (id > sequence) sequence = id const list = Array.isArray(items) ? items : [] const values = await Promise.all(list.map(async (item) => { - if (!item || !item.idb) return item + if (!item) return item + if (item.data) return item + if (thumb && item.thumbHash) { + try { const v = await thumb(item.thumbHash); if (v) return Object.assign({}, item, { data: v }) } catch (e) { /* 读文件失败退回 idb */ } + } + if (!item.idb) return item try { const value = await get(item.idb) return value == null ? item : Object.assign({}, item, { data: value }) diff --git a/src/storage-file.js b/src/storage-file.js index 2bce871..cc9f3b6 100644 --- a/src/storage-file.js +++ b/src/storage-file.js @@ -324,8 +324,11 @@ try { const u8 = await fs().readFile(await join(dir, a.f)) if (!u8 || u8.length > 96 * 1024) continue - it.data = bytesToDataUrl(a.m || 'image/jpeg', u8) - it.thumbHash = String(a.f).split('/').pop().replace(/\.[^.]+$/, '') + const hash = String(a.f).split('/').pop().replace(/\.[^.]+$/, '') + const thumbPath = await join(dir, IMG_DIR, 'thumbs', hash + '.jpg') + try { if (!(await fs().exists(thumbPath))) await fs().writeFile(thumbPath, u8) } catch (e) { /* 写失败不阻塞 */ } + it.thumbHash = hash + it.data = '' n++ } catch (e) { /* 单张失败忽略 */ } } @@ -642,6 +645,8 @@ try { await migrateBlobs() } catch (e) { /* 迁移失败不阻塞启动 */ } // 图库清单补齐缩略图(一次性) try { await backfillGalleryThumbs() } catch (e) { /* 忽略 */ } + // 图库清单去 base64(一次性):缩略图已在 images/thumbs/,清单只留 thumbHash + try { await stripInlineGalleryThumbs() } catch (e) { /* 忽略 */ } // trash/ 轮转:gcImages 只是移进来,超过 30 天在这里真正删除 try { await rotateTrash(30) } catch (e) { /* 忽略 */ } // 自愈:图库清单丢了但 assets 还有 out_ 键 ⇒ 按键重建(名称/时间/idb 可推,label/远程 url 无法恢复) @@ -723,6 +728,84 @@ } catch (e) { return 0 } } + // 图库缩略图:清单里只存 thumbHash,渲染时按需从 images/thumbs/ 读(避免 base64 撑爆 index.json) + let thumbMapCache = null + async function thumbFileMap() { + if (thumbMapCache) return thumbMapCache + const map = {} + try { + const abs = await join(dir, IMG_DIR, 'thumbs') + const entries = await fs().readDir(abs) + for (const e of entries) { + if (!e || e.isDirectory) continue + const base = String(e.name).replace(/\.[^.]+$/, '') + if (!map[base]) map[base] = String(e.name) + } + } catch (e) { /* 没有 thumbs 目录就返回空表 */ } + thumbMapCache = map + return map + } + async function galleryThumb(hash) { + try { + if (!enabled() || !dir || !hash) return '' + const map = await thumbFileMap() + const name = map[String(hash)] + if (!name) return '' + const u8 = await fs().readFile(await join(dir, IMG_DIR, 'thumbs', name)) + if (!u8 || !u8.length) return '' + const mime = /\.png$/i.test(name) ? 'image/png' : 'image/jpeg' + return bytesToDataUrl(mime, u8) + } catch (e) { return '' } + } + + // 画布显示缩略图(768px)落盘:重启后直接命中,不用重新解码 4K 原图 + async function readDisplayThumb(hash) { + try { + if (!enabled() || !dir || !hash) return '' + const abs = await join(dir, IMG_DIR, 'thumbs', String(hash) + '_768.jpg') + if (!(await fs().exists(abs))) return '' + const u8 = await fs().readFile(abs) + if (!u8 || !u8.length) return '' + return bytesToDataUrl('image/jpeg', u8) + } catch (e) { return '' } + } + async function writeDisplayThumb(hash, dataUrl) { + try { + if (!enabled() || !dir || !hash || !dataUrl) return false + const m = /^data:[^;]+;base64,(.*)$/.exec(String(dataUrl)) + if (!m) return false + const bin = atob(m[1]) + const u8 = new Uint8Array(bin.length) + for (let i = 0; i < bin.length; i++) u8[i] = bin.charCodeAt(i) + const abs = await join(dir, IMG_DIR, 'thumbs', String(hash) + '_768.jpg') + if (await fs().exists(abs)) return true + await fs().mkdir(await join(dir, IMG_DIR, 'thumbs'), { recursive: true }) + await fs().writeFile(abs, u8) + return true + } catch (e) { return false } + } + + // 一次性:图库清单里历史遗留的 base64 data 删掉(缩略图已在 images/thumbs/) + async function stripInlineGalleryThumbs() { + try { + const marker = await join(dir, 'gallery-data-stripped.flag') + if (await fs().exists(marker)) return 0 + const raw = mem['iwb_web_outputs'] + if (!raw) { await fs().writeTextFile(marker, '0'); return 0 } + const items = JSON.parse(raw) + if (!Array.isArray(items)) return 0 + const map = await thumbFileMap() + let n = 0 + for (const it of items) { + if (!it || !it.data) continue + if (it.thumbHash && map[String(it.thumbHash)]) { it.data = ''; n++ } + } + if (n) { mem['iwb_web_outputs'] = JSON.stringify(items); await flush() } + await fs().writeTextFile(marker, String(n)) + return n + } catch (e) { return 0 } + } + async function gcImages(opts) { const dryRun = !!(opts && opts.dryRun) try { @@ -758,7 +841,7 @@ } global.IWBStorageFile = { - enabled, ready: init, flush, runtime, pickDir, switchTo, migrateTo, gcImages, rotateTrash, + enabled, ready: init, flush, runtime, pickDir, switchTo, migrateTo, gcImages, rotateTrash, galleryThumb, readDisplayThumb, writeDisplayThumb, plugins: { manifest: pluginManifest, read: pluginRead, write: pluginWrite, remove: pluginRemove, dir: pluginDir, listLocal: pluginListLocal }, storage: () => storage, kv: () => kv,