From 48a136b3badd4ec239c2af9b0e4abc2ce0d2e321 Mon Sep 17 00:00:00 2001 From: Ingvar Stepanyan Date: Mon, 5 Aug 2019 21:05:20 +0100 Subject: [PATCH] Avoid using time API when we don't need it This avoids a syscall to the time API when the result is ignored later anyway. This allows to use the library with default options on wasm32-unknown-unknown, where the unimplemented syscall would panic otherwise. --- src/lib.rs | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f4179808..938fbd6d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -784,19 +784,25 @@ fn perform_reductions( try_alpha_reductions(png, &opts.alphas, eval); } +struct DeadlineImp { + start: Instant, + timeout: Duration, + print_message: AtomicBool, +} + /// Keep track of processing timeout pub(crate) struct Deadline { - start: Instant, - timeout: Option, - print_message: AtomicBool, + imp: Option, } impl Deadline { pub fn new(timeout: Option, verbose: bool) -> Self { Self { - start: Instant::now(), - timeout, - print_message: AtomicBool::new(verbose), + imp: timeout.map(|timeout| DeadlineImp { + start: Instant::now(), + timeout, + print_message: AtomicBool::new(verbose), + }) } } @@ -804,11 +810,11 @@ impl Deadline { /// /// If the verbose option is on, it also prints a timeout message once. pub fn passed(&self) -> bool { - if let Some(timeout) = self.timeout { - let elapsed = self.start.elapsed(); - if elapsed > timeout { - if self.print_message.load(Ordering::Relaxed) { - self.print_message.store(false, Ordering::Relaxed); + if let Some(imp) = &self.imp { + let elapsed = imp.start.elapsed(); + if elapsed > imp.timeout { + if imp.print_message.load(Ordering::Relaxed) { + imp.print_message.store(false, Ordering::Relaxed); eprintln!("Timed out after {} second(s)", elapsed.as_secs()); } return true;