Skip to content

cmd/compile: avoid call to runtime.makeslice for zero-length types? #65685

@bradfitz

Description

@bradfitz

Go version

Go 1.22

What did you do?

return make([]struct{}, 123) compiles to a call to runtime.makeslice:

        LEAQ    type:struct {}(SB), AX
        MOVL    $123, BX
        MOVQ    BX, CX
        PCDATA  $1, $0
        NOP
        CALL    runtime.makeslice(SB)
        MOVL    $123, BX
        MOVQ    BX, CX
        ADDQ    $24, SP
        POPQ    BP
        RET

We see this show up in profiles (small, but non-zero) for calls to https://pkg.go.dev/tailscale.com/types/views#Slice.LenIter (which predates Go 1.22's range-over-int):

// LenIter returns a slice the same length as the v.Len().
// The caller can then range over it to get the valid indexes.
// It does not allocate.
func (v Slice[T]) LenIter() []struct{} { return make([]struct{}, len(v.ж)) }

I realize that the compiler could omit those makeslice calls when the width of the type being made is zero (as there's no actual allocation).

The compiler already does that when the slice length is zero:

// Recognise make([]T, 0) and replace it with a pointer to the zerobase
(StaticLECall {callAux} _ (Const(64|32) [0]) (Const(64|32) [0]) mem)
        && isSameCall(callAux, "runtime.makeslice")
        => (MakeResult (Addr <v.Type.FieldType(0)> {ir.Syms.Zerobase} (SB)) mem)

/cc @golang/compiler @twitchyliquid64 @maisem

Metadata

Metadata

Assignees

No one assigned

    Labels

    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

    Projects

    Status

    Todo

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions