traceTc and friends allocate even when tracing is disabled
While staring at Core from a typechecker module I noticed that traceTc
currently allocates. Specifically, Ticky-Ticky revealed that we allocate hundreds of millions of SDoc
thunks while typechecking Cabal
's Distribution.SPDX.LicenseId
module. For instance, in GHC.Tc.Utils.Zonk.commitFlexi
(called from GHC.Tc.Utils.Zonk.zonkTyVarOcc
) we allocated 24 million documents of each of the "Defaulting flexi tyvar to ..." documents. This is simply awful.
The problem is that we fail to inline the check for whether tracing is enabled or not. That is, we have:
trace :: SDoc -> IO ()
trace doc =
if tracingIsEnabled
then emitTraceMsg doc
else return ()
somewhereElse =
let thunk = ... :: SDoc
in trace thunk >> ...
What we rather want is to inline trace
and push the allocation into the conditional:
somewhereElse =
(if traceIsEnabled
then let thunk = ... :: SDoc
in emitTraceMsg thunk
else return ()) >> ...