• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Ubuntu 22.04下加快rust的编译速度

武飞扬头像
smallswan
帮助1

       在学习Rust的时候,相信不少人和我一样一直受Rust编译慢的问题困扰。最近阅读一篇写得相当不错的英文博客《Tips for Faster Rust Compile Times》,加上最近刚刚入手一台华为云服务器,于是决定尝试一下效果如何(主要介绍mold,sccache)。

运行环境:
操作系统:Ubuntu 22.04
CPU与内存:1核2G
mold版本:v1.3.1

编译、安装mold

按照mold官方的教程,使用源代码进行编译、安装。

  1.  
     
  2.  
    git clone https://github.com/rui314/mold.git
  3.  
    cd mold
  4.  
    git checkout v1.3.1
  5.  
    make -j$(nproc) CXX=clang
  6.  
    sudo make install

       在执行编译时,系统报错,是因为mold使用C 20编写,需要相应的编译器进行编译器(g -10),而操作系统默认的编译器gcc版本较低(gcc 9.4.0)。接下来则先安装相应的编译器。

  1.  
    apt install g -10
  2.  
    g -10 --version
  3.  
    apt install gcc-10
  4.  
    gcc-10 --version

   再次编译,报如下错误;

  1.  
    root@hecs-131857:/home/rust/mold# make -j$(nproc) CXX=clang
  2.  
    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
  3.  
    In file included from main.cc:1:
  4.  
    ./elf/mold.h:30:10: fatal error: 'xxh3.h' file not found

    于是使用apt-file查询xxh3.h所在的库并进行安装。

  1.  
    apt-get install apt-file
  2.  
    apt-file update
  3.  
    apt-file search xxh3.h
  4.  
    apt install libxxhash-dev

    再次编译,还是报错,继续安装cmake,编译......。

  1.  
    (cd out/mimalloc; CFLAGS=-DMI_USE_ENVIRON=0 cmake -G'Unix Makefiles' ../../third-party/mimalloc)
  2.  
    /bin/sh: 1: cmake: not found
  3.  
    make: *** [Makefile:115: out/mimalloc/libmimalloc.a] Error 127
  4.  
     
  5.  
    apt install cmake

学新通

      最后进行安装。

  1.  
    root@hecs-131857:/home/rust/mold# make install
  2.  
    install -m 755 -d /usr/local/bin
  3.  
    install -m 755 mold /usr/local/bin
  4.  
    strip /usr/local/bin/mold
  5.  
    install -m 755 -d /usr/local/lib/mold
  6.  
    install -m 644 mold-wrapper.so /usr/local/lib/mold
  7.  
    strip /usr/local/lib/mold/mold-wrapper.so
  8.  
    install -m 755 -d /usr/local/share/man/man1
  9.  
    install -m 644 docs/mold.1 /usr/local/share/man/man1
  10.  
    ln -sf mold /usr/local/bin/ld.mold
  11.  
    ln -sf mold /usr/local/bin/ld64.mold

测试mold

接下来测试未使用mold和使用mold两种情况的编译速度。

测试1:未使用mold

  1.  
    root@hecs-131857:/home/rust/mp-share-it# cargo clean
  2.  
    root@hecs-131857:/home/rust/mp-share-it# cargo build
  3.  
    -------省略-------
  4.  
    Compiling tower v0.4.11
  5.  
    Compiling hyper v0.14.15
  6.  
    Compiling hyper-tls v0.5.0
  7.  
    Compiling axum v0.3.4
  8.  
    Compiling reqwest v0.11.7
  9.  
    Compiling mp-share-it v0.1.0 (/home/rust/mp-share-it)
  10.  
    Finished dev [unoptimized debuginfo] target(s) in 3m 20s

测试2:使用mold,(修改 ~/.cargo/config

  1.  
    [target.x86_64-unknown-linux-gnu]
  2.  
    linker = "clang"
  3.  
    rustflags = ["-C", "link-arg=-fuse-ld=/usr/local/bin/mold"]
  1.  
    root@hecs-131857:/home/rust/mp-share-it# cargo clean
  2.  
    root@hecs-131857:/home/rust/mp-share-it# cargo build
  3.  
    -------省略-------
  4.  
    Compiling tower v0.4.11
  5.  
    Compiling hyper v0.14.15
  6.  
    Compiling hyper-tls v0.5.0
  7.  
    Compiling axum v0.3.4
  8.  
    Compiling reqwest v0.11.7
  9.  
    Compiling mp-share-it v0.1.0 (/home/rust/mp-share-it)
  10.  
    Finished dev [unoptimized debuginfo] target(s) in 3m 04s

       经过一番操作折腾(走了不少弯路,踩了一些坑),通过测试的结果来看,mold对于加快Rust的编译速度还是有一定作用的(还有很大的优化空间)。

编译、安装sccache

  1.  
    cargo install sccache
  2.  
     
  3.  
    # 修改配置
  4.  
    vim ~/.cargo/config
  5.  
     
  6.  
    ## 添加如下配置
  7.  
    [build]
  8.  
    rustc-wrapper = "/root/.cargo/bin/sccache"

测试sccache

测试1:未使用sccache

  1.  
    root@hecs-131857:/home/rust/leetcode-rust# cargo build --release
  2.  
    -------省略-------
  3.  
    Compiling criterion-plot v0.4.4
  4.  
    Compiling criterion v0.3.5
  5.  
    Compiling leetcode-rust v0.1.0 (/home/rust/leetcode-rust)
  6.  
    Finished release [optimized] target(s) in 2m 43s
  7.  
    root@hecs-131857:/home/rust/leetcode-rust#

测试2:使用sccache

  1.  
    root@hecs-131857:/home/rust/leetcode-rust# cargo clean
  2.  
    root@hecs-131857:/home/rust/leetcode-rust# cargo build --release
  3.  
    -------省略-------
  4.  
    Compiling criterion-plot v0.4.4
  5.  
    Compiling criterion v0.3.5
  6.  
    Compiling leetcode-rust v0.1.0 (/home/rust/leetcode-rust)
  7.  
    Finished release [optimized] target(s) in 26.76s
  8.  
    root@hecs-131857:/home/rust/leetcode-rust#

      经过测试,使用sccache能大幅加快rust编译速度,节约了不少编译时间。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgchgbe
系列文章
更多 icon
同类精品
更多 icon
继续加载