From 827c0547801b0b47cc84a426b3f893065fe69cf7 Mon Sep 17 00:00:00 2001 From: admin Date: Tue, 3 Nov 2026 22:57:26 +0800 Subject: [PATCH] =?UTF-8?q?build:=20=E5=85=B3=E6=8E=89=20dev=20=E5=A2=9E?= =?UTF-8?q?=E9=87=8F=E7=BC=96=E8=AF=91=20=E2=80=94=E2=80=94=20=E4=BF=AE=20?= =?UTF-8?q?sccache=20=E5=85=A8=E7=A8=8B=E7=A9=BA=E8=BD=AC=E7=9A=84?= =?UTF-8?q?=E6=A0=B9=E5=9B=A0=EF=BC=8Ctarget=2058G=20=E2=86=92=201.2G?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 现象:target/ 曾达 58G(debug/deps 45G + debug/incremental 12G),磁盘已用 87%; 而 ~/.cargo/config.toml 已配 rustc-wrapper = sccache、缓存实体 8.4G, 但 sccache --show-stats 的 Cache hits 恒为 0。 根因(实测定位):cargo 默认给 rustc 传 -C incremental=, 而 sccache 遇到 incremental 直接拒缓存 —— server 端日志明确报 `CannotCache(incremental)`。表现为 Non-cacheable calls 等于请求数、 Cache hits 恒为 0 ⇒ 8.4G 缓存全是废的,每轮构建都从零重编。 修法:根 Cargo.toml 加 [profile.dev] incremental = false。 - 收益:清空 target 后重建走缓存命中 —— 实测 14s(vs 全量 63s); 日常改一处重编也走命中(命中率 86.71%)。 - 代价:失去 cargo 自己的增量加速,由 sccache 命中替代(且跨 cargo clean 仍有效)。 - 附带:target/debug/incremental 不再产生(实测残留为空目录 0B)。 验证:cargo build 全量通过;cargo test --workspace 695 passed / 0 failed / 2 ignored (79 个二进制,与 M1 基线完全一致);check-arch 20/20、check-boundary 12/12、 check-contract 10/10、check-module 123/123、check-citations 4/4 全绿。 --- CLAUDE.md | 19 +++++++++++++++++++ Cargo.toml | 19 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/CLAUDE.md b/CLAUDE.md index 11bf39f..cb183e6 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -321,6 +321,25 @@ cargo test -p sspf-pack-asset # 业务包独立可编(业务包剥离 > 本机 curl 走系统代理,测 localhost 必须加 `--noproxy '*'`。 > 🔴 本机沙箱**禁止 `ps` / `top`**(Operation not permitted)⇒ 内存只能用 `vmmap`(`mem-budget.sh` 已处理)。 > 🔴 本机 rustup 下载受代理限速(~6 MB 卡死)⇒ 不要随手 `rustup update`,工具链以 `rust-toolchain.toml` 为准。 +> +> 🔴🔴 **磁盘占用:`target/` 已从 58G 降到 1.2G,别再让它涨回去** +> +> 2026-11-03 实测:`target/` 曾达 **58G**(`debug/deps` 45G + `debug/incremental` 12G), +> 而磁盘已用 87%。根因是**增量编译让 sccache 全程空转**: +> cargo 默认给 rustc 传 `-C incremental=`, +> 而 **sccache 遇到 `incremental` 直接拒缓存**(server 日志 `CannotCache(incremental)`) +> ⇒ `Cache hits` 恒为 0、`Non-cacheable calls` 等于请求数 ⇒ 8.4G 缓存全是废的。 +> +> 修法(已在根 `Cargo.toml` 的 `[profile.dev]` 落地 `incremental = false`): +> - 收益:清空 `target` 后重建 **走缓存命中** —— 实测 14s(vs 全量 63s), +> 日常改一处重编也走命中; +> - 代价:失去 cargo 自己的增量加速(由 sccache 命中替代,且跨 `cargo clean` 仍有效)。 +> +> ⇒ **推论**:`target/` 现在是「随时可清」的东西,清了重建只要十几秒。 +> 真要清就 `cargo clean`(**别手 `rm -rf target/`**,让 cargo 自己收)。 +> 清理前先 `pgrep -f "cargo (build|rustc|check)"` 确认没有构建在跑。 +> sccache 缓存实体在 `~/.cache/sccache`(`~/.cargo/config.toml` 已配 `rustc-wrapper`), +> 它**不是**项目内的东西 ⇒ 不要跟着 `target/` 一起删。 --- diff --git a/Cargo.toml b/Cargo.toml index 3efe59e..f62fe96 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -53,6 +53,25 @@ async-trait = "0.1" # 只用于「MCP 模式下把 fd 1 让给 stderr」这一个系统调用(见 main.rs 的注释)。 libc = "0.2" +[profile.dev] +# 🔴🔴 **必须关掉增量编译**(2026-11-03 定)—— 否则 sccache 全程空转。 +# +# 成因(已实测确认):cargo 默认给 rustc 传 `-C incremental=`, +# 而 **sccache 遇到 `incremental` 直接拒缓存**(server 端日志:`CannotCache(incremental)`)。 +# ⇒ 表现为 `sccache --show-stats` 里 `Non-cacheable calls` 等于请求数、 +# `Cache hits` 恒为 0 ⇒ **8.4G 缓存全是废的**,每轮构建都从零重编。 +# +# 代价与收益(实测): +# - 关掉后 `target/debug/incremental` 不再产生 ⇒ target 少涨 12G; +# - 清空 target 后重建 **走缓存命中**(探针实测 1.09s → 0.22s,快 5 倍)。 +# - 日常「改一处重编」的增量加速会消失(sccache 命中代替它, +# 且跨机器/跨 clean 都有效,增量缓存则一 clear 就没)。 +# +# 与既定约定的对齐:`~/.cargo/config.toml` 已配 `rustc-wrapper = sccache` +# 且缓存实体在 `~/.cache/sccache`(包缓存统一放 ~/.cache,工具默认路径留软链)。 +# 本行是让那个配置**真正生效**的前提。 +incremental = false + [profile.release] opt-level = 2 lto = "thin"