New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: BCE optimizes less for string than slice #27585

Open
go101 opened this Issue Sep 9, 2018 · 2 comments

Comments

Projects
None yet
3 participants
@go101

go101 commented Sep 9, 2018

Please answer these questions before submitting your issue. Thanks!

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

go version go1.11 linux/amd64

Does this issue reproduce with the latest release?

yes

What did you do?

package main

type T = string
// type T = []int

func NumSameBytes_1(x, y T) int {
	if len(x) > len(y) {
		x, y = y, x
	}
	for i := 0; i < len(x); i++ {
		if x[i] != y[i] { // y[i] needs bound check
			return i
		}
	}
	return len(x)
}

func NumSameBytes_2(x, y T) int {
	if len(x) > len(y) {
		x, y = y, x
	}
	
	y = y[:len(x)] // this line doesn't work
	for i := 0; i < len(x); i++ {
		if x[i] != y[i] { // y[i] still needs bound check (line 25)
			return i
		}
	}
	return len(x)
}

func NumSameBytes_3(x, y T) int {
	if len(x) > len(y) {
		x, y = y, x
	}
	if len(x) <= len(y) { // this line works
		for i := 0; i < len(x); i++ {
			if x[i] != y[i] { // bound check elimated for y[i]
				return i
			}
		}
	}
	return len(x)
}

func main() {}

What did you expect to see?

For T is either string or []int, the bounds check for y[i] at line 25 should be eliminated.
But this is only true for T is []int.

What did you see instead?

When T is string, the bounds check for y[i] at line 25 is still needed.

$ go build -gcflags="-d=ssa/check_bce/debug=1" main.go
# command-line-arguments
./main.go:11:15: Found IsInBounds
./main.go:23:7: Found IsSliceInBounds
./main.go:25:15: Found IsInBounds

@go101 go101 changed the title from cmd/go: BCE doesn't optimize less for string than slice to cmd/go: BCE optimizes less for string than slice Sep 9, 2018

@josharian josharian changed the title from cmd/go: BCE optimizes less for string than slice to cmd/compile: BCE optimizes less for string than slice Sep 10, 2018

@josharian

This comment has been minimized.

Contributor

josharian commented Sep 10, 2018

cc @rasky

@josharian josharian added this to the Unplanned milestone Sep 10, 2018

@rasky rasky self-assigned this Sep 10, 2018

@rasky

This comment has been minimized.

Member

rasky commented Sep 10, 2018

I’ll have a look

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment