From ec68fc67503d63a4a63d5714d9a0a6c8ee7ffc65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sat, 2 Jul 2022 14:03:53 +0100 Subject: [PATCH] avoid `...any` allocs in debug logs in hot loops MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit These lines get executed for every identifier in every package in each Go build, so one allocation per log.Printf call can quickly add up to millions of allocations across a build. Until https://go.dev/issue/53465 is fixed, the best way to avoid the escaping due to `...any` is to not perform the function call at all. name old time/op new time/op delta Build-16 10.5s ± 1% 10.5s ± 2% ~ (p=0.604 n=9+10) name old bin-B new bin-B delta Build-16 5.52M ± 0% 5.52M ± 0% ~ (all equal) name old cached-time/op new cached-time/op delta Build-16 506ms ±13% 500ms ± 7% ~ (p=0.739 n=10+10) name old mallocs/op new mallocs/op delta Build-16 31.7M ± 0% 30.1M ± 0% -5.33% (p=0.000 n=10+9) name old sys-time/op new sys-time/op delta Build-16 5.70s ± 5% 5.78s ± 6% ~ (p=0.278 n=9+10) --- main.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index e294d15..96f6c8e 100644 --- a/main.go +++ b/main.go @@ -672,7 +672,9 @@ func transformAsm(args []string) ([]string, error) { } newName := hashWithPackage(curPkg, name) - log.Printf("asm name %q hashed with %x to %q", name, curPkg.GarbleActionID, newName) + if flagDebug { // TODO(mvdan): remove once https://go.dev/issue/53465 if fixed + log.Printf("asm name %q hashed with %x to %q", name, curPkg.GarbleActionID, newName) + } buf.WriteString(newName) } @@ -1680,7 +1682,9 @@ func (tf *transformer) transformGo(file *ast.File) *ast.File { panic("could not find for " + name) } node.Name = hashWithStruct(strct, name) - log.Printf("%s %q hashed with struct fields to %q", debugName, name, node.Name) + if flagDebug { // TODO(mvdan): remove once https://go.dev/issue/53465 if fixed + log.Printf("%s %q hashed with struct fields to %q", debugName, name, node.Name) + } return true case *types.TypeName: @@ -1708,7 +1712,9 @@ func (tf *transformer) transformGo(file *ast.File) *ast.File { node.Name = hashWithPackage(lpkg, name) // TODO: probably move the debugf lines inside the hash funcs - log.Printf("%s %q hashed with %x… to %q", debugName, name, hashToUse[:4], node.Name) + if flagDebug { // TODO(mvdan): remove once https://go.dev/issue/53465 if fixed + log.Printf("%s %q hashed with %x… to %q", debugName, name, hashToUse[:4], node.Name) + } return true } post := func(cursor *astutil.Cursor) bool {