Bounds Check Elimination

Go is a memory safe language. In array/slice element indexing and subslice operations, Go runtime will check whether or not the involved indexes are out of range. If an index is out of range, a panic will be produced to prevent the invalid index from doing harm. This is called bounds check. Bounds checks make our code run safely, on the other hand, they also make our code run a little slower.

Since Go SDK 1.7, the standard Go compiler has used a new compiler backend, which based on SSA (static single-assignment form). SSA helps Go compilers effectively use optimizations like BCE (bounds check elimination) and CSE (common subexpression elimination). BCE can avoid some unnecessary bounds checks, and CSE can avoid some duplicate calculations, so that the standard Go compiler can generate more efficient programs. Sometimes the improvement effects of these optimizations are obvious.

This article will list some examples to show how BCE works with the standard Go compiler 1.7+.

For Go SDK 1.7+, we can run go build -gcflags="-d=ssa/check_bce/debug=1" to show which code lines still need bounds checks.

Example 1

// example1.go
package main

func f1(s []int) {
	_ = s[0] // line 5: bounds check
	_ = s[1] // line 6: bounds check
	_ = s[2] // line 7: bounds check
}

func f2(s []int) {
	_ = s[2] // line 11: bounds check
	_ = s[1] // line 12: bounds check eliminated!
	_ = s[0] // line 13: bounds check eliminated!
}

func f3(s []int, index int) {
	_ = s[index] // line 17: bounds check
	_ = s[index] // line 18: bounds check eliminated!
}

func f4(a [5]int) {
	_ = a[4] // line 22: bounds check eliminated!
}

func main() {}
$ go build -gcflags="-d=ssa/check_bce/debug=1" example1.go
./example1.go:5: Found IsInBounds
./example1.go:6: Found IsInBounds
./example1.go:7: Found IsInBounds
./example1.go:11: Found IsInBounds
./example1.go:17: Found IsInBounds

We can see that there are no needs to do bounds checks for line 12 and line 13 in function f2, for the bounds check at line 11 ensures that the indexes in line 12 and line 13 will not be out of range.

But in function f1, bounds checks must be performed for all three lines. The bounds check at line 5 can't ensure line 6 and line 7 are safe, and the bounds check at line 6 can't ensure line 7 is safe.

For function f3, the compiler knows the second s[index] is absolutely safe if the first s[index] is safe.

The compiler also correctly thinks the only line (line 22) in function f4 is safe.

Example 2

// example2.go
package main

func f5(s []int) {
	for i := range s {
		_ = s[i]
		_ = s[i:len(s)]
		_ = s[:i+1]
	}
}

func f6(s []int) {
	for i := 0; i < len(s); i++ {
		_ = s[i]
		_ = s[i:len(s)]
		_ = s[:i+1]
	}
}

func f7(s []int) {
	for i := len(s) - 1; i >= 0; i-- {
		_ = s[i]
		_ = s[i:len(s)]
	}
}

func f8(s []int, index int) {
	if index >= 0 && index < len(s) {
		_ = s[index]
		_ = s[index:len(s)]
	}
}

func f9(s []int) {
	if len(s) > 2 {
	    _, _, _ = s[0], s[1], s[2]
	}
}

func main() {}
$ go build -gcflags="-d=ssa/check_bce/debug=1" example2.go

Cool! The standard compiler removes all bound checks in this program.

Note: before Go SDK version 1.11, the standard compiler is not smart enough to detect line 22 is safe.

Example 3

// example3.go
package main

import "math/rand"

func fa() {
	s := []int{0, 1, 2, 3, 4, 5, 6}
	index := rand.Intn(7)
	_ = s[:index] // line 9: bounds check
	_ = s[index:] // line 10: bounds check eliminated!
}

func fb(s []int, index int) {
	_ = s[:index] // line 14: bounds check
	_ = s[index:] // line 15: bounds check, not smart enough?
}

func fc() {
	s := []int{0, 1, 2, 3, 4, 5, 6}
	s = s[:4]
	index := rand.Intn(7)
	_ = s[:index] // line 22: bounds check
	_ = s[index:] // line 23: bounds check, not smart enough?
}

func main() {}
$ go build -gcflags="-d=ssa/check_bce/debug=1" example3.go
./example3.go:9: Found IsSliceInBounds
./example3.go:14: Found IsSliceInBounds
./example3.go:15: Found IsSliceInBounds
./example3.go:22: Found IsSliceInBounds
./example3.go:23: Found IsSliceInBounds

Oh, so many places still need to do bounds check!

But wait, why does the Go standard compiler think line 10 is safe but line 15 and line 23 are not? Is the compiler still not smart enough?

In fact, the compiler is right here! Why? The reason is the start index in a subslice expression may be larger than the length of the base slice. Let's view a simple example:

package main

func main() {
	s0 := make([]int, 5, 10) // len(s0) == 5, cap(s0) == 10

	index := 8

	// In Go, for the subslice syntax s[a:b], the relations
	// 0 <= a <= b <= cap(s) must be ensured to avoid panicking.

	_ = s0[:index]
	// The above line is safe can't ensure the following line
	// is also safe. In fact, the following line will panic,
	// for the starting index is larger than the end idnex.
	_ = s0[index:] // panic
}

So the conclusion that if s[:index] is safe then s[index:] is also safe is only right when len(s) is equal to cap(s). This is why the code lines in function fb and fc of example 3 still need to do bounds checks.

Go standard compiler successfully detects len(s) is equal to cap(s) in function fa. Great work! Go team!

Example 4

// example4.go
package main

import "math/rand"

func fb2(s []int, index int) {
	_ = s[index:] // line 7: bounds check
	_ = s[:index] // line 8: bounds check eliminated!
}

func fc2() {
	s := []int{0, 1, 2, 3, 4, 5, 6}
	s = s[:4]
	index := rand.Intn(7)
	_ = s[index:] // line 15 bounds check
	_ = s[:index] // line 16: bounds check eliminated!
}

func main() {}
$ go build -gcflags="-d=ssa/check_bce/debug=1" example4.go
./example4.go:7:7: Found IsSliceInBounds
./example4.go:15:7: Found IsSliceInBounds
In this example, The standard Go compiler successfully concludes

Note: before Go SDK 1.9, the standard Go compiler failed to detect line 8 doesn't need bounds check.

Example 5

The current version of the standard Go compiler is not smart enough to eliminate all unnecessary bounds checks. Sometimes, we can make some hints to help the compiler eliminate some unnecessary bounds checks.
// example5.go
package main

func fd(is []int, bs []byte) {
	if len(is) >= 256 {
		for _, n := range bs {
			_ = is[n] // line 7: bounds check, not smart enough
		}
	}
}

func fd2(is []int, bs []byte) {
	if len(is) >= 256 {
		is = is[:256] // line 14: to avoid bounds check at line 16
		for _, n := range bs {
			_ = is[n] // line 16: bounds check eliminated!
		}
	}
}

func fe(isa []int, isb []int) {
	if len(isa) > 0xFFF {
		for _, n := range isb {
			_ = isa[n & 0xFFF] // line 24: bounds check, not smart enough
		}
	}
}

func fe2(isa []int, isb []int) {
	if len(isa) > 0xFFF {
		isa = isa[:0xFFF+1] // line 31: to avoid bounds check at line 33
		for _, n := range isb {
			_ = isa[n & 0xFFF] // line 33: bounds check eliminated!
		}
	}
}

func main() {}
$ go build -gcflags="-d=ssa/check_bce/debug=1" example5.go
./example5.go:7: Found IsInBounds
./example5.go:24: Found IsInBounds
./example5.go:41: Found IsInBounds
./example5.go:48: Found IsSliceInBounds

A Use Case Of BCE: Efficient Slice Comparison

(Note: as Erik Dubbelboer pointed out, the trick shown in this section to optimize slice comparisons is not needed any more since Go SDK 1.11. The standard Go compiler 1.11+ becomes even smarter so that this trick is not needed ay more. I will remove this section after some time.)

We all know that slice types don't support comparison in Go. To compare two slices, we must write custom comparison code, or use the DeepEqual function in the reflect standard package. But the reflect.DeepEqual function is too slow.

The following is a custom slice comparison function.
func CompareSlices_General(a, b []int) bool {
	if len(a) != len(b) {
		return false
	}

	if (a == nil) != (b == nil) {
		return false
	}

	for i, v := range a {
		if v != b[i] { // here bounds check is needed for b[i]
			return false
		}
	}

	return true
}
Bounds check is still needed for the b[i] in the for-range loop block to avoid index i out of range. We can modify the function a bit to remove the bounds check.
func CompareSlices_BCE(a, b []int) bool {
	if len(a) != len(b) {
		return false
	}

	if (a == nil) != (b == nil) {
		return false
	}

	b = b[:len(a)] // this line is the key.
	for i, v := range a {
		if v != b[i] { // no bounds check for b[i] now!
			return false
		}
	}

	return true
}
Let's benchmark the two functions. The result (update: no differences for the standard compiler 1.11+):
Benchmark_SliceComparison/General-4         	 5000000	       287 ns/op
Benchmark_SliceComparison/BCE-4             	 5000000	       251 ns/op

The BCE version is about 12.5% faster than the general version. Really not bad.

Summary

There are more BCE optimizations made by the standard Go compiler. They might be not as abvious as the above listed ones, So this article will not show them all.

Although the BCE feature in the standard Go compiler is still not perfect, it really does well for many common cases. It is no doubt that standard Go compiler will do better in later versions. Thank Go team for adding this wonderful feature!

References:

  1. Bounds Check Elimination
  2. Utilizing the Go 1.7 SSA Compiler

[edit@2016/09/22] added example 5 and refs.


The Go 101 project is hosted on both Github and Gitlab. Welcome to improve Go 101 articles by submitting corrections for all kinds of mistakes, such as typos, grammar errors, wording inaccuracies, description flaws, code bugs and broken links.

Support Go 101 by playing Tapir's games. Cryptocurrency donations are also welcome:
Bitcoin: 1xucQbv5jujFPPwhyg395ri5yV71hx9g9
Ethereum: 0x5dc4aa2c2bbfaadae373dadcfca11b3358912212