1

In a debug build, I want to check for OpenGL errors after almost every OpenGL call to ease debugging. 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?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Daan
  • 1,516
  • 12
  • 20
  • Or you could use an [existing OpenGL crate](https://crates.io/crates/glium) that already uses runtime and compile time checks. – Shepmaster Apr 11 '17 at 17:15
  • I literally just submitted an issue two hours ago proposing a `debug` macro: https://github.com/rust-lang/rust/issues/41223. It'd be good to show that there are other people who'd use that feature. – joshlf Apr 11 '17 at 18:24

1 Answers1

4

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 you are being paranoid you could #[cfg] two functions like

#[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...

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005