cmd/compile: major refactoring of switch walking
There are a lot of complexities to handling switches efficiently: 1. Order matters for expression switches with non-constant cases and for type expressions with interface types. We have to respect side-effects, and we also can't allow later cases to accidentally take precedence over earlier cases. 2. For runs of integers, floats, and string constants in expression switches or runs of concrete types in type switches, we want to emit efficient binary searches. 3. For runs of consecutive integers in expression switches, we want to collapse them into range comparisons. 4. For binary searches of strings, we want to compare by length first, because that's more efficient and we don't need to respect any particular ordering. 5. For "switch true { ... }" and "switch false { ... }", we want to optimize "case x:" as simply "if x" or "if !x", respectively, unless x is interface-typed. The current swt.go code reflects how these constraints have been incrementally added over time, with each of them being handled ad hocly in different parts of the code. Also, the existing code tries very hard to reuse logic between expression and type switches, even though the similarities are very superficial. This CL rewrites switch handling to better abstract away the logic involved in constructing the binary searches. In particular, it's intended to make further optimizations to switch dispatch much easier. It also eliminates the need for both OXCASE and OCASE ops, and a subsequent CL can collapse the two. Passes toolstash-check. Change-Id: Ifcd1e56f81f858117a412971d82e98abe7c4481f Reviewed-on: https://go-review.googlesource.com/c/go/+/194660 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Keith Randall <khr@golang.org>
Showing
This diff is collapsed.
Please register or sign in to comment