Ubuntu 22.04下加快rust的编译速度
在学习Rust的时候,相信不少人和我一样一直受Rust编译慢的问题困扰。最近阅读一篇写得相当不错的英文博客《Tips for Faster Rust Compile Times》,加上最近刚刚入手一台华为云服务器,于是决定尝试一下效果如何(主要介绍mold,sccache)。
运行环境:
操作系统:Ubuntu 22.04
CPU与内存:1核2G
mold版本:v1.3.1
编译、安装mold
按照mold官方的教程,使用源代码进行编译、安装。
-
-
git clone https://github.com/rui314/mold.git
-
cd mold
-
git checkout v1.3.1
-
make -j$(nproc) CXX=clang
-
sudo make install
在执行编译时,系统报错,是因为mold使用C 20编写,需要相应的编译器进行编译器(g -10
),而操作系统默认的编译器gcc版本较低(gcc 9.4.0)。接下来则先安装相应的编译器。
-
apt install g -10
-
g -10 --version
-
apt install gcc-10
-
gcc-10 --version
再次编译,报如下错误;
-
root@hecs-131857:/home/rust/mold# make -j$(nproc) CXX=clang
-
clang -DMOLD_VERSION=\"1.0.0\" -DLIBDIR="\"/usr/local/lib\"" -DGIT_HASH=\"ed9924895d9b9584106791247596677db8113528\" -Ithird-party/mimalloc/include -Ithird-party/tbb/include -O2 -pthread -fPIE -fno-unwind-tables -fno-asynchronous-unwind-tables -std=c 20 -fno-exceptions -c -o out/main.o main.cc
-
In file included from main.cc:1:
-
./elf/mold.h:30:10: fatal error: 'xxh3.h' file not found
于是使用apt-file查询xxh3.h所在的库并进行安装。
-
apt-get install apt-file
-
apt-file update
-
apt-file search xxh3.h
-
apt install libxxhash-dev
再次编译,还是报错,继续安装cmake,编译......。
-
(cd out/mimalloc; CFLAGS=-DMI_USE_ENVIRON=0 cmake -G'Unix Makefiles' ../../third-party/mimalloc)
-
/bin/sh: 1: cmake: not found
-
make: *** [Makefile:115: out/mimalloc/libmimalloc.a] Error 127
-
-
apt install cmake
最后进行安装。
-
root@hecs-131857:/home/rust/mold# make install
-
install -m 755 -d /usr/local/bin
-
install -m 755 mold /usr/local/bin
-
strip /usr/local/bin/mold
-
install -m 755 -d /usr/local/lib/mold
-
install -m 644 mold-wrapper.so /usr/local/lib/mold
-
strip /usr/local/lib/mold/mold-wrapper.so
-
install -m 755 -d /usr/local/share/man/man1
-
install -m 644 docs/mold.1 /usr/local/share/man/man1
-
ln -sf mold /usr/local/bin/ld.mold
-
ln -sf mold /usr/local/bin/ld64.mold
测试mold
接下来测试未使用mold和使用mold两种情况的编译速度。
测试1:未使用mold
-
root@hecs-131857:/home/rust/mp-share-it# cargo clean
-
root@hecs-131857:/home/rust/mp-share-it# cargo build
-
-------省略-------
-
Compiling tower v0.4.11
-
Compiling hyper v0.14.15
-
Compiling hyper-tls v0.5.0
-
Compiling axum v0.3.4
-
Compiling reqwest v0.11.7
-
Compiling mp-share-it v0.1.0 (/home/rust/mp-share-it)
-
Finished dev [unoptimized debuginfo] target(s) in 3m 20s
测试2:使用mold,(修改 ~/.cargo/config
)
-
[target.x86_64-unknown-linux-gnu]
-
linker = "clang"
-
rustflags = ["-C", "link-arg=-fuse-ld=/usr/local/bin/mold"]
-
root@hecs-131857:/home/rust/mp-share-it# cargo clean
-
root@hecs-131857:/home/rust/mp-share-it# cargo build
-
-------省略-------
-
Compiling tower v0.4.11
-
Compiling hyper v0.14.15
-
Compiling hyper-tls v0.5.0
-
Compiling axum v0.3.4
-
Compiling reqwest v0.11.7
-
Compiling mp-share-it v0.1.0 (/home/rust/mp-share-it)
-
Finished dev [unoptimized debuginfo] target(s) in 3m 04s
经过一番操作折腾(走了不少弯路,踩了一些坑),通过测试的结果来看,mold对于加快Rust的编译速度还是有一定作用的(还有很大的优化空间)。
编译、安装sccache
-
cargo install sccache
-
-
-
vim ~/.cargo/config
-
-
-
[]
-
rustc-wrapper = "/root/.cargo/bin/sccache"
测试sccache
测试1:未使用sccache
-
root@hecs-131857:/home/rust/leetcode-rust# cargo build --release
-
-------省略-------
-
Compiling criterion-plot v0.4.4
-
Compiling criterion v0.3.5
-
Compiling leetcode-rust v0.1.0 (/home/rust/leetcode-rust)
-
Finished release [optimized] target(s) in 2m 43s
-
root@hecs-131857:/home/rust/leetcode-rust#
测试2:使用sccache
-
root@hecs-131857:/home/rust/leetcode-rust# cargo clean
-
root@hecs-131857:/home/rust/leetcode-rust# cargo build --release
-
-------省略-------
-
Compiling criterion-plot v0.4.4
-
Compiling criterion v0.3.5
-
Compiling leetcode-rust v0.1.0 (/home/rust/leetcode-rust)
-
Finished release [optimized] target(s) in 26.76s
-
root@hecs-131857:/home/rust/leetcode-rust#
经过测试,使用sccache能大幅加快rust编译速度,节约了不少编译时间。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgchgbe
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01