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.
- Ole Agesen, Jens Palsberg, and Michael I. Schwartzbach. 1993. Type Inference of Self. In ECOOP’ 93 — Object-Oriented Programming, Oscar M. Nierstrasz (Ed.). Springer Berlin Heidelberg, Berlin, Heidelberg, 247–267. Google Scholar
Cross Ref
- Wonsun Ahn, Jiho Choi, Thomas Shull, María J. Garzarán, and Josep Torrellas. 2014. Improving JavaScript Performance by Deconstructing the Type System. In Proceedings of the 35th ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI ’14). ACM, New York, NY, USA, 496–507. Google Scholar
Digital Library
- Owen Anderson, Emily Fortuna, Luis Ceze, and Susan Eggers. 2011. Checked load: Architectural support for javascript typechecking on mobile processors. In 2011 IEEE 17th International Symposium on High Performance Computer Architecture. IEEE, 419–430. Google Scholar
Cross Ref
- AppleOTA 2019. Apple Over-The-Air Requirements. https://developer.apple.com/accessories/Accessory- Design- Guidelines. pdf . Accessed: 2019-04-01.Google Scholar
- David F. Bacon and Peter F. Sweeney. 1996. Fast Static Analysis of C++ Virtual Function Calls. In Proceedings of the 11th ACM SIGPLAN Conference on Object-oriented Programming, Systems, Languages, and Applications (OOPSLA ’96). ACM, New York, NY, USA, 324–341. Google Scholar
Digital Library
- BucketSort 2019. BucketSort. https://github.com/raywenderlich/swift- algorithm- club/tree/master/Bucket%20Sort . Accessed: 2019-01-29.Google Scholar
- MichaÅĆ Cierniak, Guei-Yuan Lueh, and James M. Stichnoth. 2000. Practicing JUDO: Java Under Dynamic Optimizations. In Proceedings of the ACM SIGPLAN 2000 Conference on Programming Language Design and Implementation (PLDI ’00). ACM, New York, NY, USA, 13–26. Google Scholar
Digital Library
- Jeffrey Dean, David Grove, and Craig Chambers. 1995. Optimization of Object-Oriented Programs Using Static Class Hierarchy Analysis. In ECOOP’95 — Object-Oriented Programming, 9th European Conference, Åarhus, Denmark, August 7–11, 1995, Mario Tokoro and Remo Pareschi (Eds.). Springer Berlin Heidelberg, Berlin, Heidelberg, 77–101. Google Scholar
Cross Ref
- David Detlefs and Ole Agesen. 1999. Inlining of Virtual Methods. In Proceedings of the 13th European Conference on ObjectOriented Programming (ECOOP ’99). Springer-Verlag, London, UK, UK, 258–278. Google Scholar
Cross Ref
- L. Peter Deutsch and Allan M. Schiffman. 1984. Efficient Implementation of the Smalltalk-80 System. In Proceedings of the 11th ACM SIGACT-SIGPLAN Symposium on Principles of Programming Languages (POPL ’84). ACM, New York, NY, USA, 297–302. Google Scholar
Digital Library
- Gem Dot, Alejandro Martínez, and Antonio González. 2017. Removing Checks in Dynamically Typed Languages Through Efficient Profiling. In Proceedings of the 2017 International Symposium on Code Generation and Optimization (CGO ’17). IEEE Press, Piscataway, NJ, USA, 257–268. Google Scholar
Cross Ref
- Mary F. Fernández. 1995. Simple and Effective Link-time Optimization of Modula-3 Programs. In Proceedings of the ACM SIGPLAN 1995 Conference on Programming Language Design and Implementation (PLDI ’95). ACM, New York, NY, USA, 103–115. Google Scholar
Digital Library
- Nobuhisa Fujinami. 1998. Determination of Dynamic Method Dispatches Using Run-Time Code Generation. In Proceedings of the Second International Workshop on Types in Compilation (TIC ’98). Springer-Verlag, Berlin, Heidelberg, 253–271. Google Scholar
Cross Ref
- Andreas Gal, Brendan Eich, Mike Shaver, David Anderson, David Mandelin, Mohammad R. Haghighat, Blake Kaplan, Graydon Hoare, Boris Zbarsky, Jason Orendorff, Jesse Ruderman, Edwin W. Smith, Rick Reitmaier, Michael Bebenita, Mason Chang, and Michael Franz. 2009. Trace-based Just-in-time Type Specialization for Dynamic Languages. In Proceedings of the 30th ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI ’09). ACM, New York, NY, USA, 465–478. Google Scholar
Digital Library
- Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. 1995. Design Patterns: Elements of Reusable Object-oriented Software. Addison-Wesley Longman Publishing Co., Inc., Boston, MA, USA.Google Scholar
Digital Library
- Martin Hirzel, Daniel Von Dincklage, Amer Diwan, and Michael Hind. 2007. Fast Online Pointer Analysis. ACM Trans. Program. Lang. Syst. 29, 2, Article 11 (April 2007). Google Scholar
Digital Library
- Jon Hoffman. 2019. Swift 4 Protocol-Oriented Programming (3rd ed.). Packt Publishing.Google Scholar
- Urs Hölzle, Craig Chambers, and David Ungar. 1991. Optimizing Dynamically-Typed Object-Oriented Languages With Polymorphic Inline Caches. In Proceedings of the European Conference on Object-Oriented Programming (ECOOP ’91). Springer-Verlag, London, UK, UK, 21–38. Google Scholar
Cross Ref
- Urs Hölzle and David Ungar. 1994. Optimizing Dynamically-dispatched Calls with Run-time Type Feedback. In Proceedings of the ACM SIGPLAN 1994 Conference on Programming Language Design and Implementation (PLDI ’94). ACM, New York, NY, USA, 326–336. Google Scholar
Digital Library
- Paul Hudak and Joseph H. Fasel. 1992. A Gentle Introduction to Haskell. SIGPLAN Not. 27, 5 (May 1992), 1–52. Google Scholar
Digital Library
- Kazuaki Ishizaki, Motohiro Kawahito, Toshiaki Yasue, Hideaki Komatsu, and Toshio Nakatani. 2000. A Study of Devirtualization Techniques for a Java Just-In-Time Compiler. In Proceedings of the 15th ACM SIGPLAN Conference on Object-oriented Programming, Systems, Languages, and Applications (OOPSLA ’00). ACM, New York, NY, USA, 294–310. Google Scholar
Digital Library
- Suresh Jagannathan and Stephen Weeks. 1995. A Unified Treatment of Flow Analysis in Higher-order Languages. In Proceedings of the 22Nd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’95). ACM, New York, NY, USA, 393–407. Google Scholar
Digital Library
- Madhukar N. Kedlaya, Jared Roesch, Behnam Robatmili, Mehrdad Reshadi, and Ben Hardekopf. 2013. Improved Type Specialization for Dynamic Scripting Languages. In Proceedings of the 9th Symposium on Dynamic Languages (DLS ’13). ACM, New York, NY, USA, 37–48. Google Scholar
Digital Library
- Steve Klabnik and Carol Nichols. 2019. The Rust Programming Language. https://doc.rust- lang.org/stable/book/ . Accessed: 2019-04-05.Google Scholar
- William Landi and Barbara G. Ryder. 1991. Pointer-Induced Aliasing: A Problem Classification. In Conference Record of the Eighteenth Annual ACM Symposium on Principles of Programming Languages, Orlando, Florida, USA, January 21-23, 1991. 93–103. Google Scholar
Digital Library
- Hidehiko Masuhara and Akinori Yonezawa. 2002. A Portable-approach to Dynamic Optimization in Run-time Specialization. New Gen. Comput. 20, 1 (Jan. 2002), 101–124. Google Scholar
Digital Library
- Benjamin C. Pierce. 2002. Types and Programming Languages (1st ed.). The MIT Press.Google Scholar
Digital Library
- Tiark Rompf, Arvind K. Sujeeth, Kevin J. Brown, HyoukJoong Lee, Hassan Chafi, and Kunle Olukotun. 2014. Surgical Precision JIT Compilers. In Proceedings of the 35th ACM SIGPLAN Conference on Programming Language Design and Implementation (PLDI ’14). ACM, New York, NY, USA, 41–52. Google Scholar
Digital Library
- Ulrik P. Schultz, Julia L. Lawall, and Charles Consel. 2003. Automatic Program Specialization for Java. ACM Trans. Program. Lang. Syst. 25, 4 (July 2003), 452–499. Google Scholar
Digital Library
- O. Shivers. 1988. Control Flow Analysis in Scheme. In Proceedings of the ACM SIGPLAN 1988 Conference on Programming Language Design and Implementation (PLDI ’88). ACM, New York, NY, USA, 164–174. Google Scholar
Digital Library
- Olin Grigsby Shivers. 1991. Control-flow Analysis of Higher-order Languages of Taming Lambda. Ph.D. Dissertation. Pittsburgh, PA, USA. UMI Order No. GAX91-26964.Google Scholar
- SILDocs 2019. Swift Intermediate Language (SIL). https://github.com/apple/swift/blob/master/docs/SIL.rst . Accessed: 2019-03-22.Google Scholar
- SILSSA 2017. SIL SSA Updater. https://github.com/apple/swift/blob/master/lib/SILOptimizer/Utils/SILSSAUpdater.cpp . Accessed: 2019-01-29.Google Scholar
- SILVerifier 2017. SILVerifier. https://github.com/apple/swift/blob/master/lib/SIL/SILVerifier.cpp . Accessed: 2019-01-29.Google Scholar
- Bjarne Steensgaard. 1996. Points-to Analysis in Almost Linear Time. In Proceedings of the 23rd ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages (POPL ’96). ACM, New York, NY, USA, 32–41. Google Scholar
Digital Library
- Vijay Sundaresan, Laurie Hendren, Chrislain Razafimahefa, Raja Vallée-Rai, Patrick Lam, Etienne Gagnon, and Charles Godin. 2000. Practical Virtual Method Call Resolution for Java. In Proceedings of the 15th ACM SIGPLAN Conference on Object-oriented Programming, Systems, Languages, and Applications (OOPSLA ’00). ACM, New York, NY, USA, 264–280. Google Scholar
Digital Library
- SwiftBench 2019. Swift Benchmark Suite. https://github.com/apple/swift/tree/master/benchmark . Accessed: 2019-01-29.Google Scholar
- SwiftLang 2019. Swift Programming Language. https://github.com/apple/swift/ . Accessed: 2019-01-29.Google Scholar
- SwiftLangDoc 2019. Swift Language Documentation. https://developer.apple.com/documentation/swift Accessed: 2019-04-01.Google Scholar
- UberRibs 2017. Ribs: Cross Platform Mobile Architecture. https://github.com/uber/RIBs/ . Accessed: 2019-01-29.Google Scholar
- WWDCSwiftPerf 2016. Understanding Swift Performance - Apple WWDC 2016. https://developer.apple.com/videos/play/ wwdc2016/416/ . Accessed: 2019-01-29.Google Scholar
Index Terms
Optimization of swift protocols
Recommendations
Featherweight Swift: a Core calculus for Swift’s type system
SLE 2020: Proceedings of the 13th ACM SIGPLAN International Conference on Software Language EngineeringSwift is a modern general-purpose programming language, designed to be a replacement for C-based languages. Although primarily directed at development of applications for Apple's operating systems, Swift's adoption has been growing steadily in other ...
How swift developers handle errors
MSR '18: Proceedings of the 15th International Conference on Mining Software RepositoriesSwift is a new programming language developed by Apple as a replacement to Objective-C. It features a sophisticated error handling (EH) mechanism that provides the kind of separation of concerns afforded by exception handling mechanisms in other ...
A Swift Introduction to Swift App Development (Abstract Only)
SIGCSE '15: Proceedings of the 46th ACM Technical Symposium on Computer Science EducationSwift is a new programming language recently introduced by Apple as a replacement for Objective-C. Considering that Objective-C ranks third on the Tiobe Index and is the progenitor of virtually all the 1.2 million apps in the App Store, Swift is likely ...






Comments