Repository navigation
perf: 加速原生模板导出并保留未变化文件 - #55
Conversation
|
Important
This repository does not receive automatic reviews because it has fewer than 10 stars.
|
AliceJump
left a comment
There was a problem hiding this comment.
Walkthrough
本 PR 针对 Split 与 Packed 两种原生模板导出流程进行了性能优化与增量保留改进:在宿主侧引入基于完整字节 SHA-256 的图像尺寸验证缓存(templateImageValidation.ts),使 Split 几何校验与 Python 原生渲染并行执行,并在提交事务中将文件名与字节完全一致的裁剪 PNG 原地保留;在 Python 导出桥接层引入有界(128 MiB)单次消费的 _PackedDecodeCache,复用预检阶段已解码的像素副本。
| Layer / File(s) | Summary |
|---|---|
Host Image Size Cache & Packed Validationsrc/templateImageValidation.tssrc/templateAssetData.tsscripts/test_image_decode.js |
新增 cachedTemplateImageSize 与 validatedTemplateImageSize(上限 1024 条 LRU,冻结尺寸对象),并在 validatePackedOutput 及 Split 导出校验中复用。 |
Parallel Split Export & In-Place Retentionsrc/splitTemplateExport.tsscripts/test_split_template_export.js |
先行校验非法标签名并计算源图 SHA-256,随后并行启动原生渲染与宿主校验;对同名且字节未变的裁剪图跳过文件替换事务,同时在最终回调前后复核未改动文件的修订状态。 |
Python Packed Decode Cachepython/export_templates.pypython/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: |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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 }) 回填进尺寸缓存。
问题
原生模板导出已与框架输出统一,但 Split 的宿主解码与 Python 生成串行执行,重复导出仍完整解码并替换未变化 PNG;Packed 还会重复解码预检过的图片。
改动
实机结果
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 摘要一致,源文件未变。
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 相同。
这是按旧→新运行的小样本补测,未做 ABBA。EF Split 首轮单样本为 3.163 → 3.355 秒(增加 6.1%),不能泛称首轮全面提速。生产要求与正式 CI 仍为 JDK 21。
验证
a7ac0cc的 六项 CI 全绿;最终 HEAD6bb9416eb10c3ab2878c366d13c756f745322a05的 CI 六项作业全部通过。47027472741完成同样审计;15 个共享脚本与最终源文件一致,两包均无开发资源。跨仓依赖与版本
共享 Python 核心:
a7ac0cc8fcf7bc56161246e232f77cc4b44a3f86。子仓 #48 已合并至f2cde4a668aa48a4e5e1d03358ec1a944e72ea2d,与已测试的0fafbeftree 完全相同;本 PR 的 gitlink 已更新到该子仓 main 提交。版本号仍为 1.29.0,本 PR 不发布新版本。