Browse Source

avoid `...any` allocs in debug logs in hot loops

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)
Daniel Martí 7 months ago
parent
commit
ec68fc6750
1 changed files with 9 additions and 3 deletions
  1. 9 3
      main.go

+ 9 - 3
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 {