cmd/compile: slight reorder of conditions in loop results in much slower code #60611
Labels
compiler/runtime
Issues related to the Go compiler and/or runtime.
FrozenDueToAge
help wanted
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Performance
Milestone
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
These two functions are equivalent:
However, the second is ~2x slower on AMD64 (benchmark: https://go.dev/play/p/IlYydhYWUGl)
Looking at the disassembly, it seems like for
countFor2
a useless bounds check is generated on the hot loop path - it's missing in the disasm ofcountFor1
. Moreover,countFor1
generates a conditonal move instruction to updateresult
, whilecounteFor2
uses another branch.The text was updated successfully, but these errors were encountered: