从14s到4s,我如何将 macOS上的 Rust 增量编译时间减少70%?

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Rust 1.51 发布之后,我注意到里面有一个新的特性 Cargo 可以帮助缩短 macOS 的编译时间。升级启用了这个选项周,我的增量开发重建时间从 14s 缩短到了 4s。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"加快构建速度"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Rust 1.51 发行说明中,有一章节是专门介绍如何配置调试信息。默认情况下,在 macOS 上,rustc 会运行一个名为 dsymutil 的工具,该工具会分析二进制文件,然后构建调试信息目录。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果使用 cargo run 来开发构建,那么可能每次都会构建此调试信息,因为它是开发配置文件的默认设置。而在 Rust 1.51 中,我们可以配置 split-debuginfo,使其跳过 dsymutil。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Rust 1.51 发行说明:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"现在,Rust backtraces 切换到使用其它后端,该后端支持加载 debuginfo 无需运行 dsymutil,并且该功能目前已经稳定。"}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有人认为,这个功能很快就会成为 macOS 的默认配置。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如何配置呢?首先打开 Cargo.toml 并添加以下选项:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"null"},"content":[{"type":"text","text":"[profile.dev]split-debuginfo = \"unpacked\"\n"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如果你之前禁用了 debug,那么就无法生成调试信息,自然也就无法看到速度提升。如果你之前因为编译时间太久而禁用了 debug,那么现在可以重新打开,试试这个新方法吧。"}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"到底能快多少?"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Cargo 新特性到底能帮助 debug 提升多少呢?我自己构建了一个 Puzzle Panda 的拼图游戏网站,该网站是使用 Rust 编写的后端 API。经过我自己的测试,cargo run dev rebuilds 从之前的 14s 成功减少到了 4s,大约节省了 70% 的构建时间。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"就项目规模而言,Puzzle Panda 的 Rust API 很小,但是它其中引入了很多传递性依赖关系,因此构建时间很长。14s 的 Dev rebuilds 时间是一直困扰我的问题,但是现在 split-debuginfo 的出现解决了我的难题。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"至于 split-debuginfo 在中型 Rust 项目中,是否会有一样的项目,目前我还不确定。希望有感兴趣的人,能够实践并分享给我们。"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"参考链接:"}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"link","attrs":{"href":"https:\/\/jakedeichert.com\/blog\/reducing-rust-incremental-compilation-times-on-macos-by-70-percent\/","title":"","type":null},"content":[{"type":"text","text":"https:\/\/jakedeichert.com\/blog\/reducing-rust-incremental-compilation-times-on-macos-by-70-percent\/"}]}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章