]> Cypherpunks repositories - gostls13.git/commit
cmd/compile: recognize integer ranges in switch statements
authorJosh Bleecher Snyder <josharian@gmail.com>
Fri, 17 Jun 2016 23:27:23 +0000 (16:27 -0700)
committerJosh Bleecher Snyder <josharian@gmail.com>
Tue, 30 Aug 2016 21:20:25 +0000 (21:20 +0000)
commit8c85e23087d90e831a70ccd199cac49a38d91027
tree18442d9cfd289bfbea0b764f8fc740a8afe3cf29
parent5c3edc46a6014f9ff74a5e46a69a8891cad3190d
cmd/compile: recognize integer ranges in switch statements

Consider a switch statement like:

switch x {
case 1:
  // ...
case 2, 3, 4, 5, 6:
  // ...
case 5:
  // ...
}

Prior to this CL, the generated code treated
2, 3, 4, 5, and 6 independently in a binary search.
With this CL, the generated code checks whether
2 <= x && x <= 6.
walkinrange then optimizes that range check
into a single unsigned comparison.

Experiments suggest that the best min range size
is 2, using binary size as a proxy for optimization.

Binary sizes before/after this CL:

cmd/compile: 14209728 / 14165360
cmd/go:       9543100 /  9539004

Change-Id: If2f7fb97ca80468fa70351ef540866200c4c996c
Reviewed-on: https://go-review.googlesource.com/26770
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/fmt.go
src/cmd/compile/internal/gc/swt.go
src/cmd/compile/internal/gc/syntax.go