cmd/compile: suboptimal code generated for simple integer comparison if-block #33012
Labels
compiler/runtime
Issues related to the Go compiler and/or runtime.
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?
The program linked below generates an array of random uint8s and then repeatedly runs a function that finds the second highest value in the array of random uint8s. The logic inside the inner loop is simple, it keeps track of the highest value and the second highest value using an if-else condition.
The problem is that the code generated by the compiler using the naive implementation (shown in max2_slow() in the example) runs slow -- 810 ms for the test. Adding a
continue
statement in the final else block generates better code that runs the test in 540 milliseconds.For comparison, the same logic as the max2_slow, written in JavaScript runs the same test in 690 ms and it doesn't make a difference if the logic is converted to if-continue.
Just for reference, the same logic in C runs much faster (490ms without loop unrolling, 350 with unrolling), but the C compiler seems to make use of conditional move instructions, which neither Golang nor V8 seem able to generate.
The program:
https://play.golang.org/p/uV2tVrLifCq
(Note that timings will not show in the web based play page, because it does not have a functioning clock)
What did you expect to see?
I would expect the code generated for the plain if-else condition to run as fast as the code generated by manually adding "continue" statement.
What did you see instead?
The code generated by the if-else condition runs 50% slower compared to the code with the artificially added
continue
statement.The text was updated successfully, but these errors were encountered: