Skip to content

perf: 加速原生模板导出并保留未变化文件 - #55

Merged
AliceJump merged 7 commits into
mainfrom
perf/export-speed-20261203
Dec 3, 2026
Merged

AliceJump merged 7 commits into
mainfrom
perf/export-speed-20261203

Conversation

@AliceJump

@AliceJump AliceJump commented Dec 3, 2026 •

Copy link
Copy Markdown
Owner

问题

原生模板导出已与框架输出统一,但 Split 的宿主解码与 Python 生成串行执行,重复导出仍完整解码并替换未变化 PNG;Packed 还会重复解码预检过的图片。

改动

  • 用完整内容 SHA-256 缓存已完整验证的图片尺寸,只保留最多 1024 条元数据,不缓存截图像素。重复非 box 名称仍读取并比较真实 RGB。
  • Split 在安全标签与源身份/hash 预检后并行进行宿主校验和原生生成;失败或取消时等待 renderer 退出再清暂存,发布末尾不增加异步窗口。
  • 精确同名、字节相同的 Split PNG 保留在原位置;最终回调后及混合发布后仍完整复核,保持回滚、枚举、大小写改名和过期文件处理。
  • Packed 在单次 Python 请求内以 128 MiB 上限复用已预检像素;重新核对完整内容、flags 和 decoder 路径,每项只消费一次。原生分页、PNG 编码参数和 JSON 保持原调用。
  • 双语行为说明、性能报告和脱敏逐轮数据记录测量范围、缓存内存取舍与仍有的编码瓶颈。

实机结果

Windows 11、i7-12700H / 15.7 GiB,Node 22.15.1、项目 Python 3.12.14 / ok-script 3.0.14。真实编译后宿主 → 默认 renderer → 项目 Python;只替代 IDE 配置/本地化外壳,计时包含完整导出服务,不包含界面操作、输入加载和基准本身的哈希。首轮是新宿主进程,文件系统缓存已被输入完整性读取预热。

96 次未插桩 ABBA(旧、新、新、旧)全部成功;每种数据/格式各版本有 2 个首轮和 6 个重复样本。同一案例的全部 PNG/JSON 摘要一致,源文件未变。

数据 / 格式 旧版首轮 → 候选(秒) 旧版重复 → 候选(秒) 重复耗时减少
GF Split 0.535 → 0.406 0.524 → 0.383 26.8%
GF Packed 0.668 → 0.647 0.639 → 0.486 24.0%
AP Split 5.277 → 3.751 5.280 → 2.304 56.4%
AP Packed 3.597 → 3.188 3.551 → 3.010 15.2%
EF Split 3.333 → 2.312 3.606 → 1.976 45.2%
EF Packed 4.025 → 3.981 3.950 → 2.732 30.8%

AP 为 41 张引用中的 1080p 原始截图 / 59 框;EF 为 20 张已合图页 / 292 框,GF 为 3 页 / 13 框。EF Packed 首轮仅约 1% 差值,不能认定明显改善。额外 16 次单框编辑测试也保持新旧输出相同:AP 状态切换 5.238 → 2.318 秒,EF 3.766 → 2.025 秒;候选每轮保留 58 / 291 个内容未变 PNG 的原 inode。AP 使用真实 UI setter,EF 嵌套源路径通过公开内存模型仅修改 bbox/area。

128/256 MiB 插桩预算比较显示:提高预算额外省 AP 约 504 ms / EF 约 116 ms,但 Python 工作集峰值分别再增约 115.4 / 79.3 MiB,故保留 128 MiB。这只限制缓存像素,不是进程内存上限。

JetBrains 实机补测

正式插件 JAR → 真实服务/桥接 → 同一项目 Python,显式选择对应 helper,仅 IDE 外壳使用替身。JDK 23.0.1 / Kotlin 2.2.0 / Jackson 2.20.2,两版本一致;它不是完整 IDE UI 或默认解释器发现测试。AP/EF × Split/Packed × 旧/新各使用独立 JVM,首轮 + 3 次重复,共 32 次成功,源未变,所有输出逐文件 SHA 相同。

数据 / 格式 旧版重复 → 候选(秒) 重复耗时减少
AP Split 5.483 → 2.085 62.0%
EF Split 3.162 → 1.914 39.5%
AP Packed 3.204 → 2.734 14.6%
EF Packed 3.259 → 2.536 22.2%

这是按旧→新运行的小样本补测,未做 ABBA。EF Split 首轮单样本为 3.163 → 3.355 秒(增加 6.1%),不能泛称首轮全面提速。生产要求与正式 CI 仍为 JDK 21。

验证

  • Windows 实机 TypeScript compile、图片解码/缓存测试、Split 67 项及原生 Python 47 项(0 skip)通过。
  • 独立交叉审阅完成;覆盖取消、并发修改、同 size/mtime 改写、缓存边界和原生字节对照。
  • 初始候选 a7ac0cc 的 六项 CI 全绿;最终 HEAD 6bb9416eb10c3ab2878c366d13c756f745322a05 的 CI 六项作业全部通过。
  • 原项目 76 个文件及其 76 个快照在测试后全部核对完整 SHA,一致。
  • 当前 VSIX 与新 JetBrains JAR 的 15 个 Python runtime 文件逐字节相同,并与父仓源文件一致;未打入 Agent、测试或开发文件。最终组合的两个安装包已取自同一次 CI run 47027472741 完成同样审计;15 个共享脚本与最终源文件一致,两包均无开发资源。

跨仓依赖与版本

共享 Python 核心:a7ac0cc8fcf7bc56161246e232f77cc4b44a3f86。子仓 #48 已合并至 f2cde4a668aa48a4e5e1d03358ec1a944e72ea2d,与已测试的 0fafbef tree 完全相同;本 PR 的 gitlink 已更新到该子仓 main 提交。版本号仍为 1.29.0,本 PR 不发布新版本。

@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: e90e6997-b363-4557-8178-570d4049c169
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@AliceJump AliceJump left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Walkthrough

本 PR 针对 Split 与 Packed 两种原生模板导出流程进行了性能优化与增量保留改进:在宿主侧引入基于完整字节 SHA-256 的图像尺寸验证缓存(templateImageValidation.ts),使 Split 几何校验与 Python 原生渲染并行执行,并在提交事务中将文件名与字节完全一致的裁剪 PNG 原地保留;在 Python 导出桥接层引入有界(128 MiB)单次消费的 _PackedDecodeCache,复用预检阶段已解码的像素副本。

Layer / File(s) Summary
Host Image Size Cache & Packed Validation
src/templateImageValidation.ts
src/templateAssetData.ts
scripts/test_image_decode.js
新增 cachedTemplateImageSize 与 validatedTemplateImageSize(上限 1024 条 LRU,冻结尺寸对象),并在 validatePackedOutput 及 Split 导出校验中复用。
Parallel Split Export & In-Place Retention
src/splitTemplateExport.ts
scripts/test_split_template_export.js
先行校验非法标签名并计算源图 SHA-256,随后并行启动原生渲染与宿主校验;对同名且字节未变的裁剪图跳过文件替换事务,同时在最终回调前后复核未改动文件的修订状态。
Python Packed Decode Cache
python/export_templates.py
python/tests/test_export_templates.py
新增 _encoded_image_fingerprint 与 _PackedDecodeCache,在 128 MiB 预算内将预检阶段已验证的快照解码数组拷贝后供原生 compress_copy_coco 单次复用。

if entry is not None:
digest, prior_flags, prior_ascii, image = entry
self.remaining += image.nbytes
if type(flags) is int and flags == prior_flags and ascii_path == prior_ascii:

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · 非 ASCII 输出目录缓存命中率

_PackedDecodeCache.read() 要求 ascii_path == prior_ascii,当输出目录名含中文等非 ASCII 字符而其父目录为纯 ASCII 时,会导致 Packed 解码缓存 100% 失配。

在 export_templates()(第 347、365 行)中,预检快照目录通过 tempfile.TemporaryDirectory(prefix="ok-toolkit-export-source-", dir=output.parent) 创建在 output.parent 下,且文件名固定为纯 ASCII 的 image_{index:08d}{suffix};而原生导出器读取的 alias 路径位于 output / "images" / snapshot_name。若 output.parent 为纯 ASCII(如 D:\projects)而 output.name 含中文(如 D:\projects\模板输出),预检读取时 prior_ascii=True,原生导出器读取 alias 时 ascii_path=False。由于两侧在同一 flags 与相同文件字节(已由 current[0] == digest 校验)下由 OpenCV 同一解码器产出相同像素矩阵,建议移除 ascii_path == prior_ascii 限制。

建议修改
-        ascii_path = os.fsdecode(filename).isascii()
         entry = self.entries.pop(key, None)
         if entry is not None:
-            digest, prior_flags, prior_ascii, image = entry
+            digest, prior_flags, image = entry
             self.remaining += image.nbytes
-            if type(flags) is int and flags == prior_flags and ascii_path == prior_ascii:
+            if type(flags) is int and flags == prior_flags:
                 current = _encoded_image_fingerprint(Path(os.fsdecode(filename)))

(同步将第 227 行存入 self.entries[source[0]] 的元组改为 (source[1], flags, retained)。)

}
try {
if (needsPixels) decoded = decodeRgba(bytes);
dimensions = decoded ?? validatedTemplateImageSize(bytes);

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor · 像素校验分支未回填尺寸缓存

当冷启动源图存在重复非 box_ 标签(needsPixels === true)时,decoded ?? validatedTemplateImageSize(bytes) 会因 decoded 为真值而短路跳过尺寸缓存写入,导致该源图在后续导出时仍被视作冷启动。

在第 240–241 行中,当 needsPixels 为 true 时已完成完整的 decodeRgba(bytes) 解码并拿到 { width, height } 及 initial.digest,但因 ?? 短路未写入 templateImageValidation.ts 的 validatedSizes;相反第 303 行对每个生成的裁剪小图都会调用 validatedTemplateImageSize(bytes) 占用 1024 条缓存预算。建议在 decodeRgba(bytes) 成功后也将源图 (initial.digest, { width, height }) 回填进尺寸缓存。

@AliceJump
AliceJump marked this pull request as ready for review December 3, 2026 03:25
@AliceJump
AliceJump merged commit f30484c into main Dec 3, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants