Down the memory rabbit hole to understand the need for a modify keyword 🕵🏿‍♂️

Last updated: 7 months ago

The difference between value type and reference type is key to know when you should create a class, a struct or an enum.

But what does it mean for the memory management?

Passing values around instead of pointers, is great for immutability and limiting side effects, but it comes at a cost : calling a function with a value type parameter means that we are passing a copy of the argument, which might take some time to create. Also there is something really weird when you think about it : Array are value types in swift, but how does swift manage a struct that has a potential infinite number of elements. That is where copy on write comes along. (COW 🐮)

COW is a great tool, but it can lead to some performance issues. To mitigate them, a new modify keyword has yet to be released in a future version of Swift.

This talk aims at introducing progressively concepts and problematics related to COW, in order to make it accessible even to those that are new to Swift.

  • Starting with explaining what are the differences between reference and value type.
  • Which means talking briefly about Automatic Reference Counting.
  • Then trying to understand how the memory is managed for value types and its performance cost.
  • Which is going to lead us to wonder how we can make the best of both world using COW.
  • And finally investigating the current limitations of COW and how a new keyword in swift is going to save the day.

I do believe this is a really interesting topic and that it is important to cover it because the pitfall of COW is not going to magically fix itself with the appearance of a new keyword, developers will need to know what is under the hood and when they should or should not worry about this performance issues, and how they can fix it.

This technical talk is rich in content so it should last at least 40 minutes. But some parts can be cut out to fulfil the time requirement.