Skip to content

cmd/compile: inlining budget excludes cross-package calls #22194

@mdempsky

Description

@mdempsky

In the example below, p.G and q.H are identical, except that p.G is in the same package as p.F, yet cmd/compile decides that q.H is inlineable whereas p.G is not.

This is because cmd/compile currently only computes Inlcost for locally defined functions. Calls to functions in external packages are effectively treated as free.

$ go tool compile -m p.go
p.go:3:6: can inline F
p.go:13:3: inlining call to F

$ go tool compile -m q.go
q.go:5:6: can inline H
q.go:6:5: inlining call to p.F

$ cat p.go
package p

func F() {
    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
}

func G() {
    F()
    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
}

$ cat q.go
package q

import "./p"

func H() {
    p.F()
    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    print(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions