Swift Heroes 2024

Swift Heroes 2024

Swift Ownership: A road to predictable Swift performance
2024-04-19 , Sala 150

Talk objectives

  • Challenge the audience to think more deeply about memory ownership
  • Provide examples and benchmarks to build intuition for ownership rules

Abstract

Introduction

Swift Ownership Manifesto was initially published in 2017, laying out a vision for making Swift a successful low-level programming language. By 2023, with the release of Swift 5.9, several features outlined in the manifesto have been released:
- Non-copyable types
- borrowing and consuming parameter modifiers
- consume operator

Unfortunately, these features, as well as the idea of ownership, remain hidden from day-to-day development.
In this talk, we will explore the concept of ownership and analyze the performance impact of new features released in Swift 5.9.

What is Ownership

To write predictable and high-performance code, a programmer needs to understand the semantics of high-level constructs.
Before diving into the topic, we will explore the idea of ownership. We will discover that every language requires some kind of ownership rules, either manual or automated. We will explore the idea of ownership in other contexts such as resource ownership.

Swift’s default ownership system

Building on foundations set up in the previous section, we reveal Swift’s default ownership system and how it works under the covers. We explore default ownership rules the Swift team has made. To better understand these rules, we will show short excerpts from Compiler Explorer and present benchmarking results to illustrate why these rules matter.

Borrowing and consuming parameter modifiers

Building upon the rules presented in the previous section, we introduce an explicit mechanism to control the ownership. We will explore their semantical meaning and performance benchmarks in various contexts. We will also touch on the role of compiler optimizations and why these modifiers are crucial despite compiler optimizations.

Further directions

Features released in Swift 5.9 are only a starting point for Swift to become a low-level programming language.
In this section, we will cover the remaining directions and pitches such as borrow bindings

Conclusion

Swift’s new ownership features are paving the way for Swift to become a successful low-level programming language. But even with these new “advanced” features, Swift remains a beginner-friendly language.
As nicely summarised in A roadmap for improving Swift performance predictability:
“We want these features to fit within the "progressive disclosure" ethos of Swift”

Josip is a seasoned programmer with over a decade of experience. With a recently discovered passion for audio, he has been devoted to this field for the past two years. He enjoys the challenge of debugging, creating application architecture, testing, and reverse engineering.
He loves to document his adventures on Infinum blog, sharing their experiences and insights with other programmers.