Skip to content

[Infrastructure] Cache encoded images and minified HTML across builds - #45

Merged
agoudbg merged 5 commits into
mainfrom
perf/build-pipeline
Sep 16, 2026
Merged

agoudbg merged 5 commits into
mainfrom
perf/build-pipeline

Conversation

@Alice39s

@Alice39s Alice39s commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

背景

build workflow 每次跑构建都在重新编码所有图片和页面,但大部分时候源文件根本没变过。

排查后发现 optimize_assets 的增量判断是摆设:MkDocs 每次非 dirty 构建都会先把 site/ 掀掉,上一轮的产物和判断依据被连带抹掉,于是每张图每次都重编码。

WebP 压缩模块之前写死了最高压缩档 method=6,压缩时长暴增数倍,换来的却只有微不足道的 1.7% 体积缩减。

改动

  • site/ 外面加一层内容寻址缓存,键由输入内容和变换参数一起算出。命中的栅格图和页面直接复制进 site/,只有源文件或参数变了才重新编码。
  • 调整 WebP 档位与并发,将 method 从 6 降到 5;并发不再写死 4,改为 CPU 核心数 -1 (上限 8)。
  • 接管 HTML 压缩流程,并补上它在模板事件里做的那一次,否则 404.html 不会被压缩。

基准测试

pytest-benchmark,同一台机器两份 worktree 交替跑,取 min。

场景 改前 改后 提升倍数
全量构建,冷缓存 4.41 s 2.33 s 1.89x
全量构建,热缓存 4.50 s 0.84 s 5.36x

同一批 51 张图片的 WebP 档位权衡:

WebP method 编码耗时 体积
4 0.59 s +3.6%
5 0.68 s +1.7%
6 2.62 s (baseline)

为什么不用 minify-html

本来优先试了 Rust 的 minify-html:快近 10 倍,产物还更小。换成它之后逐字节对比输出,才看出它会丢掉空属性值。

MkDocs Material 的可折叠导航直接拿 label[tabindex] 当选择器,而模板渲染出来的正是 tabindex=""

空属性一被吞掉,导航的 aria-expanded 就再也不会更新。上游没有保留空属性的配置项,所以还是留着 htmlmin2,用上面那层外置缓存把性能差距瞬间抹平。

Comment thread tests/test_minify.py

import pytest

import nmteam_support.minify as minify
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Sep 15, 2026

Copy link
Copy Markdown

Deploying nmteam-support with  Cloudflare Pages  Cloudflare Pages

Latest commit: 4c1d336
Status: ✅  Deploy successful!
Preview URL: https://e3054767.nmbot-telegram-doc.pages.dev
Branch Preview URL: https://perf-build-pipeline.nmbot-telegram-doc.pages.dev

View logs

@Alice39s Alice39s changed the title Cache encoded images and minified HTML across builds [Infrastructure] Cache encoded images and minified HTML across builds Sep 15, 2026
@agoudbg
agoudbg merged commit 5eb0a13 into main Sep 16, 2026
6 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