Uninvoked logging with interpolated string screws performance

I have a a hot loop function which merges 2 SparseVector of 1k items each.

With @benchmark I can see that adding
conditionWhichIsFalse && @warn lazy"msg $myIntVariable"
after the loop, makes the function 3 times slower (25us instead of 8us).

Converting to conditionWhichIsFalse && @warn "msg" myIntVariable"
solves it.

  1. Is the compiler not optimizing the function because it finds it too complex when adding the string interpolation?
  2. Any best practice or tool to mitigate or diagnose this kind of problems?

thanks

Without having checked, I think what you’re seeing is that the string from the interpolation might be constructed regardless. Or that the effects of creating a string through interpolation disable some other optimization the compiler is ordinarily able to do.

You could try using a LazyString?

1 Like

Sorry, I forgot to add it to my code (edited). I had tried with lazy".." but it didn’t help.

@warn lazy"msg $myIntVariable"