I just discovered that this is a known and unresolved issue when the same array appears on both sides of the equal sign, such as when swapping elements of an array. An unnecessary copy-on-write of the array happens each time. Breaking it into 4 lines also fixes the problem:
let val1 = arr[index1]
let val2 = arr[index2]
arr[index1] = val2
arr[index2] = val1
https://github.com/apple/swift/issues/56451
Topic:
Programming Languages
SubTopic:
Swift
Tags: