]> Cypherpunks repositories - gostls13.git/commit
cmd/compile: eliminate bound check for slices of the same length
authorYoulin Feng <fengyoulin@live.com>
Tue, 26 Aug 2025 10:23:31 +0000 (18:23 +0800)
committerGopher Robot <gobot@golang.org>
Wed, 15 Oct 2025 19:34:48 +0000 (12:34 -0700)
commit519ae514abace4551e0f99bf7da349a894b399b9
treebce67c9373f5b8eecd2a88a9897fd5651013023f
parentb5a29cca486d26651e249c8395bc2df4a2d92d17
cmd/compile: eliminate bound check for slices of the same length

If two slices start out with the same length and decrease in length by
the same amount on each round of the loop (or in the if block), then
we think their length are always equal.

For example:

if len(a) != len(b) {
return
}
for len(a) >= 4 {
a = a[4:]
b = b[4:] // proved here, omit boundary check
}
if len(a) == len(b) { // proved here
//...
}

Or, change 'for' to 'if':

if len(a) != len(b) {
return
}
if len(a) >= 4 {
a = a[4:]
b = b[4:]
}
if len(a) == len(b) { // proved here
//...
}

Fixes #75144

Change-Id: I4e5902a02b5cf8fdc122715a7dbd2fb5e9a8f5dc
Reviewed-on: https://go-review.googlesource.com/c/go/+/699155
Reviewed-by: Michael Pratt <mpratt@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Keith Randall <khr@golang.org>
src/cmd/compile/internal/ssa/prove.go
test/prove.go