Skip to content

cmd/compile: missing BCE optimization cases #64272

Open
@zigo101

Description

@zigo101

What version of Go are you using (go version)?

$ go version
go version go1.21.4 linux/amd64

Does this issue reproduce with the latest release?

Yes

What did you do?

https://github.com/golang/go/blob/go1.21.4/src/slices/slices.go#L346-L385

func Compact[S ~[]E, E comparable](s S) S {
	if len(s) < 2 {
		return s
	}
	i := 1
	for k := 1; k < len(s); k++ {
		if s[k] != s[k-1] {
			if i != k {
				s[i] = s[k] // Found IsInBounds
			}
			i++
		}
	}
	clear(s[i:]) // Found IsSliceInBounds
	return s[:i]
}

func CompactFunc[S ~[]E, E any](s S, eq func(E, E) bool) S {
	if len(s) < 2 {
		return s
	}
	i := 1
	for k := 1; k < len(s); k++ {
		if !eq(s[k], s[k-1]) {
			if i != k {
				s[i] = s[k] // Found IsInBounds
			}
			i++
		}
	}
	clear(s[i:]) // Found IsSliceInBounds
	return s[:i]
}

And

func DeleteFunc[S ~[]E, E any](s S, del func(E) bool) S {
	i := IndexFunc(s, del)
	if i == -1 {
		return s
	}
	// Don't start copying elements until we find one to delete.
	for j := i + 1; j < len(s); j++ {
		if v := s[j]; !del(v) { // Found IsInBounds
			s[i] = v // Found IsInBounds
			i++
		}
	}
	clear(s[i:]) // Found IsSliceInBounds
	return s[:i]
}

What did you expect to see?

No bound checks.

What did you see instead?

Many bound checks.

This problem might have been reported before, but I couldn't find it now.

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