From 49ef2c652a51e63b837c33eac233b7c3903e92a4 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Tue, 23 Mar 2021 16:07:32 +0100 Subject: [PATCH] Cranelift: remove logging of vcode when the log level isn't debug or more (#2755) This logging step may be quite expensive, since logging has never been optimized at all. Removing it is a clear win in compile times on my machine for a large wasm module, for which parallel compilation is lowering from 6 seconds to 1.5 seconds. Co-authored-by: bjorn3 --- cranelift/codegen/src/machinst/compile.rs | 25 +++++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/cranelift/codegen/src/machinst/compile.rs b/cranelift/codegen/src/machinst/compile.rs index 9a00cee805..0aaee5e98f 100644 --- a/cranelift/codegen/src/machinst/compile.rs +++ b/cranelift/codegen/src/machinst/compile.rs @@ -5,7 +5,7 @@ use crate::machinst::*; use crate::settings; use crate::timing; -use log::debug; +use log::{debug, log_enabled, Level}; use regalloc::{allocate_registers_with_opts, Algorithm, Options, PrettyPrint}; /// Compile the given function down to VCode with allocated registers, ready @@ -29,10 +29,15 @@ where lower.lower(b)? }; - debug!( - "vcode from lowering: \n{}", - vcode.show_rru(Some(b.reg_universe())) - ); + // Creating the vcode string representation may be costly for large functions, so don't do it + // if the Debug level hasn't been statically (through features) or dynamically (through + // RUST_LOG) enabled. + if log_enabled!(Level::Debug) { + debug!( + "vcode from lowering: \n{}", + vcode.show_rru(Some(b.reg_universe())) + ); + } // Perform register allocation. let (run_checker, algorithm) = match vcode.flags().regalloc() { @@ -101,10 +106,12 @@ where vcode.replace_insns_from_regalloc(result); } - debug!( - "vcode after regalloc: final version:\n{}", - vcode.show_rru(Some(b.reg_universe())) - ); + if log_enabled!(Level::Debug) { + debug!( + "vcode after regalloc: final version:\n{}", + vcode.show_rru(Some(b.reg_universe())) + ); + } Ok(vcode) }