Skip to footer
Home Research Programming Systems Optimization of Swift Protocols

Optimization of Swift Protocols

Abstract

Swift, an increasingly-popular programming language, advocates the use of protocols, which define a set of required methods and properties for conforming types. Protocols are commonly used in Swift programs for abstracting away implementation details; e.g., in a large industrial app from Uber, they are heavily used to enable mock objects for unit testing. Unfortunately, heavy use of protocols can result in significant performance overhead. Beyond the dynamic dispatch often associated with such a feature, Swift allows for both value and reference types to conform to a protocol, leading to significant boxing and unboxing overheads.

In this paper, we describe three new optimizations and transformations to reduce the overhead of Swift protocols. Within a procedure, we define LocalVar, a dataflow analysis and transformation to remove both dynamic dispatch and boxing overheads. We also describe Param, which optimizes the case of protocol-typed method parameters using specialization. Finally, we describe SoleType, a transformation that injects casts when a global analysis (like type-hierarchy analysis) discovers some protocol variable must have some concrete type. We also describe how these optimizations work fruitfully together and with existing Swift optimizations to deliver further speedups.

We perform elaborate experimentation and demonstrate that our optimizations deliver an average 1.56x speedup on a suite of Swift benchmarks that use protocols. Further, we applied the optimizations to a production iOS Swift application from Uber used by millions of customers daily. For a set of performance spans defined by the developers of the application, the optimized version showed speedups ranging from 6.9% to 55.49%. A version of our optimizations has been accepted as part of the official Swift compiler distribution.

Authors

Raj Barik, Manu Sridharan, Murali Krishna Ramanathan, Milind Chabbi

Conference

OOPSLA 2019

Full Paper

‘Optimization of Swift Protocols’ (PDF)

Programming Systems Team

Comments
Previous article Learning Joint 2D-3D Representations for Depth Completion
Next article Efficient Graph Generation with Graph Recurrent Attention Networks
Raj Barik
Raj Barik is a programming systems research scientist and technical manager on Uber's Programming Systems team. He currently works on building tools for understanding performance anomalies in data centers, including developing static analysis and transformation tools for Swift and Go. In the past, he has worked on optimizing compilers and runtime systems.
Murali Krishna Ramanathan
Murali Krishna Ramanathan is a programming systems research scientist at Uber. He currently works on building program analysis tools to improve developer productivity. In the past, he has designed novel program analyses for concurrency bug detection and automated test generation.
Milind Chabbi
Milind Chabbi is a senior researcher in the programming systems group at Uber. He conducts research in the areas of compiler optimizations, high-performance parallel computing, synchronization techniques, and performance analysis tools.