perf(优化提升): 提升双端性能并优化内存占用
build.yaml / build (push) Successful in 46s

This commit is contained in:
xiaoqidun committed 2026-10-19 23:17:39 +08:00
1 parent eb20f66ffb
commit 075797402d
41 files changed
+633 -125

No files matched your search

+10 -3
View File
@@ -419,6 +419,7 @@ type outputContext struct {
// businessEngine 独立文档、编辑、剪贴板及操作上下文
type businessEngine struct {
resources *ofdgo.Resources
currentSession *Session
currentEditor *ofdgo.Editor
saveSnapshot *ofdgo.Editor
@@ -1396,7 +1397,7 @@ func (e *businessEngine) convertPDFDocument(args []platformValue) (any, error) {
return nil, err
}
var output bytes.Buffer
options := ofdgo.PDFImportOptions{OnProgress: e.editorOperationProgress(args[4]), PasswordUTF8: true}
options := ofdgo.PDFImportOptions{Resources: e.resources, OnProgress: e.editorOperationProgress(args[4]), PasswordUTF8: true}
if !args[2].IsNull() && !args[2].IsUndefined() {
options.Password, err = e.bytesFromJS(args[2])
if err != nil {
@@ -1450,7 +1451,8 @@ func (e *businessEngine) convertFile(args []platformValue) (any, error) {
return nil, err
}
options := ofdgo.ConvertOptions{
Format: settings.Get("format").String(), PageRange: settings.Get("pages").String(),
Resources: e.resources,
Format: settings.Get("format").String(), PageRange: settings.Get("pages").String(),
Pool: e.conversionPool,
SkipUnchanged: true,
Backends: &backends,
@@ -1608,7 +1610,7 @@ func (e *businessEngine) openDocument(args []platformValue) (any, error) {
return nil, err
}
renderAnnotations := args[2].Bool()
options := OpenOptions{Fonts: fonts, RenderAnnotations: renderAnnotations}
options := OpenOptions{Fonts: fonts, RenderAnnotations: renderAnnotations, ReaderOptions: []ofdgo.ReaderOption{ofdgo.WithResources(e.resources)}}
if len(args) > 3 && !args[3].IsNull() && !args[3].IsUndefined() {
credentials, err := e.credentialsFromJS(args[3])
if err != nil {
@@ -4047,6 +4049,7 @@ func (e *businessEngine) previewText(args []platformValue) (any, error) {
return nil, err
}
editor = ofdgo.NewEditor()
editor.SetResources(e.resources)
fontID, err = editor.AddFont(FontFile{Data: data}, 0)
if err != nil {
return nil, err
@@ -5568,6 +5571,7 @@ func (e *businessEngine) loadImport(args []platformValue) (any, error) {
}
}()
var options []ofdgo.ReaderOption
options = append(options, ofdgo.WithResources(e.resources))
if len(args) > 1 && !args[1].IsNull() && !args[1].IsUndefined() {
credentials, err := e.credentialsFromJS(args[1])
if err != nil {
@@ -5685,6 +5689,7 @@ func (e *businessEngine) importImages(args []platformValue) (any, error) {
editor := e.currentEditor
if at < 0 {
editor = ofdgo.NewEditor()
editor.SetResources(e.resources)
editor.Info.Title = args[3].String()
} else if editor == nil {
return nil, fmt.Errorf("document is not editable")
@@ -5721,6 +5726,7 @@ func (e *businessEngine) importImages(args []platformValue) (any, error) {
func (e *businessEngine) createDocument(args []platformValue) (any, error) {
e.clearImport()
editor := ofdgo.NewEditor()
editor.SetResources(e.resources)
editor.Info.Title = args[0].String()
if _, err := editor.AddPage(args[1].Float(), args[2].Float()); err != nil {
return nil, err
@@ -7124,6 +7130,7 @@ func (e *businessEngine) previewSignatureSeal(args []platformValue) (any, error)
pages = append(pages, map[string]any{"index": index, "x": box.X, "y": box.Y, "width": box.W, "height": box.H})
}
editor := ofdgo.NewEditor()
editor.SetResources(e.resources)
if _, err := editor.AddPage(seal.Width, seal.Height); err != nil {
return nil, err
}
+10
View File
@@ -70,6 +70,8 @@ type NativeOutputTarget struct {
// hooks应在首次Call前配置,Resolve返回的资源需保持有效直到相应会话关闭
type NativeEngine struct {
businessEngine
// Resources 协调会话的计算与缓存,需在首次Call前配置
Resources *ofdgo.Resources
// ConversionPool 协调不同会话的批量转换资源
ConversionPool *ofdgo.ConversionPool
// RenderParallelism 限制同时渲染的页面数,零值使用可用处理器数
@@ -152,6 +154,7 @@ func (e *NativeEngine) Call(ctx context.Context, name string, args []any, emit f
e.renderSlots = make(chan struct{}, e.renderParallelism())
e.resolveInput = e.input
e.conversionPool = e.ConversionPool
e.resources = e.Resources
})
if ctx == nil {
ctx = context.Background()
@@ -163,6 +166,13 @@ func (e *NativeEngine) Call(ctx context.Context, name string, args []any, emit f
case <-ctx.Done():
return nativeFailure(ctx.Err())
}
if e.resources != nil {
release, err := e.resources.Acquire(ctx, 64<<20)
if err != nil {
return nativeFailure(err)
}
defer release()
}
}
select {
case e.gate <- struct{}{}:
+2 -1
View File
@@ -39,7 +39,8 @@ type platformValue = js.Value
// RunWASM 注册浏览器WASM接口并阻塞运行
func RunWASM() {
e := &businessEngine{}
resources, _ := ofdgo.NewResources(ofdgo.ResourceOptions{Parallelism: 1, MemoryLimit: 64 << 20, CacheBytes: 48 << 20, SnapshotBytes: 32 << 20})
e := &businessEngine{resources: resources}
asynchronous := map[string]bool{
"ofdgoDetectFormat": true,
"ofdgoExportVersion": true,
+12 -3
View File
@@ -51,6 +51,7 @@ type Host struct {
engines map[string]*webui.NativeEngine
engineStops map[string]func() bool
conversionPool *ofdgo.ConversionPool
resources *ofdgo.Resources
cancels map[string]context.CancelFunc
batchResults map[string]any
fontOnce sync.Once
@@ -79,11 +80,16 @@ func Run(ctx context.Context, paths []string, dataDir string) error {
return err
}
h := &Host{files: files, engines: make(map[string]*webui.NativeEngine), cancels: make(map[string]context.CancelFunc), fontFiles: make(map[string]string), pendingPaths: append([]string(nil), paths...)}
h.conversionPool, err = ofdgo.NewConversionPool(ofdgo.ConversionPoolOptions{AvailableMemory: conversionMemory})
cacheBytes := int64(256 << 20)
if memory := conversionMemory; memory != nil {
cacheBytes = max(32<<20, memory()/8)
}
h.resources, err = ofdgo.NewResources(ofdgo.ResourceOptions{CacheBytes: cacheBytes, AvailableMemory: conversionMemory})
if err != nil {
files.close()
return err
}
h.conversionPool = h.resources.ConversionPool()
defer h.close()
bindings := make(map[string]guigo.HandlerFunc)
for _, name := range []string{"engine.open", "engine.close", "engine.call", "engine.start", "engine.cancel", "engine.finish", "batch", "open", "save", "directory", "file", "release", "child", "remove", "write.begin", "write.data", "write.commit", "write.abort", "fonts", "ready", "title"} {
@@ -271,11 +277,11 @@ func (h *Host) call(ctx context.Context, name string, value any) (any, error) {
parallelism, cacheBytes := runtime.GOMAXPROCS(0), int64(128<<20)
if memory := conversionMemory; memory != nil {
available := memory()
parallelism = max(1, min(parallelism, int(available/(256<<20))))
cacheBytes = max(32<<20, min(256<<20, available/16))
cacheBytes = max(32<<20, available/32)
}
engine.RenderParallelism = parallelism
engine.ConversionPool = h.conversionPool
engine.Resources = h.resources
engine.OpenWriter = h.openWriter
engine.ReserveOutput = h.reserveOutput
engine.CleanupOutput = h.cleanupOutput
@@ -798,6 +804,9 @@ func (h *Host) close() {
for _, result := range results {
h.discard(result)
}
if h.resources != nil {
h.resources.Clear()
}
_ = h.files.close()
}