This article got posted to reddit the other day, and during a discussion in the comments about the semantics of inout
he posted a benchmark with a really weird optimization miss.
The important part is this:
// Doesn't optimize.
func fx(_ allints: inout [Int], _ j : inout Int) {
for k in allints.indices {
allints[k] = j
j &+= 1
}
}
// Does optimize.
func f(_ allints: inout [Int], _ i : inout Int) {
var j = i
for k in allints.indices {
allints[k] = j
j &+= 1
}
i = j
}
An inout variable is supposed to be semantically equivalent to copying the value of the parameter to a local when the function starts and copying the value back out when the function ends. But in this case the compiler doesn't seem to optimize the function unless the copying is done manually.
I've run the benchmark, and confirmed that this happens on Apple Swift version 5.0, but I don't have the Xcode beta installed to see if it's already been fixed in 5.1. So, I was wondering if someone could confirm that this bug still exists before I report it.