There is a newer version of the record available.

Published August 12, 2019 | Version v2
Conference paper Open

Optimization of Swift Protocols

  • 1. Uber Technologies Inc.
  • 2. University of California, Riverside

Description

Title: 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 an industrial app, 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 we have developed to reduce the overhead of Swift protocols.  Within a procedure, we define LocalVar, a data-flow 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 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.

Files

README.md

Files (1.4 GB)

Name Size Download all
md5:3413ddf24646f10ded189fe26f42afb8
13.0 kB Download
md5:85277307ed42d98760602bc6ef1ead54
109.9 MB Download
md5:85277307ed42d98760602bc6ef1ead54
109.9 MB Download
md5:cfe648e50b9e7a2f79e51eda8ad2d760
4.0 kB Preview Download
md5:2913e6e2525353bea6d8b888b06e8d1b
1.4 kB Preview Download
md5:bafa7f271ab1ba462c9c78bfa1af6533
2.9 MB Download
md5:89ece72568fa79beae2b7523028fea62
15.7 MB Download
md5:28a369e65291de02c54ccb81a112e105
502.9 MB Download
md5:acd9f279060a99579665a28e2920030b
681.3 MB Download