gpt4 book ai didi

rust - 为什么 Rust 编译一个简单的程序比 gcc/clang 慢 5-10 倍?

转载 作者:行者123 更新时间:2023-11-29 07:41:54 24 4
gpt4 key购买 nike

跟进 Rust minimal compiled program size .

rustc hello.rs
> 600 ms

为什么 rustc 编译一个简单的 Hello World 比 gcc/clang 慢 5-10 倍?

Rust 使用 LLVM,因此它应该与 clang 不相上下。不管怎样,我们谈论的是一个只有三行代码的程序。

rustc hello.rs -C opt-level=0 -C prefer-dynamic
> 400 ms

gcc hello.c
> 60 ms

clang hello.c
> 110 ms

最佳答案

首先,我认为比较两个极其简单的程序的编译时间并期望结果更普遍地代表两种语言之间的编译时间没有多大意义。

也就是说,我确实希望 Rust 作为一种语言,它提供了更高级语言更常见的抽象级别,几乎没有运行时性能成本,在编译时必须在某种程度上为此付出代价。

此节选自the Rust FAQ :

Rust compilation seems slow. Why is that?

Code translation and optimizations. Rust provides high levelabstractions that compile down into efficient machine code, and thosetranslations take time to run, especially when optimizing.

But Rust’s compilation time is not as bad as it may seem, and there isreason to believe it will improve. When comparing projects of similarsize between C++ and Rust, compilation time of the entire project isgenerally believed to be comparable. The common perception that Rustcompilation is slow is in large part due to the differences in thecompilation model between C++ and Rust: C++’s compilation unit is thefile, while Rust’s is the crate, composed of many files. Thus, duringdevelopment, modifying a single C++ file can result in much lessrecompilation than in Rust. There is a major effort underway torefactor the compiler to introduce incremental compilation, which willprovide Rust the compile time benefits of C++’s model.

Aside from the compilation model, there are several other aspects ofRust’s language design and compiler implementation that affectcompile-time performance.

First, Rust has a moderately-complex type system, and must spend anon-negligible amount of compile time enforcing the constraints thatmake Rust safe at runtime.

Secondly, the Rust compiler suffers from long-standing technical debt,and notably generates poor-quality LLVM IR which LLVM must spend time“fixing”. There is hope that future MIR-based optimization andtranslation passes will ease the burden the Rust compiler places onLLVM.

Thirdly, Rust’s use of LLVM for code generation is a double-edgedsword: while it enables Rust to have world-class runtime performance,LLVM is a large framework that is not focused on compile-timeperformance, particularly when working with poor-quality inputs.

Finally, while Rust’s preferred strategy of monomorphising generics(ala C++) produces fast code, it demands that significantly more codebe generated than other translation strategies. Rust programmers canuse trait objects to trade away this code bloat by using dynamicdispatch instead.

关于rust - 为什么 Rust 编译一个简单的程序比 gcc/clang 慢 5-10 倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37362640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com