【问题标题】:How to compile and run an optimized Rust program with overflow checking enabled如何在启用溢出检查的情况下编译和运行优化的 Rust 程序
【发布时间】:2016-03-07 09:33:40
【问题描述】:

我正在编写一个计算量很大的程序,并且在调试模式下运行速度非常慢。

我的程序也受到整数溢出的困扰,因为我正在从 u8 数组中读取数据,而 u8 类型通过类型推断传播到意想不到的地方,而且 Rust 更喜欢溢出而不是将整数提升为更大的类型。

在发布模式下构建会禁用溢出检查:

cargo run --release

如何在启用优化运行时溢出检查的情况下构建 Rust 可执行文件?

【问题讨论】:

    标签: rust compiler-optimization integer-overflow


    【解决方案1】:

    可以在发布模式下编译with overflow checks enabled:

    [profile.release]
    overflow-checks = true
    

    这会将-C overflow-checks=true 传递给编译器。在早期版本的 Rust 中,overflow-checksdebug-assertions 开关的一部分,因此在某些情况下您可能需要使用它。

    其他时候,最简单的可能是build in test or dev mode with optimizations

    [profile.dev]
    opt-level = 3
    

    【讨论】:

    • 注意:要获得良好的加速但仍然有有意义的堆栈跟踪,您可能只想使用opt-level = 1。每个函数体都会得到优化,但通常没有(或很少)内联。当然没那么快……
    • 对于内联,有 #[inline(never)]#[inline(always)](当然,只有在您发现编译器没有做好工作时才谨慎使用,因为过度使用这些属性会适得其反)。
    猜你喜欢
    • 1970-01-01
    • 2018-05-30
    • 2022-08-02
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 2021-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多