From ac4815abb62458e15962120783de9d879f4c41d7 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Fri, 30 Aug 2024 17:38:37 +0000 Subject: [PATCH] Dont output logs when benchmarking In 11ab302087622b546d116fc9679f601667d18c4d we accidentally removed the `not(ldk_bench)` bound before outputting logs to stderr, which we restore here. Instead of simply ignoring logs in benchmarks, which we did previously, we instead format logs (in a way that LLVM will not optimize out). --- lightning/src/util/test_utils.rs | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index 7a0f75a57..7252162e2 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -1174,10 +1174,25 @@ impl TestLogger { impl Logger for TestLogger { fn log(&self, record: Record) { - *self.lines.lock().unwrap().entry((record.module_path, format!("{}", record.args))).or_insert(0) += 1; - *self.context.lock().unwrap().entry((record.module_path, record.peer_id, record.channel_id)).or_insert(0) += 1; - let pfx = format!("{} {} [{}:{}]", self.id, record.level.to_string(), record.module_path, record.line); - println!("{:<55}{}", pfx, record.args); + let s = format!("{:<55} {}", + format_args!("{} {} [{}:{}]", self.id, record.level.to_string(), record.module_path, record.line), + record.args + ); + #[cfg(ldk_bench)] { + // When benchmarking, we don't actually want to print logs, but we do want to format + // them. To make sure LLVM doesn't skip the above entirely we push it through a + // volitile read. This may not be super fast, but it shouldn't be worse than anything a + // user actually does with a log + let s_bytes = s.as_bytes(); + for i in 0..s.len() { + let _ = unsafe { core::ptr::read_volatile(&s_bytes[i]) }; + } + } + #[cfg(not(ldk_bench))] { + *self.lines.lock().unwrap().entry((record.module_path, format!("{}", record.args))).or_insert(0) += 1; + *self.context.lock().unwrap().entry((record.module_path, record.peer_id, record.channel_id)).or_insert(0) += 1; + println!("{}", s); + } } } -- 2.39.5