by mdaniel · 1y ago · 0 comments
dlg.Printf("the thing.Something is %+v", thing.Something())
since surely golang will still call Something, and still call PrintfAnd don't misunderstand me: it's a grave pet peeve of mine to do action inside the args to Printf but I can tell you it's a very common [anti-]pattern so I'd presume your users will want a story for how that interacts with the "disappears completely" claim
If we consider this example:
func risky() error {
return fmt.Errorf("unexpected error")
}
func risky2() error{
return fmt.Errorf("also an error")
}
func main() {
fmt.Printf("something failed: %v", risky())
dlg.Printf("something failed: %v", risky2())
}
And look at the disassembly: 0x10009f354 d503201f NOOP ; Dead code of dlg.Printf
0x10009f358 b0000000 ADRP 4096(PC), R0 ; Load "also an error" string address
0x10009f35c 9124ec00 ADD $2363, R0, R0 ; Calculate exact string address
0x10009f360 d28001a1 MOVD $13, R1 ; Set string length=13 (R1)
0x10009f364 aa1f03e2 MOVD ZR, R2 ; Clear R2
0x10009f368 aa1f03e3 MOVD ZR, R3 ; Clear R3
0x10009f36c aa0303e4 MOVD R3, R4 ; Clear R4
0x10009f370 97ffd890 CALL fmt.Errorf(SB) ; Call fmt.Errorf
What disappears is the logging work (formatting, interface/slice plumbing, I/O) and the call itself.
Go cannot eliminate calls to functions inside of Printf because they could produce side-effects.
Eliminating functions calls like this would be very expensive to do and would clashes with Go's focus on fast compilation times.I'll add a section to the README that explains this. Thanks for pointing it out.
package main
import (
"fmt"
"github.com/vvvvv/dlg"
)
func risky() error {
_, err := fmt.Printf("unexpected error\n")
return err
}
func main() {
dlg.Printf("something failed: %s", risky())
risky()
}
prints "unexpected error" twiceETA: Presumably if there was at least an option for passing a closure that would provide a way to address this? Since the closure will only uselessly compile, and not actually uselessly run?
I'd appreciate any feedback on whether these changes make the behavior more transparent and less misleading.