golang/go

cmd/compile: unnecessary bounds check when slicing after using `slices.Grow`

Open

#73.145 aberto em 3 de abr. de 2025

Ver no GitHub
 (2 comments) (1 reaction) (0 assignees)Go (19.008 forks)batch import
BugReportNeedsInvestigationPerformancecompiler/runtimehelp wanted

Métricas do repositório

Stars
 (133.883 stars)
Métricas de merge de PR
 (Nenhuma PRs mesclada em 30d)

Description

Go version

go version go1.24.1 linux/amd64

Output of go env in your module/workspace:

-

What did you do?

package pkg

import "slices"

func Foo() {
	var xs []int
	xs = slices.Grow(xs, 4)[:len(xs)+4]
}

What did you see happen?

Slicing after growing inserts a check for the capacity and a conditional call to runtime.panicSliceAcap

What did you expect to see?

The compiler should be able to tell that we just guaranteed that the slice's capacity is at least len(xs)+4 and shouldn't insert another check.

Guia do colaborador