Skip to content

Commit ac4815a

Browse files
committed
Dont output logs when benchmarking
In 11ab302 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).
1 parent cdcab1c commit ac4815a

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

lightning/src/util/test_utils.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -1174,10 +1174,25 @@ impl TestLogger {
11741174

11751175
impl Logger for TestLogger {
11761176
fn log(&self, record: Record) {
1177-
*self.lines.lock().unwrap().entry((record.module_path, format!("{}", record.args))).or_insert(0) += 1;
1178-
*self.context.lock().unwrap().entry((record.module_path, record.peer_id, record.channel_id)).or_insert(0) += 1;
1179-
let pfx = format!("{} {} [{}:{}]", self.id, record.level.to_string(), record.module_path, record.line);
1180-
println!("{:<55}{}", pfx, record.args);
1177+
let s = format!("{:<55} {}",
1178+
format_args!("{} {} [{}:{}]", self.id, record.level.to_string(), record.module_path, record.line),
1179+
record.args
1180+
);
1181+
#[cfg(ldk_bench)] {
1182+
// When benchmarking, we don't actually want to print logs, but we do want to format
1183+
// them. To make sure LLVM doesn't skip the above entirely we push it through a
1184+
// volitile read. This may not be super fast, but it shouldn't be worse than anything a
1185+
// user actually does with a log
1186+
let s_bytes = s.as_bytes();
1187+
for i in 0..s.len() {
1188+
let _ = unsafe { core::ptr::read_volatile(&s_bytes[i]) };
1189+
}
1190+
}
1191+
#[cfg(not(ldk_bench))] {
1192+
*self.lines.lock().unwrap().entry((record.module_path, format!("{}", record.args))).or_insert(0) += 1;
1193+
*self.context.lock().unwrap().entry((record.module_path, record.peer_id, record.channel_id)).or_insert(0) += 1;
1194+
println!("{}", s);
1195+
}
11811196
}
11821197
}
11831198

0 commit comments

Comments
 (0)