You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What operating system and processor architecture are you using (go env)?
set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=D:\Go
set GORACE=
set GOROOT=D:\Go1.7
set GOTOOLDIR=D:\Go1.7\pkg\tool\windows_amd64
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\Taco\AppData\Local\Temp\go-build651913466=/tmp/go-build -gno-record-gcc-switches
set CXX=g++
set CGO_ENABLED=1
What did you do?
A switch statement of an integer will order the cases such that the integer is tested by increasing values. The following example would be expected to test first for 2, then 3 and then 1.
package main
varcintfunctest(iint) {
switchi {
case2:
c++case3:
c++case1:
c++
}
}
Here i is tested against 1, then 2 and then 3. A different order in the source code will lead to the same output, it looks like the compiler orders the integer checks by increasing value. This is unexpected, because the docs say that switch will evaluate from top-to-bottom, left-to-right.
I encountered this while rewriting if-else statements to switch statements. I saw a drop in performance because the tests were reordered. Now the most occurring case wasn't the first check...see tdewolff/minify#102
The text was updated successfully, but these errors were encountered:
What version of Go are you using (
go version
)?go version go1.7 windows/amd64
What operating system and processor architecture are you using (
go env
)?set GOARCH=amd64
set GOBIN=
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=D:\Go
set GORACE=
set GOROOT=D:\Go1.7
set GOTOOLDIR=D:\Go1.7\pkg\tool\windows_amd64
set CC=gcc
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\Taco\AppData\Local\Temp\go-build651913466=/tmp/go-build -gno-record-gcc-switches
set CXX=g++
set CGO_ENABLED=1
What did you do?
A switch statement of an integer will order the cases such that the integer is tested by increasing values. The following example would be expected to test first for
2
, then3
and then1
.Instead, the ASM output shows:
Here
i
is tested against1
, then2
and then3
. A different order in the source code will lead to the same output, it looks like the compiler orders the integer checks by increasing value. This is unexpected, because the docs say thatswitch
will evaluate from top-to-bottom, left-to-right.I encountered this while rewriting
if-else
statements toswitch
statements. I saw a drop in performance because the tests were reordered. Now the most occurring case wasn't the first check...see tdewolff/minify#102The text was updated successfully, but these errors were encountered: