feat(内存占用): 优化数值解析与页面缓存,减少内存分配和重复处理

This commit is contained in:
xiaoqidun committed 2026-09-12 13:47:59 +08:00
1 parent 0fd4954547
commit 830378c6e4
2 files changed
+15 -19

No files matched your search

+3 -12
View File
@@ -53,7 +53,6 @@ const state = {
pageObserver: null, pageObserver: null,
visiblePages: new Set(), visiblePages: new Set(),
scrollFrame: 0, scrollFrame: 0,
thumbnailCache: new Map(),
thumbnailInFlight: new Set(), thumbnailInFlight: new Set(),
thumbnailObserver: null, thumbnailObserver: null,
visibleThumbnails: new Set(), visibleThumbnails: new Set(),
@@ -1549,7 +1548,6 @@ async function processPageRenderQueue() {
const page = await callWASM("ofdgoRenderPage", task.index); const page = await callWASM("ofdgoRenderPage", task.index);
if (task.openSeq === state.openSeq) { if (task.openSeq === state.openSeq) {
state.pageCache.set(task.index, page); state.pageCache.set(task.index, page);
cacheThumbnail(task.index, page.svg);
} }
task.resolve(page); task.resolve(page);
} catch (err) { } catch (err) {
@@ -1809,7 +1807,7 @@ function renderPageList() {
const thumb = document.createElement("span"); const thumb = document.createElement("span");
thumb.className = "thumb-paper"; thumb.className = "thumb-paper";
thumb.setAttribute("aria-hidden", "true"); thumb.setAttribute("aria-hidden", "true");
setThumbnailContent(thumb, state.thumbnailCache.get(page.index), page.index, openSeq); setThumbnailContent(thumb, state.pageCache.get(page.index)?.svg, page.index, openSeq);
const label = document.createElement("span"); const label = document.createElement("span");
label.className = "thumb-label"; label.className = "thumb-label";
@@ -1831,7 +1829,6 @@ function renderPageList() {
} }
function resetThumbnails() { function resetThumbnails() {
state.thumbnailCache.clear();
state.visibleThumbnails.clear(); state.visibleThumbnails.clear();
state.thumbnailInFlight.clear(); state.thumbnailInFlight.clear();
if (state.thumbnailObserver) { if (state.thumbnailObserver) {
@@ -1840,12 +1837,6 @@ function resetThumbnails() {
} }
} }
function cacheThumbnail(index, svgText) {
if (typeof svgText === "string" && svgText) {
state.thumbnailCache.set(index, svgText);
}
}
function setThumbnailContent(container, svgText, index, openSeq = state.openSeq) { function setThumbnailContent(container, svgText, index, openSeq = state.openSeq) {
const renderKey = `${openSeq}:${index}`; const renderKey = `${openSeq}:${index}`;
if (svgText && container.dataset.renderKey === renderKey) { if (svgText && container.dataset.renderKey === renderKey) {
@@ -1923,7 +1914,7 @@ async function renderThumbnail(index, openSeq = state.openSeq) {
if (openSeq !== state.openSeq || !state.doc) { if (openSeq !== state.openSeq || !state.doc) {
return; return;
} }
if (state.thumbnailCache.has(index)) { if (state.pageCache.has(index)) {
updateThumbnail(index, openSeq); updateThumbnail(index, openSeq);
return; return;
} }
@@ -1956,7 +1947,7 @@ function updateThumbnail(index, openSeq = state.openSeq) {
} }
const thumb = el.pageList.querySelector(`[data-page-index="${index}"] .thumb-paper`); const thumb = el.pageList.querySelector(`[data-page-index="${index}"] .thumb-paper`);
if (thumb) { if (thumb) {
setThumbnailContent(thumb, state.thumbnailCache.get(index), index, openSeq); setThumbnailContent(thumb, state.pageCache.get(index)?.svg, index, openSeq);
} }
} }
+12 -7
View File
@@ -17,6 +17,7 @@ package ofdgo
import ( import (
"fmt" "fmt"
"math" "math"
"slices"
"strconv" "strconv"
"strings" "strings"
) )
@@ -30,17 +31,21 @@ type Box struct {
// 入参: s 字符串 // 入参: s 字符串
// 返回: Box 矩形对象, error 错误信息 // 返回: Box 矩形对象, error 错误信息
func ParseBox(s string) (Box, error) { func ParseBox(s string) (Box, error) {
parts := strings.Fields(s)
if len(parts) != 4 {
return Box{}, fmt.Errorf("invalid box: %s", s)
}
var values [4]float64 var values [4]float64
for i, part := range parts { i := 0
for part := range strings.FieldsSeq(s) {
if i == len(values) {
return Box{}, fmt.Errorf("invalid box: %s", s)
}
value, err := strconv.ParseFloat(part, 64) value, err := strconv.ParseFloat(part, 64)
if err != nil { if err != nil {
return Box{}, fmt.Errorf("invalid box: %s", s) return Box{}, fmt.Errorf("invalid box: %s", s)
} }
values[i] = value values[i] = value
i++
}
if i != len(values) {
return Box{}, fmt.Errorf("invalid box: %s", s)
} }
return Box{X: values[0], Y: values[1], W: values[2], H: values[3]}, nil return Box{X: values[0], Y: values[1], W: values[2], H: values[3]}, nil
} }
@@ -146,11 +151,10 @@ func parseFloats(s string) []float64 {
// 入参: s 字符串 // 入参: s 字符串
// 返回: []float64 浮点数数组 // 返回: []float64 浮点数数组
func parseFloatsWithG(s string) []float64 { func parseFloatsWithG(s string) []float64 {
parts := strings.Fields(s)
var result []float64 var result []float64
gFlag := false gFlag := false
gCount := 0 gCount := 0
for _, p := range parts { for p := range strings.FieldsSeq(s) {
if p == "g" { if p == "g" {
gFlag = true gFlag = true
continue continue
@@ -166,6 +170,7 @@ func parseFloatsWithG(s string) []float64 {
} }
if gCount > 0 { if gCount > 0 {
v, _ := strconv.ParseFloat(p, 64) v, _ := strconv.ParseFloat(p, 64)
result = slices.Grow(result, gCount)
for j := 0; j < gCount; j++ { for j := 0; j < gCount; j++ {
result = append(result, v) result = append(result, v)
} }