当前位置: 首页 > news >正文

利用mold加快rust程序构建

我用rust的cargo build命令编译polars-cli时,用时达到14分钟,如下所示。

    Finished `dev` profile [unoptimized + debuginfo] target(s) in 14m 19s,

通过核对时间戳,发觉其中最后一步生成可执行文件花了6分钟。
于是向DeepSeek请教,我在编译polars-cli时,debug 总用时14分钟,最后一步生成可执行文件花了6分钟,有办法缩短吗?
他向我推荐了mold(现代链接器),它的主页是https://github.com/rui314/mold,
关于rust它这么说

If you are using Rust
Create .cargo/config.toml in your project directory with the following:

[target.‘cfg(target_os = “linux”)’]
linker = “clang”
rustflags = [“-C”, “link-arg=-fuse-ld=/path/to/mold”]
where /path/to/mold is an absolute path to the mold executable. In the example above, we use clang as a linker driver since it always accepts the -fuse-ld option. If your GCC is recent enough to recognize the option, you may be able to remove the linker = “clang” line.

[target.‘cfg(target_os = “linux”)’]
rustflags = [“-C”, “link-arg=-fuse-ld=mold”]
If you want to use mold for all projects, add the above snippet to ~/.cargo/config.toml.

我的gcc版本12.2足够新,把上述两行添加到/usr/local/cargo/config.toml,但是cargo build报错:

  = note: cc: error: unrecognized command-line option '-fuse-ld=/par/mold240/bin/mold'    

再问deepseek, 得到了答案,原来gcc不支持绝对路径,而我用上述clang的语法加了绝对路径。所以要把mold二进制文件的位置加入PATH搜索路径。
用简单的命令试验成功

export PATH=/par/mold240/bin:$PATH
gcc fib.c -o fibm -fuse-ld=mold

于是修改 /usr/local/cargo/config.toml 为如下

cat  /usr/local/cargo/config.toml
[source.crates-io]
replace-with = 'mirror'[source.mirror]
registry = "sparse+https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/"[registries.mirror]
index = "sparse+https://mirrors.tuna.tsinghua.edu.cn/crates.io-index/"[target.'cfg(target_os = "linux")']
rustflags = ["-C", "link-arg=-fuse-ld=mold"]

再次执行cargo build成功,全部重新编译用时

    Finished `dev` profile [unoptimized + debuginfo] target(s) in 4m 04s

构建速度提高了4倍,得到的二进制文件只比ld生成的略大。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.dtcms.com/a/257346.html

相关文章:

  • 苹果芯片macOS安装版Homebrew(亲测)
  • mac隐藏文件现身快捷键
  • 全局配置Axios后的api使用指南
  • Spring MVC极简入门:从@Request到Postman的全链路开发
  • Excel学习03
  • Redis 配置与优化
  • 左神算法之给定一个数组arr,返回其中的数值的差值等于k的子数组有多少个
  • 【软考高级系统架构论文】论边缘计算及其应用
  • 虚拟 DOM 与 Diff 算法:现代前端框架的核心机制
  • 首席运营官职责与工作内容概述
  • Anaconda虚拟环境
  • Linux 系统中,/usr/bin/ 和/bin/的区别?
  • Linux部署Sonic前后端(详细版)(腾讯云)
  • 从java到vue3:第一天
  • 高质量车牌识别数据集推荐(10k张图像)| 支持目标检测任务
  • 基于数据同步canal的使用详解
  • Transformer 中 QKV 流向全解析(含注意力机制箭头图示)
  • Spring--IOC容器的一些扩展属性
  • C++之二叉搜索树及其实现
  • 【开源工具】一键解决使用代理后无法访问浏览器网页问题 - 基于PyQt5的智能代理开关工具开发全攻略
  • 17、Rocket MQ快速实战以及核⼼概念详解
  • Vscode自定义代码快捷方式
  • MySQL-日志+事务
  • 海拔案例分享-门店业绩管理小程序
  • uniapp+vue3做小程序,获取容器高度
  • 短期项目与长期目标如何同时兼顾
  • 华为云 Flexus+DeepSeek 征文|增值税发票智能提取小工具:基于大模型的自动化信息解析实践
  • 【面板数据】上市公司投资者保护指数(2010-2023年)
  • 【达梦数据库】忘记SYSDBA密码处理方法-已适配
  • 第十六届蓝桥杯C/C++程序设计研究生组国赛 国二