Skip to content

cmd/compile: low function inline cost #54407

@subtle-byte

Description

@subtle-byte

What version of Go are you using (go version)?

$ go version
go version go1.19 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

I write the following code:

package main
import "encoding/binary"
func main() {
}

func A(b []byte) uint64 {
	return binary.LittleEndian.Uint64(b)
}

// Copy-paste of binary.LittleEndian.Uint64
func Uint64(b []byte) uint64 {
	_ = b[7] // bounds check hint to compiler; see golang.org/issue/14808
	return uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 |
		uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
}

func B(b []byte) uint64 {
	return Uint64(b)
}

Then I run the command go build -gcflags -m=2 . and I see:

can inline A with cost 5 as: func([]byte) uint64 { return binary.littleEndian.Uint64(binary.LittleEndian, b) }
inlining call to binary.littleEndian.Uint64
can inline B with cost 63 as: func([]byte) uint64 { return Uint64(b) }
inlining call to Uint64

So A calls Uint64 from another module and B calls Uint64 from the same module, and A and B have different costs.

What did you expect to see?

I expected to see the same cost no matter where the called function is located.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions