简体   繁体   English

如何创建用于调试的Rust函数或宏,使其在发布版本中得到优化?

[英]How do I create a Rust function or macro for debugging that gets optimized away in a release build?

In a debug build, I want to check for OpenGL errors after almost every OpenGL call to ease debugging. 在调试版本中,我想在几乎每个OpenGL调用之后检查OpenGL错误,以简化调试。 As this is a costly operation, I don't want to do it in a release build. 由于这是一项昂贵的操作,因此我不想在发行版本中进行。 Right now I'm using functions like: 现在我正在使用类似的功能:

pub fn debug_panic_on_errors() {
    if cfg!(debug_assertions) {
        get_errors().unwrap();
    }
}

Am I correct in assuming that this method will always be optimized away completely? 我是否可以始终完全优化此方法,这是否正确? Is there a better, more future-proof way? 有没有更好,更适应未来的方法?

In release mode the function will be expanded to if false { … } which is very trivial to optimize away, so yes you could just use it as-is. 在发布模式下,该功能将扩展为if false { … } ,这对于优化起来非常简单,因此是的,您可以按原样使用它。


If you are being paranoid you could #[cfg] two functions like 如果您偏执狂,可以#[cfg]两个函数,例如

#[cfg(debug_assertions)]
pub fn debug_panic_on_errors() { 
    get_errors().unwrap();
}

#[cfg(not(debug_assertions))]
pub fn debug_panic_on_errors() {
}

so that the outcome is chosen during parsing, to ensure we don't rely on the optimizer. 以便在解析过程中选择结果,以确保我们不依赖优化器。 But I don't really recommend this... 但是我真的不推荐这个...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 rust-gdb 进行调试时,如何进入在返回值中调用的函数? - How do I step into a function called in a return value when debugging with rust-gdb? 发布构建调试问题 - Release build debugging issue 如何强行导致仅针对构建版本的调试代码的编译错误? - How to forcefully cause compilation error of debugging code only for the build release? C#调试版本可以,但是由于优化,版本发布失败 - C# Debug build ok, but Release build fail due to optimized 通过调试发布版本而不是调试版本,我可能会遇到哪些问题? - What problems might I miss by debugging a release build instead of debug build? 调试优化的版本会导致程序的行为有所不同吗? - Can debugging an optimized build cause a program to behave differently? 在客户端机器上调试发布版本 - Debugging release build on a client's machine 如果我有一个C函数并且我用gdb调试它,我如何找到函数的返回值? - If I have a C function and I'm debugging it with gdb, how do I find the return value of a function? 是否有自动方法来删除发布版本的调试方法? - Is there an automatic way to remove debugging methods for a release build? 如何强制链接器在调试期间包含我需要的函数? - How do I force the linker to include a function I need during debugging?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM