• Josh Bleecher Snyder's avatar
    cmd/compile: recognize integer ranges in switch statements · 8c85e230
    Josh Bleecher Snyder authored
    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: 's avatarMatthew Dempsky <mdempsky@google.com>
    8c85e230
swt.go 24.8 KB