Skip to content

cmd/compile: expected to inline a closure with a superficially low inline budget #27703

@jech

Description

@jech

Using go1.11 linux/amd64.

I've got this function that logs debugging information:

	debugf := func(format string, v ...interface{}) {
		if l != nil {
			l.Printf(format, v...)
		}
	}

I was expecting the compiler to inline debugf and avoid consing the varargs when l is nil, but apparently that's not the case:

BenchmarkDebugf/No_Logger-8             30000000                45.4 ns/op            24 B/op          2 allocs/op
BenchmarkDebugf/Logger-8                 5000000               291 ns/op              38 B/op          3 allocs/op

Here's a complete test:

package debugf

import (
	"log"
	"testing"
	"io/ioutil"
)

func BenchmarkDebugf(b *testing.B) {
	var l *log.Logger

	debugf := func(format string, v ...interface{}) {
		if l != nil {
			l.Printf(format, v...)
		}
	}

	bench := func(b *testing.B) {
		for i := 0; i < b.N; i++ {
			debugf("i=%v", i)
		}
	}

	b.Run("No Logger", bench)
	l = log.New(ioutil.Discard, "", 0)
	b.Run("Logger", bench)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performance

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions