Remove debug prints code from release builds.
If building release mode, remove debug code. Now, the `debug_print!` macro resolves to nothing in release build. In debug build, don't check the environment for each print. This has a huge overhead. The environment is only checked once and next checks are made against a simple boolean value.
This commit is contained in:
parent
e6fae1c679
commit
5789169ceb
4 changed files with 55 additions and 16 deletions
41
saphyr/src/debug.rs
Normal file
41
saphyr/src/debug.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
//! Debugging helpers.
|
||||||
|
//!
|
||||||
|
//! Debugging is governed by two conditions:
|
||||||
|
//! 1. The build mode. Debugging code is not emitted in release builds and thus not available.
|
||||||
|
//! 2. The `YAMLALL_DEBUG` environment variable. If built in debug mode, the program must be fed
|
||||||
|
//! the `YAMLALL_DEBUG` variable in its environment. While debugging code is present in debug
|
||||||
|
//! build, debug helpers will only trigger if that variable is set when running the program.
|
||||||
|
|
||||||
|
// If a debug build, use stuff in the debug submodule.
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
pub use debug::enabled;
|
||||||
|
|
||||||
|
// Otherwise, just export dummies for publicly visible functions.
|
||||||
|
/// Evaluates to nothing.
|
||||||
|
#[cfg(not(debug_assertions))]
|
||||||
|
macro_rules! debug_print {
|
||||||
|
($($arg:tt)*) => {{}};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
#[macro_use]
|
||||||
|
#[allow(clippy::module_inception)]
|
||||||
|
mod debug {
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
|
/// If debugging is [`enabled`], print the format string on the error output.
|
||||||
|
macro_rules! debug_print {
|
||||||
|
($($arg:tt)*) => {{
|
||||||
|
if $crate::debug::enabled() {
|
||||||
|
eprintln!($($arg)*)
|
||||||
|
}
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Return whether debugging features are enabled in this execution.
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
pub fn enabled() -> bool {
|
||||||
|
static ENABLED: OnceLock<bool> = OnceLock::new();
|
||||||
|
*ENABLED.get_or_init(|| std::env::var("YAMLRUST2_DEBUG").is_ok())
|
||||||
|
}
|
||||||
|
}
|
|
@ -46,6 +46,8 @@
|
||||||
extern crate linked_hash_map;
|
extern crate linked_hash_map;
|
||||||
|
|
||||||
pub(crate) mod char_traits;
|
pub(crate) mod char_traits;
|
||||||
|
#[macro_use]
|
||||||
|
pub(crate) mod debug;
|
||||||
pub mod emitter;
|
pub mod emitter;
|
||||||
pub mod parser;
|
pub mod parser;
|
||||||
pub mod scanner;
|
pub mod scanner;
|
||||||
|
|
|
@ -413,9 +413,8 @@ impl<T: Iterator<Item = char>> Parser<T> {
|
||||||
fn state_machine(&mut self) -> ParseResult {
|
fn state_machine(&mut self) -> ParseResult {
|
||||||
// let next_tok = self.peek_token().cloned()?;
|
// let next_tok = self.peek_token().cloned()?;
|
||||||
// println!("cur_state {:?}, next tok: {:?}", self.state, next_tok);
|
// println!("cur_state {:?}, next tok: {:?}", self.state, next_tok);
|
||||||
if std::env::var("YAMLRUST_DEBUG").is_ok() {
|
debug_print!("\n\x1B[;33mParser state: {:?} \x1B[;0m", self.state);
|
||||||
eprintln!("\n\x1B[;33mParser state: {:?} \x1B[;0m", self.state);
|
|
||||||
}
|
|
||||||
match self.state {
|
match self.state {
|
||||||
State::StreamStart => self.stream_start(),
|
State::StreamStart => self.stream_start(),
|
||||||
|
|
||||||
|
|
|
@ -364,12 +364,11 @@ impl<T: Iterator<Item = char>> Iterator for Scanner<T> {
|
||||||
}
|
}
|
||||||
match self.next_token() {
|
match self.next_token() {
|
||||||
Ok(Some(tok)) => {
|
Ok(Some(tok)) => {
|
||||||
if std::env::var("YAMLRUST_DEBUG").is_ok() {
|
debug_print!(
|
||||||
eprintln!(
|
|
||||||
" \x1B[;32m\u{21B3} {:?} \x1B[;36m{:?}\x1B[;m",
|
" \x1B[;32m\u{21B3} {:?} \x1B[;36m{:?}\x1B[;m",
|
||||||
tok.1, tok.0
|
tok.1,
|
||||||
|
tok.0
|
||||||
);
|
);
|
||||||
}
|
|
||||||
Some(tok)
|
Some(tok)
|
||||||
}
|
}
|
||||||
Ok(tok) => tok,
|
Ok(tok) => tok,
|
||||||
|
@ -565,13 +564,11 @@ impl<T: Iterator<Item = char>> Scanner<T> {
|
||||||
}
|
}
|
||||||
self.skip_to_next_token()?;
|
self.skip_to_next_token()?;
|
||||||
|
|
||||||
if std::env::var("YAMLRUST_DEBUG").is_ok() {
|
debug_print!(
|
||||||
eprintln!(
|
|
||||||
" \x1B[38;5;244m\u{2192} fetch_next_token after whitespace {:?} {:?}\x1B[m",
|
" \x1B[38;5;244m\u{2192} fetch_next_token after whitespace {:?} {:?}\x1B[m",
|
||||||
self.mark,
|
self.mark,
|
||||||
self.ch()
|
self.ch()
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
self.stale_simple_keys()?;
|
self.stale_simple_keys()?;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue