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

编译缓存工具 sccache 效果对比

概要

本文介绍了编译缓存工具 sccache 的安装和配置过程。

sccache 的效果大致如下:

项目语言无缓存使用缓存
RedisC48.263s30.012s
ripgrepRust17.99s5.95s

背景

与脚本型语言不同,编译型语言需要经过编译之后,才能运行起来。而对于一些超大型项目来说,编译一次代码可能会消耗很长的时间。除了组件化拆分等方法,比较立竿见影的是使用编译缓存工具来缓存部分的编译中间结果,这样在下一次编译时就可以大大加快流程速度。

久经考验的编译缓存工具有 ccache 等。在这篇文档中,我们主要认识一下 sccache —— 一款由 mozilla 开发的 ccache-like 工具,除了 C/C++之外,还为 Rust 等其他技术提供了编译缓存支持。

安装

参照 官方文档,安装过程非常简单:

cargo install sccache --locked

服务器启停

sccache 使用 client-server 模式进行工作。其服务器默认监听 127.0.0.1:4226

要启动服务器,可以使用命令:sccache --start-server。相应的,使用 sccache --stop-server 可以停止服务器。

注意,如果在 WSL 下遇到无法启动服务器的问题,可以参照 sccache 无法启动问题 进行解决。

缓存效果

C/C++

我们以 Redis 项目为例,检查 sccache 的提升效果。

无缓存编译

首先,安装编译 redis 需要的依赖并准备编译环境:

sudo apt install build-essential tcl libjemalloc-dev libssl-dev lua5.3 liblua5.3-dev
git clone https://github.com/redis/redis.git --depth=1 # commit dee0d11a74dbb8efcec8148bd2ff43d203b48931
cd redis/
make clean && make distclean

然后,编译 redis 查看

$ time make -j16 &> /dev/nullreal    0m48.263s
user    5m23.822s
sys     0m34.994s

可以看到,在无缓存的场景下,编译 redis 所用的时间是:48.263s

缓存编译

接下来,我们使用验证缓存效果。由于缓存需要先编译一次后才能生成,所以接下来我们将一共编译两次。

首先查看 sccache 统计,可以看到是没有缓存的:

$ sccache -s
Compile requests                      0
Compile requests executed             0
Cache hits                            0
Cache misses                          0
Cache hits rate                       -
Cache timeouts                        0
Cache read errors                     0
Forced recaches                       0
Cache write errors                    0
Cache errors                          0
Compilations                          0
Compilation failures                  0
Non-cacheable compilations            0
Non-cacheable calls                   0
Non-compilation calls                 0
Unsupported compiler calls            0
Average cache write               0.000 s
Average compiler                  0.000 s
Average cache read hit            0.000 s
Failed distributed compilations       0
Cache location                  Local disk: "/home/focksor/.cache/sccache"
Use direct/preprocessor mode?   yes
Version (client)                0.10.0
Max cache size                       10 GiB

然后,设置使用 sccache 并进行一次编译:

$ export CC="sccache gcc"
$ export CXX="sccache g++"
$ (make clean && make distclean) &> /dev/null
$ time make -j16 &> /dev/nullreal    0m58.897s
user    2m53.564s
sys     0m20.504s

现在可以看到 sccache 已经生成了一些缓存:

$ sccache -s
Compile requests                    584
Compile requests executed           355
Cache hits                            7
Cache hits (C/C++)                    7
Cache misses                        334
Cache misses (C/C++)                334
Cache hits rate                    2.05 %
Cache hits rate (C/C++)            2.05 %
Cache timeouts                        0
Cache read errors                     0
Forced recaches                       0
Cache write errors                    0
Cache errors                          9
Cache errors (C/C++)                  9
Compilations                        334
Compilation failures                  5
Non-cacheable compilations            0
Non-cacheable calls                 133
Non-compilation calls                96
Unsupported compiler calls            0
Average cache write               0.002 s
Average compiler                  0.593 s
Average cache read hit            0.000 s
Failed distributed compilations       0Non-cacheable reasons:
-MM                                 126
-E                                    6
argument parse                        1Cache location                  Local disk: "/home/focksor/.cache/sccache"
Use direct/preprocessor mode?   yes
Version (client)                0.10.0
Cache size                          346 MiB
Max cache size                       10 GiB

缓存已经生成,我们再进行一次编译以验证编译缓存效果:

$ (make clean && make distclean) &> /dev/null
$ time make -j16 &> /dev/nullreal    0m30.012s
user    2m51.748s
sys     0m21.422s

可以看到,生成缓存后,再次编译时速度有了非常大的提升!查看 sccache 的统计信息,可以看到编译过程响应的缓存命中数据也有上升:

$ sccache -s
Compile requests                   1159
Compile requests executed           707
Cache hits                          345
Cache hits (C/C++)                  345
Cache misses                        334
Cache misses (C/C++)                334
Cache hits rate                   50.81 %
Cache hits rate (C/C++)           50.81 %
Cache timeouts                        0
Cache read errors                     0
Forced recaches                       0
Cache write errors                    0
Cache errors                         18
Cache errors (C/C++)                 18
Compilations                        334
Compilation failures                 10
Non-cacheable compilations            0
Non-cacheable calls                 265
Non-compilation calls               187
Unsupported compiler calls            0
Average cache write               0.002 s
Average compiler                  0.593 s
Average cache read hit            0.002 s
Failed distributed compilations       0Non-cacheable reasons:
-MM                                 251
-E                                   12
argument parse                        2Cache location                  Local disk: "/home/focksor/.cache/sccache"
Use direct/preprocessor mode?   yes
Version (client)                0.10.0
Cache size                          346 MiB
Max cache size                       10 GiB

Rust

在 Rust 项目上,我们使用 ripgrep 验证缓存效果。

首先准备项目:

git clone https://github.com/BurntSushi/ripgrep.git --depth=1 # commit id: 119a58a400ea948c2d2b0cd4ec58361e74478641
cd ripgrep/
cargo clean

无缓存编译

$ cargo build --release 2>&1 | tail -1Finished `release` profile [optimized + debuginfo] target(s) in 17.99s

缓存编译

首先设置使用 sccache 进行缓存编译,并构建一次编译以创建缓存:

export RUSTC_WRAPPER=/home/focksor/.cargo/bin/sccache
$ cargo cleanRemoved 313 files, 201.7MiB total
$ cargo build --release 2>&1 | tail -1Finished `release` profile [optimized + debuginfo] target(s) in 23.68s
$ sccache -s
Compile requests                     47
Compile requests executed            33
Cache hits                            0
Cache misses                         32
Cache misses (Rust)                  32
Cache hits rate                    0.00 %
Cache hits rate (Rust)             0.00 %
Cache timeouts                        0
Cache read errors                     0
Forced recaches                       0
Cache write errors                    0
Cache errors                          0
Compilations                         32
Compilation failures                  1
Non-cacheable compilations            0
Non-cacheable calls                  13
Non-compilation calls                 1
Unsupported compiler calls            0
Average cache write               0.002 s
Average compiler                  1.341 s
Average cache read hit            0.000 s
Failed distributed compilations       0Non-cacheable reasons:
crate-type                            7
missing input                         4
-                                     2Cache location                  Local disk: "/home/focksor/.cache/sccache"
Use direct/preprocessor mode?   yes
Version (client)                0.10.0
Cache size                           31 MiB
Max cache size                       10 GiB

然后,使用缓存重新构建一次编译以查看加速效果:

$ cargo cleanRemoved 313 files, 201.7MiB total
$ cargo build --release 2>&1 | tail -1Finished `release` profile [optimized + debuginfo] target(s) in 5.95s
$ sccache -s
Compile requests                     91
Compile requests executed            66
Cache hits                           32
Cache hits (Rust)                    32
Cache misses                         32
Cache misses (Rust)                  32
Cache hits rate                   50.00 %
Cache hits rate (Rust)            50.00 %
Cache timeouts                        0
Cache read errors                     0
Forced recaches                       0
Cache write errors                    0
Cache errors                          0
Compilations                         32
Compilation failures                  2
Non-cacheable compilations            0
Non-cacheable calls                  23
Non-compilation calls                 2
Unsupported compiler calls            0
Average cache write               0.002 s
Average compiler                  1.341 s
Average cache read hit            0.000 s
Failed distributed compilations       0Non-cacheable reasons:
crate-type                           14
missing input                         6
-                                     3Cache location                  Local disk: "/home/focksor/.cache/sccache"
Use direct/preprocessor mode?   yes
Version (client)                0.10.0
Cache size                           31 MiB
Max cache size                       10 GiB

资料

  • Ccache — Compiler cache
  • mozilla/sccache: Sccache is a ccache-like tool. It is used as a compiler wrapper and avoids compilation when possible. Sccache has the capability to utilize caching in remote storage environments, including various cloud storage options, or alternatively, in local storage.
  • redis/redis: For developers, who are building real-time data-driven applications, Redis is the preferred, fastest, and most feature-rich cache, data structure server, and document and vector query engine.
  • BurntSushi/ripgrep: ripgrep recursively searches directories for a regex pattern while respecting your gitignore

文章转载自:

http://nARWYhd2.mcbpc.cn
http://MUTUUkEJ.mcbpc.cn
http://G500ZMVv.mcbpc.cn
http://kSlUfPgD.mcbpc.cn
http://rs6z7bs9.mcbpc.cn
http://BmGsRp50.mcbpc.cn
http://LTu9ssyJ.mcbpc.cn
http://qBwfpIqC.mcbpc.cn
http://3jF81cHT.mcbpc.cn
http://AllTuIyC.mcbpc.cn
http://65DnLxbN.mcbpc.cn
http://cSK5yUpv.mcbpc.cn
http://V9tX4A7N.mcbpc.cn
http://9VK9drVl.mcbpc.cn
http://bhWBQU5b.mcbpc.cn
http://9rJ5UCqo.mcbpc.cn
http://c902uBVo.mcbpc.cn
http://7PJuIBFY.mcbpc.cn
http://M475hc27.mcbpc.cn
http://bQo2p9sb.mcbpc.cn
http://ZITA7uUe.mcbpc.cn
http://9os5pb0M.mcbpc.cn
http://CYLP0suq.mcbpc.cn
http://aUD9ehb8.mcbpc.cn
http://1PlISu0R.mcbpc.cn
http://ljoFxUpf.mcbpc.cn
http://G8lVcwJq.mcbpc.cn
http://fqzKj8th.mcbpc.cn
http://EPY7vQpz.mcbpc.cn
http://LDONewJI.mcbpc.cn
http://www.dtcms.com/a/373787.html

相关文章:

  • 【MFC典型类和函数:CString的字符串魔法与Afx全局函数的便利店】
  • 【MFC】对话框属性:字体 (Font Name) 和 大小 (Font Size)
  • 搜索框设计实用指南:规范、模板与工具全解析
  • Python调用MCP:无需重构,快速为现有应用注入AI与外部服务能力!
  • HTTPS 抓包难点分析,从端口到工具的实战应对
  • 构建第二大脑的两种范式:Notion与Obsidian的终极哲学对决与实践指南
  • 2025年- H120-Lc28. 找出字符串中第一个匹配项的下标(数组)--Java版
  • 网络编程;TCP/IP协议,和 网络编程相关概念;字节序转换;0908
  • 深度剖析Windows PE程序安全:IAT HOOK与DLL劫持的攻防之道
  • ollama笔记
  • C++语言编程规范-函数
  • 如何在 FastAPI 中优雅地模拟多模块集成测试?
  • 阿德莱德大学Nat. Commun.:盐模板策略实现废弃塑料到单原子催化剂的高值转化,推动环境与能源催化应用
  • 新型APT组织“嘈杂熊“针对哈萨克斯坦能源部门发起网络间谍活动
  • Windows 11 安装 Maven、配置国内镜像
  • 软件测试|STATIC 代码静态验证工具 C/C++ 工具链设置指南
  • JavaScript 行为型设计模式详解
  • 强化学习:从 Q-Learning 到 Deep Q-Network
  • 摄像头模块在运动相机中的特殊应用
  • 雷卯针对米尔MYC-YG2UL开发板防雷防静电方案
  • 专为石油和天然气检测而开发的基于无人机的OGI相机
  • pytest(2):测试用例查找原理详解(从默认规则到高级钩子定制)
  • Java 服务接口中解决跨域(CORS,Cross-Origin Resource Sharing)问题
  • 【VLNs篇】09:NavA³—理解任意指令,导航任意地点,找到任意物体
  • JS实现丝滑文字滚动
  • 小程序获取手机号完整流程 弹出框获取电话号码
  • Claude API 到智谱 API 迁移全流程教程(含兼容性对比)
  • 玩转Docker | 使用Docker部署Umbrel操作系统
  • 一客一策:Data Agent 如何重构大模型时代的智能营销
  • 一次用户请求的网络之旅