4 ms·
A couple of people seem surprised that the "idiomatic" Swift code seems to perform much worse. Unfortunately the blog post doesn't provide much details on the c
by saagarjha 5y ago
A couple of people seem surprised that the "idiomatic" Swift code seems to perform much worse. Unfortunately the blog post doesn't provide much details on the code it is actually running, so I tried my hand at guessing:
let vC = [Float](repeating: 1, count: 4)
func unidiomatic() -> Float {
var vA = [Float](repeating: 0, count: 4)
let vB = [1, 2, 3, 4] as [Float]
var tempA: Float = 0.0
for _ in 0..<1000 {
for i in 0...3 {
tempA += vA[i] * vB[i]
}
for i in 0...3 {
vA[i] = vA[i] + vC[i]
}
}
return tempA
}
func idiomatic() -> Float {
var vA = [Float](repeating: 0, count: 4)
let vB = [1, 2, 3, 4] as [Float]
var tempA: Float = 0.0
for _ in 0..<1000 {
tempA = zip(vA, vB).map(*).reduce(0, +)
for (index, value) in vA.enumerated() {
vA[index] = value + vC[index]
}
}
return tempA
}
When compiled with optimizations (it's unclear if the author did this?), the "idiomatic" code here is indeed much worse. It's easy to fix the worst of that by working on a lazy sequence, i.e.
tempA = zip(vA, vB).lazy.map(*).reduce(0, +)
which will save on an extra array allocation and instead run the sum "in place" (in this case, vB is recognized as a constant and in both cases and distributed accordingly). Unfortunately there is still a small amount of overhead because zip doesn't know that the arrays are of the same length, so the compiler inserts some extra bounds checks; also, the second loop gets transformed into what is essentially
temp = __swift_intrisic_move(vA)
for (index, value) in vA.enumerated() {
temp[index] = value + vC[index]
}
vA = __swift_intrisic_move(temp)
but this should ideally bring the performance of the two much closer.
- aardvark179 5y agoThanks for that. From the blog post I wasn’t sure how the temp variable was being used and thus how much of the work might be optimised away entirely.
- api 5y agoThat just means the compiler needs more work around generating optimal code. Rust has some similar idioms and does much better, with the difference between "C-like" Rust and idiomatic Rust usually being small to nonexistent. With Rust you can still sometimes hit edge cases though where the compiler spits out slow code for some idiom that should be zero cost.
- saagarjha 5y agoYes, Swift lags behind on this. Part of it is a language design thing (lots of extra allocations by default, certain invariants cannot be reasonably be expressed to the compiler, lots of optimization information is thrown away before it hits the middle end), part of it (IMO) is a priority thing. Rust has an extremely strong desire to compare favorably against C/C++, and missed optimizations make it look bad. Swift is working in this area but it seems to be late to the game. Also, just to be entirely clear, an example like this (once I “fixed” it) is probably the best Swift can reasonably expect to get, since it hits all the hardcoded semantic hints that Array has been annotated with. It actually does get quite close to the unidiomatic version; save for some extra bounds checks from the zip and a dubious double move, it’s basically done as well as it could with constant propagation and inlining. More complex examples are unlikely to fare as well.
- nerdponx 5y agoI was under the impression that this category of optimizations ("deforestation") is difficult, and requires some immutability/side-effect-free guarantees to be performed effectively. Is that kind of optimization even feasible in Swift?
- zozbot234 5y agoBound checks can be an issue in Rust as well, actually. It's often worthwhile to hoist them manually out of loops, which usually enables the compiler to optimize them out within the loop and even vectorize the resulting code. (This can't happen automatically because the default semantics generally involve a panic or exception to be triggered when the check fails; there's no way to optimize this further.)
- api 5y agoA trick I've found to eliminate them in a few cases: pass &[T; size] instead of &[T] when dealing with statically sized arrays. The compiler will elide bounds checking a lot more aggressively with statically sized arrays since the bounds are known at compile time.
- 5y ago
- mhh__ 5y agoWhen in doubt, look at the disassembly, thank god for godbolt.
- mshockwave 5y agoI'll suggest to look at higher-level IRs first. Try to dump SIL (Swift Intermediate Language) first: ``` swiftc -emit-sil ... ``` Then if that doesn't workout, then try to dump LLVM IR: ``` swiftc -emit-ir ... ``` Both compiler flags should be available in out-of-box Swift compiler.
- mhh__ 5y agoIn this case, why? If I'm looking for failed vectorization in particular, I usually start at the bottom and walk upwards.
- saagarjha 5y agoUnfortunately Swift is kind of verbose to read and doesn't really look good in Godbolt (this is why I didn't link it–also, it only does Intel). Instead I compiled on my own machine and opened the binary in Ghidra.