Skip to content

cmd/compile: closures in generic functions capture type dictionaries unconditionally #78370

Description

@mcy

Consider the following program:

package p

func f[T any]() func() {
    return func() {}
}

func g() func() {
    return func() {}
}

var x = f[int]
var y = g

One expects f and g to be identical, because they have identical bodies. However, because f is generic, the type dictionary for its type arguments is captured, meaning that f's closure escapes to the heap.

        TEXT    .g(SB)
        LEAQ    .g.func1·f(SB), AX
        RET

        TEXT    .f[int](SB)
        PUSHQ   BP
        MOVQ    SP, BP
        SUBQ    $16, SP
        NOP
        LEAQ    type:noalg.struct { F uintptr; X0 *[0]uintptr }(SB), AX
        CALL    runtime.newobject(SB)
        LEAQ    .f[int].f[go.shape.int].func1(SB), CX
        MOVQ    CX, (AX)
        LEAQ    ..dict.f[int](SB), CX
        MOVQ    CX, 8(AX)
        ADDQ    $16, SP
        POPQ    BP
        RET

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Todo

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions