Performance hit for marking a Swift struct method as mutating?

Swift already switches to pass-by-reference when a value is large. The compiler does not always catch opportunities to avoid copying into new temporary buffers between function calls, though. The case from "Fastest way to get (const) pointer to struct for inter-operability with C/C++" is in fact an instance of this—withUnsafePointer is just a regular function that takes its argument indirectly, but because of some phase ordering issues in the compiler, an avoidable copy gets emitted.

1 Like