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
Go version
Go 1.22
What did you do?
return make([]struct{}, 123)compiles to a call toruntime.makeslice: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):
I realize that the compiler could omit those
makeslicecalls 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:
/cc @golang/compiler @twitchyliquid64 @maisem