Skip to content

perf(build): compile libsqlite3-sys at O3 and drop no-op dist wrapper overrides - #7209

Merged
M4n5ter merged 1 commit into
mainfrom
perf/dist-sqlite-o3
Nov 23, 2026
Merged

M4n5ter merged 1 commit into
mainfrom
perf/dist-sqlite-o3

Conversation

@M4n5ter

@M4n5ter M4n5ter commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Summary

English

What changed

Refines [profile.dist] per-package opt-level = 3 overrides in Cargo.toml based on empirical profiling and real-Host stress benchmarks:

  • Add [profile.dist.package.libsqlite3-sys] opt-level = 3: Compiles bundled SQLite + FTS5 (sqlite3.c) at -O3 instead of -Os. Because libsqlite3-sys is a pure C static library compiled by cc, it adds zero Rust IR to ThinLTO (flat rustc peak RSS on 7 GB CI runners) while delivering measurable SQLite read/tail-latency gains for +0.69 MB (+0.32 MB gzip) binary delta.
  • Remove no-op thin-wrapper overrides flate2 and zstd: Both are thin Rust wrapper crates whose opt-level = 3 does not propagate into their underlying compression backends (zlib-rs/miniz_oxide and C zstd-sys, which remained at -Os). Empirically bumping zstd-sys + zlib-rs to -O3 only moved application compose by -0.6%, so removing these two wrapper shells cleans up dead config with zero regression.

Refs #7185, #7189, #7193, #7194, #7204.


Empirical Experimental Results

All local builds and runtime benchmarks were conducted on macOS ARM64 (rustc 1.100.0, pinned prebuilt librusty_v8.a, panic = "unwind").

1. Isolated A/B Verification of This Exact PR (baseline vs candidate: +libsqlite3-sys=3, -flate2, -zstd)

Metric Baseline (current dist) Candidate (this PR) Delta
maka binary size 235.98 MB 236.67 MB +0.69 MB (+0.29%)
__TEXT,__text code section 156.12 MB 156.81 MB +0.69 MB (+0.44%)
maka gzip -6 size 90.25 MB 90.57 MB +0.32 MB (+0.35%)
maka-python-worker size 8.47 MB 8.47 MB 0.00 MB
Final maka rustc peak RSS (default parallelism) 10.90 GiB 10.94 GiB +0.04 GiB (+0.4%, flat)
Final maka rustc user CPU 1,141.2 s 1,126.9 s -1.3% (flat)
Host base / Scenario J end RSS 214.6 / 272.8 MiB 214.3 / 273.3 MiB ~0 MiB
ultra_long_session wall (3 pairs, 6/6 pass) 10.50 s median (10.57 s mean) 10.48 s median (10.48 s mean) -0.9% mean
ultra_long_session WS 15-turn sum 4,911 ms median ([4924, 4911, 4894]) 4,864 ms median ([4872, 4864, 4838]) -1.0% (all 3 faster)
Scenario J (200 turns + 143 compactions + 78 MiB SQLite, 3 pairs) 27.68 s median (27.55 s mean) 27.41 s median (27.31 s mean) -0.9%
Scenario J bg history p99 (~378 reads/run) 11 ms ([11, 11, 11]) 9 ms ([9, 9, 10]) -18.2% p99 tail

(Confirmed libsqlite3-sys 0.39.0 build script receives OPT_LEVEL=3 for sqlite3/sqlite3.c. Cold --version 4v4 samples overlapped within SSD/AMFI variance: baseline [2084, 1532, 1551, 1541] ms vs candidate [1669, 1764, 1829, 1574] ms; warm --version 15v15: 9.01 vs 9.38 ms.)

2. Why Not Blanket opt-level = 3? (Head-to-Head dist_s vs Full dist_o3 Stress Test)

Same clean main (19e5caf51), counterbalanced S / O3 / O3 / S runs on 4 real-Host stress suites plus mincore=0 cold startup:

Metric / Stress Suite dist_s (current dist) dist_o3 (all crates O3) Delta (O3 vs s)
maka binary / __TEXT,__text 235.98 / 156.12 MB 309.21 / 223.13 MB +73.23 MB (+31.0%) / +42.9%
maka gzip -6 90.25 MB 123.55 MB +33.30 MB (+36.9%)
Final rustc peak RSS (default parallelism) / user CPU 10.86 GiB / 1,126 s 13.64 GiB / 1,827 s +2.78 GiB (+25.6%) / +62.3%
Cold --version (mincore=0, 4v4 non-overlapping) 1,641 ms 2,119 ms +477 ms (+29.1% slower)
Host idle base RSS 211.5–214.4 MiB 219.8–224.1 MiB +9–10 MiB
24-session mixed protocols + chaos 2.61 / 2.60 s · Peak 276/273 MiB 2.59 / 2.48 s · Peak 292/289 MiB Latency flat · +16 MiB RSS
Long session + compaction (18 Chat + 15 WS) 10.55 / 10.71 s · End 251 MiB 10.32 / 10.29 s · End 269 MiB ~3.1% faster · +17 MiB RSS
Scenario J (200-turn mega session, 143 compactions) 28.52 / 27.40 s (mean 27.96 s) 26.85 / 25.89 s (mean 26.37 s) ~5.7% faster · +13 MiB RSS
Scenario I (456 sessions, 36 V8/Python trees, 32 UI clients) 14.47 req/s mean 14.80 req/s mean ~2.3% higher throughput

Takeaway: Setting all 500+ remaining crates to O3 has a 2%–5.7% warm ceiling under extreme load, while causing +73 MB binary bloat, +29.1% slower cold startup, +10–17 MiB runtime RSS, and +25.6% higher rustc peak RSS (pushing 7 GB macos-latest CI runners past OOM, since dist_s at CARGO_BUILD_JOBS=3 already peaks at 6.95 GiB process-group RSS).

3. Tier 4 Architecture Matrix (928 Interleaved Samples on main 1a940845b, CARGO_BUILD_JOBS=3)

Variant Clean Build / Process-Group Peak RSS maka / npm .tgz / .dmg Notable Runtime Deltas vs Baseline (median) 7 GB CI Viable?
Baseline (s, ThinLTO, 16 CGU) 1,032 s / 6.95 GiB 237.2 / 128.5 / 134.3 MB Reference Yes
Group A (+libsqlite3-sys/zstd-sys/zlib-rs=3, -zstd/-flate2) 1,040 s / 7.00 GiB 238.2 / 129.3 / 135.2 MB 5× Host history + Recall: -15.5% (65.8 → 55.6 ms, 95% CI -17.8%..-14.0%); compose: -0.6% SQLite-only yes
Group B · PGO (IR gen + train + use) ~2,280 s (2.21×) / 9.47 GiB (gen) 239.5 / 132.1 (+2.75%) / 137.7 MB Host ready -13.4%, build-ui -15.9%, cold start +0.2%, Recall -1.4% No (OOM + 2.21× time + bloat)
Group C · FatLTO (s, lto="fat", 1 CGU) 1,243 s / 23.26 GiB (29.23 GiB footprint) 174.9 / 111.2 (-13.5%) / 115.4 MB (-14.1%) CLI cold start -24.2% (2000 → 1516 ms), build-ui -12.1%, Python worker -13.4% No (needs 32 GB+ runner)

3-platform linker audit: macOS -dead_strip_dylibs saved only 80 B with zero statistical latency change and -no_deduplicate saved 0 B; Linux zig 0.17.1 rejects --icf=safe; Windows MSVC already passes /OPT:REF,ICF by default. None of those extra linker flags are worth adding.


Projected Optimal Configuration on High-Memory Build Runners (32 GB+ Release Runner)

If release builds (e.g., tagged stable releases, without increasing daily Nightly runner spend) move to a 32 GB+ RAM build runner, the optimal configuration is FatLTO (lto = "fat", codegen-units = 1) + opt-level = "s" + the refined overrides (including libsqlite3-sys = 3):

Dimension Current 7 GB Nightly (dist + this PR) Projected Optimal on 32 GB+ Runner (FatLTO + this PR) Projected Gain vs Current
maka macOS binary ~236.7 MB ~175.6 MB (174.9 + ~0.7 MB SQLite O3) -61.1 MB (-25.8%)
maka-desktop-shell macOS binary ~75.3 MB ~56.9 MB -18.4 MB (-24.4%)
macOS npm .tgz ~128.8 MB ~111.5 MB (total -45.5% vs pre-#7185 204.7 MB) -17.3 MB (-13.4%)
macOS .dmg installer ~134.6 MB ~115.8 MB (total -46.6% vs pre-#7185 217 MB) -18.8 MB (-14.0%)
CLI cold start (--version, mincore=0) ~1,640–2,000 ms ~1,516 ms ~-24% (-480 ms)
Host history / Recall & SQLite tail -15.5% read / -18% bg p99 (from libsqlite3-sys = 3) Retained (independent of LTO) Same SQLite win
Clean build time (vs ThinLTO) 17.2 min (3-job) ~20.7 min (+3.5 min) +20% time
Required build runner RAM 7 GB (6.95 GiB process group) 32 GB+ (23.23 GiB RSS / 29.23 GiB macOS peak footprint; 16 GB OOMs) Needs 32 GB+ (recommended 64 GB for macOS safety headroom)

Note: Even on 32 GB+ runners, PGO is still not recommended for standard releases (doubles CI duration to ~38 min, adds fragile training pipelines, inflates package size by +2.75%, and gives no cold-start/Recall improvement over FatLTO + libsqlite3-sys = 3).

简体中文

改动内容

基于实测 profiling 和多套真实 Host 压力测试,收敛 Cargo.toml 中 [profile.dist] 的单包 opt-level = 3 覆写列表:

  • 新增 [profile.dist.package.libsqlite3-sys] opt-level = 3:让内置的 SQLite + FTS5(sqlite3.c)从 -Os 改为 -O3 编译。由于 libsqlite3-sys 是 cc 编译的纯 C 静态库,不会向 rustc ThinLTO 注入任何 Rust IR(对 7 GB CI runner 的编译峰值内存零压力),仅增加 +0.69 MB(gzip +0.32 MB)体积,即可换取明确的 SQLite 读路径与长尾延迟收益。
  • 清理失效的薄壳覆写 flate2 和 zstd:这两个 crate 只是 Rust 薄壳包装,其 opt-level = 3 并不会传递到底层压缩实现(zlib-rs/miniz_oxide 和 C 后端 zstd-sys 基线仍按 -Os 编译);且实测把 zstd-sys + zlib-rs 真拉到 -O3 对 application compose 也仅差 -0.6%。删除这两个无效壳子可减少死配置且无性能回退。

关联 PR:#7185、#7189、#7193、#7194、#7204。


实验结果汇总

所有本地构建与压测均在 macOS ARM64(rustc 1.100.0,锁定预编译 librusty_v8.a,panic = "unwind")完成。

1. 本 PR 精确隔离 A/B 验证(baseline vs candidate:+libsqlite3-sys=3, -flate2, -zstd)

指标 Baseline(当前 dist) Candidate(本 PR) 变化
maka 二进制体积 235.98 MB 236.67 MB +0.69 MB (+0.29%)
__TEXT,__text 代码段 156.12 MB 156.81 MB +0.69 MB (+0.44%)
maka gzip -6 体积 90.25 MB 90.57 MB +0.32 MB (+0.35%)
maka-python-worker 体积 8.47 MB 8.47 MB 0.00 MB
最终 maka rustc 峰值 RSS(默认并发) 10.90 GiB 10.94 GiB +0.04 GiB (+0.4%,持平)
最终 maka rustc user CPU 1,141.2 s 1,126.9 s -1.3%(持平)
Host 基础 / Scenario J 结束 RSS 214.6 / 272.8 MiB 214.3 / 273.3 MiB ~0 MiB
ultra_long_session 耗时(3 组交错,6/6 全过) 中位 10.50 s(均 10.57 s) 中位 10.48 s(均 10.48 s) 均值 -0.9%
ultra_long_session WS 15 轮总耗时 中位 4,911 ms([4924, 4911, 4894]) 中位 4,864 ms([4872, 4864, 4838]) -1.0%(3 轮全胜)
Scenario J(200 轮 + 143 次 compaction + 78 MiB SQLite,3 组) 中位 27.68 s(均 27.55 s) 中位 27.41 s(均 27.31 s) -0.9%
Scenario J 后台 history p99(每轮 ~378 次读) 11 ms([11, 11, 11]) 9 ms([9, 9, 10]) p99 尾延迟 -18.2%

(已确认 libsqlite3-sys 0.39.0 构建脚本以 OPT_LEVEL=3 编译 sqlite3/sqlite3.c。冷启 --version 4v4 样本分布重叠在磁盘/AMFI 波动内:baseline [2084, 1532, 1551, 1541] ms vs candidate [1669, 1764, 1829, 1574] ms;热启 15v15:9.01 vs 9.38 ms。)

2. 为什么不全部 opt-level = 3?(dist_s vs 全量 dist_o3 4 套真实 Host 压测对决)

同一干净 main(19e5caf51),对称交错 S / O3 / O3 / S 实测 4 套真实 Host 压测 + mincore=0 冷启:

指标 / 压测场景 dist_s(当前 dist) dist_o3(全量 O3) 变化(O3 vs s)
maka 文件体积 / __TEXT,__text 235.98 / 156.12 MB 309.21 / 223.13 MB +73.23 MB (+31.0%) / +42.9%
maka gzip -6 90.25 MB 123.55 MB +33.30 MB (+36.9%)
最终 rustc 峰值 RSS(默认并发)/ user CPU 10.86 GiB / 1,126 s 13.64 GiB / 1,827 s +2.78 GiB (+25.6%) / +62.3%
冷启 --version(mincore=0,4v4 区间不重叠) 1,641 ms 2,119 ms +477 ms(慢 29.1%)
Host 空载常驻 RSS 211.5–214.4 MiB 219.8–224.1 MiB +9–10 MiB
24 并发混合协议 + Chaos 2.61 / 2.60 s · 峰值 276/273 MiB 2.59 / 2.48 s · 峰值 292/289 MiB 延迟打平 · +16 MiB RSS
长会话 + Compaction(18 Chat + 15 WS) 10.55 / 10.71 s · 结束 251 MiB 10.32 / 10.29 s · 结束 269 MiB 快 ~3.1% · +17 MiB RSS
Scenario J(200 轮大 Payload + 143 次 Compaction) 28.52 / 27.40 s(均 27.96 s) 26.85 / 25.89 s(均 26.37 s) 快 ~5.7% · +13 MiB RSS
Scenario I(456 会话 + 36 棵 V8/Python 树 + 32 UI 客户端) 均值 14.47 req/s 均值 14.80 req/s 归一化吞吐 +2.3%

结论:把剩下 500 多个 crate 全拉到 O3,极限压测下的热态收益上限只有 2%–5.7%,却要付出 +73 MB 体积、冷启慢 29.1%(+477 ms)、常驻内存多吃 10–17 MiB、编译器峰值内存涨 +25.6%(dist_s 在 CARGO_BUILD_JOBS=3 下进程组峰值已达 6.95 GiB,全量 O3 会直接顶爆 7 GB macos-latest CI runner)。因此只需精准保留热点内核(补上 libsqlite3-sys),不再扩充 Rust O3 名单。

3. Tier 4 全方案对比矩阵(main 1a940845b,928 条交错实测样本,CARGO_BUILD_JOBS=3)

方案 Clean Build / 进程组峰值 RSS maka / npm .tgz / .dmg 关键运行时变化(中位数 vs 基线) 7 GB CI 可行?
Baseline(s、ThinLTO、16 CGU) 1,032 s / 6.95 GiB 237.2 / 128.5 / 134.3 MB 基线 可行
A 组(+libsqlite3-sys/zstd-sys/zlib-rs=3、-zstd/-flate2) 1,040 s / 7.00 GiB 238.2 / 129.3 / 135.2 MB 5 次 Host history + Recall 读 -15.5%(65.8 → 55.6 ms,95% CI -17.8%..-14.0%);compose -0.6% 单加 SQLite 可行
B 组 · PGO(IR 插桩 + 训练 + use) ~2,280 s(2.21×) / 9.47 GiB(插桩) 239.5 / 132.1(+2.75%)/ 137.7 MB Host ready -13.4%、build-ui -15.9%,冷启 +0.2%,Recall -1.4% 否(OOM + 2.21× 耗时 + 包体变胖)
C 组 · FatLTO(s、lto="fat"、1 CGU) 1,243 s / 23.26 GiB(29.23 GiB footprint) 174.9 / 111.2(-13.5%)/ 115.4 MB(-14.1%) CLI 冷启 -24.2%(2000 → 1516 ms)、build-ui -12.1%、Python worker -13.4% 否(需 32 GB+ runner)

三平台 linker 审计结论:macOS -dead_strip_dylibs 仅省 80 B 且延迟置信区间跨零、-no_deduplicate 省 0 B;Linux zig 0.17.1 直接拒 --icf=safe;Windows MSVC 默认已启用 /OPT:REF,ICF。因此三平台均不额外增加 linker 参数。


编译机器资源充足时的最优预估方案(32 GB+ 正式发版 Runner)

如果未来针对**正式版本发版(tag release,不给每日 Nightly 增加大规格机器成本)**提供 32 GB+ 内存编译机,综合体积与性能的最优配置是 FatLTO (lto = "fat", codegen-units = 1) + opt-level = "s" + 精简后的热点覆写(含本 PR 的 libsqlite3-sys = 3):

维度 当前 7 GB Nightly(dist + 本 PR) 32 GB+ 发版机最优预估(FatLTO + 本 PR) 相对当前预估收益
maka macOS 二进制 ~236.7 MB ~175.6 MB (174.9 + ~0.7 MB SQLite O3) -61.1 MB (-25.8%)
maka-desktop-shell macOS 二进制 ~75.3 MB ~56.9 MB -18.4 MB (-24.4%)
macOS npm .tgz ~128.8 MB ~111.5 MB (较 #7185 前 204.7 MB 总降 45.5%) -17.3 MB (-13.4%)
macOS .dmg 安装包 ~134.6 MB ~115.8 MB (较 #7185 前 217 MB 总降 46.6%) -18.8 MB (-14.0%)
CLI 冷启(--version,mincore=0) ~1,640–2,000 ms ~1,516 ms ~-24%(省 ~480 ms)
Host history / Recall 与 SQLite 尾延迟 读 -15.5% / 后台 p99 -18% (来自 libsqlite3-sys = 3) 完全保留 (C 静态库独立于 Rust LTO) 保持同等 SQLite 收益
Clean build 耗时(对比 ThinLTO) 17.2 分钟(3 并发) ~20.7 分钟(仅多 3.5 分钟) +20% 耗时
所需编译机内存 7 GB(进程组 6.95 GiB) 32 GB+ (单 rustc 23.23 GiB RSS / 29.23 GiB macOS peak footprint;16 GB 会 OOM) 建议 32–64 GB(macOS 留足系统余量)

注:即便在 32 GB+ 编译机上,仍不建议默认引入 PGO(全流程翻倍到 ~38 分钟、引入易碎的训练语料维护成本、包体反而涨 +2.75%,且在冷启和 Recall 上不如 FatLTO + libsqlite3-sys = 3)。

Verification

  • Verified cargo build --locked --profile dist -p libsqlite3-sys -vv passes OPT_LEVEL=3 when compiling sqlite3/sqlite3.c.
  • Ran isolated counterbalanced A/B builds and real-Host stress benchmarks on macOS ARM64 (baseline vs candidate: +libsqlite3-sys=3, -flate2, -zstd) covering binary/section/gzip sizes, final rustc peak RSS, mincore=0 cold startup, host_load_stress::real_host_ultra_long_session_and_compaction_stress (3 pairs, 6/6 passed), and host_hell_stress::real_host_200_turn_mega_session_huge_payloads_5level_fork_and_queue_grinder (3 pairs, 6/6 passed).
  • git diff --check and cargo fmt --check passed.
  • Not run locally: full cross-platform Linux/Windows dist Nightly builds (covered by CI and subsequent Nightly run).

AI use

Select exactly one:

  • No generative tool made a substantive contribution
  • Generative tooling made a substantive contribution

Tool(s) and scope: Maka (Gemini) and Magpie (codex/gpt-6.2-astra) ran the empirical [profile.dist] benchmarks, stress tests, dependency audit, Cargo.toml edit, and PR write-up.

Generated-by: Maka (Gemini)

Checklist

  • Tests cover the change and fail without it (Cargo release-profile compiler optimization flags only; verified via -vv flag inspection and real-Host stress A/B benchmarks)
  • Format, typecheck and the affected suites pass locally

Does this PR entail a breaking change or persisted-data migration?

  • Yes — describe user/data impact, in-place migration, and upgrade_compat.rs coverage under Summary above
  • No

Does this PR entail a change in behavior?

  • Yes — described under Summary above
  • No

… overrides

Compile bundled SQLite + FTS5 (libsqlite3-sys) at opt-level = 3 in
[profile.dist] and remove the no-op flate2 and zstd thin-wrapper
overrides whose O3 setting did not propagate into zlib-rs/miniz_oxide
or zstd-sys.

libsqlite3-sys is compiled by cc as a C static library so it adds no
Rust IR to ThinLTO peak RSS on 7 GB CI runners, grows maka by only
+0.69 MB (+0.32 MB gzip), and reduces SQLite read/tail latency.

Generated-by: Maka (Gemini)
@github-actions github-actions Bot added the effort/XS Under 10 readable lines label Nov 23, 2026
@M4n5ter
M4n5ter merged commit c6955d7 into main Nov 23, 2026
20 checks passed
@M4n5ter
M4n5ter deleted the perf/dist-sqlite-o3 branch November 23, 2026 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

effort/XS Under 10 readable lines

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants