On the guidance for using a struct versus a class and performance

The guidance for using structs in Swift is very straightforward:

  • Use structures by default.
  • Use classes when you need Objective-C interoperability.
  • Use classes when you need to control the identity of the data you're modeling.
  • Use structures along with protocols to adopt behavior by sharing implementations.

It's nice that it's so straightforward, but it made me wonder if Swift is doing any sort of optimizations under the hood in scenarios where it would be more performant to pass a reference to a function as opposed to a large struct (copy-on-write comes to mind).

it's just that COW structs have some pointers to the bulk storage (the actual buffer of an array, for instance)

This is interesting. After playing around with it a bit more it's starting to make sense. So for certain built-in types a mutable alias is COW:

var foo: [Int] = [0, 1, 2, 3]
var bar = foo
bar.append(42) // The copy happens here

And for other structs the copy happens during the assignment. That makes sense.

The thing I was hung up on was when passing these structs as parameters to a function. What I didn't understand is that they are always immutable unless passed by reference as an inout parameter, so you can't modify them even if you want to. Given that's the case I would suspect that in the majority of cases the compiler can trivially optimize the copy.

I'm still using this as a guideline:

2 Likes

It appears that this talk was removed. Does anyone know why? Were there changes to Swift that rendered this talk obsolete?

2 Likes

I can still see it in the Apple Developer app. Note mine is version 9.0.1 of the app, which only goes up to WWDC 2020; more recent versions of the app require Big Sur.

the Internet Archive is a wonderful thing

2 Likes

The transcript is available separately as well: https://asciiwwdc.com/2016/sessions/416?q=Swift%20performance

1 Like