mirror of
https://github.com/xiaoqidun/ofdgo.git
synced 2026-10-29 17:27:08 +08:00
feat(内存占用): 优化数值解析与页面缓存,减少内存分配和重复处理
This commit is contained in:
1 parent
0fd4954547
commit
830378c6e4
2 files changed
+15
-19
No files matched your search
+3
-12
@@ -53,7 +53,6 @@ const state = {
|
||||
pageObserver: null,
|
||||
visiblePages: new Set(),
|
||||
scrollFrame: 0,
|
||||
thumbnailCache: new Map(),
|
||||
thumbnailInFlight: new Set(),
|
||||
thumbnailObserver: null,
|
||||
visibleThumbnails: new Set(),
|
||||
@@ -1549,7 +1548,6 @@ async function processPageRenderQueue() {
|
||||
const page = await callWASM("ofdgoRenderPage", task.index);
|
||||
if (task.openSeq === state.openSeq) {
|
||||
state.pageCache.set(task.index, page);
|
||||
cacheThumbnail(task.index, page.svg);
|
||||
}
|
||||
task.resolve(page);
|
||||
} catch (err) {
|
||||
@@ -1809,7 +1807,7 @@ function renderPageList() {
|
||||
const thumb = document.createElement("span");
|
||||
thumb.className = "thumb-paper";
|
||||
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");
|
||||
label.className = "thumb-label";
|
||||
@@ -1831,7 +1829,6 @@ function renderPageList() {
|
||||
}
|
||||
|
||||
function resetThumbnails() {
|
||||
state.thumbnailCache.clear();
|
||||
state.visibleThumbnails.clear();
|
||||
state.thumbnailInFlight.clear();
|
||||
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) {
|
||||
const renderKey = `${openSeq}:${index}`;
|
||||
if (svgText && container.dataset.renderKey === renderKey) {
|
||||
@@ -1923,7 +1914,7 @@ async function renderThumbnail(index, openSeq = state.openSeq) {
|
||||
if (openSeq !== state.openSeq || !state.doc) {
|
||||
return;
|
||||
}
|
||||
if (state.thumbnailCache.has(index)) {
|
||||
if (state.pageCache.has(index)) {
|
||||
updateThumbnail(index, openSeq);
|
||||
return;
|
||||
}
|
||||
@@ -1956,7 +1947,7 @@ function updateThumbnail(index, openSeq = state.openSeq) {
|
||||
}
|
||||
const thumb = el.pageList.querySelector(`[data-page-index="${index}"] .thumb-paper`);
|
||||
if (thumb) {
|
||||
setThumbnailContent(thumb, state.thumbnailCache.get(index), index, openSeq);
|
||||
setThumbnailContent(thumb, state.pageCache.get(index)?.svg, index, openSeq);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+12
-7
@@ -17,6 +17,7 @@ package ofdgo
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
@@ -30,17 +31,21 @@ type Box struct {
|
||||
// 入参: s 字符串
|
||||
// 返回: 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
|
||||
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)
|
||||
if err != nil {
|
||||
return Box{}, fmt.Errorf("invalid box: %s", s)
|
||||
}
|
||||
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
|
||||
}
|
||||
@@ -146,11 +151,10 @@ func parseFloats(s string) []float64 {
|
||||
// 入参: s 字符串
|
||||
// 返回: []float64 浮点数数组
|
||||
func parseFloatsWithG(s string) []float64 {
|
||||
parts := strings.Fields(s)
|
||||
var result []float64
|
||||
gFlag := false
|
||||
gCount := 0
|
||||
for _, p := range parts {
|
||||
for p := range strings.FieldsSeq(s) {
|
||||
if p == "g" {
|
||||
gFlag = true
|
||||
continue
|
||||
@@ -166,6 +170,7 @@ func parseFloatsWithG(s string) []float64 {
|
||||
}
|
||||
if gCount > 0 {
|
||||
v, _ := strconv.ParseFloat(p, 64)
|
||||
result = slices.Grow(result, gCount)
|
||||
for j := 0; j < gCount; j++ {
|
||||
result = append(result, v)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user