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

## 添加如下配置
[build]
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编译速度,节约了不少编译时间。

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值