skip to main content
research-article
Open Access
Artifacts Available
Artifacts Evaluated & Functional

Optimization of swift protocols

Published:10 October 2019Publication History
Skip Abstract Section

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.

References

  1. 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 ScholarGoogle ScholarCross RefCross Ref
  2. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  3. 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 ScholarGoogle ScholarCross RefCross Ref
  4. AppleOTA 2019. Apple Over-The-Air Requirements. https://developer.apple.com/accessories/Accessory- Design- Guidelines. pdf . Accessed: 2019-04-01.Google ScholarGoogle Scholar
  5. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  6. BucketSort 2019. BucketSort. https://github.com/raywenderlich/swift- algorithm- club/tree/master/Bucket%20Sort . Accessed: 2019-01-29.Google ScholarGoogle Scholar
  7. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  8. 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 ScholarGoogle ScholarCross RefCross Ref
  9. 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 ScholarGoogle ScholarCross RefCross Ref
  10. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  11. 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 ScholarGoogle ScholarCross RefCross Ref
  12. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  13. 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 ScholarGoogle ScholarCross RefCross Ref
  14. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  15. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  16. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  17. Jon Hoffman. 2019. Swift 4 Protocol-Oriented Programming (3rd ed.). Packt Publishing.Google ScholarGoogle Scholar
  18. 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 ScholarGoogle ScholarCross RefCross Ref
  19. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  20. Paul Hudak and Joseph H. Fasel. 1992. A Gentle Introduction to Haskell. SIGPLAN Not. 27, 5 (May 1992), 1–52. Google ScholarGoogle ScholarDigital LibraryDigital Library
  21. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  22. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  23. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  24. Steve Klabnik and Carol Nichols. 2019. The Rust Programming Language. https://doc.rust- lang.org/stable/book/ . Accessed: 2019-04-05.Google ScholarGoogle Scholar
  25. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  26. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  27. Benjamin C. Pierce. 2002. Types and Programming Languages (1st ed.). The MIT Press.Google ScholarGoogle ScholarDigital LibraryDigital Library
  28. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  29. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  30. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  31. 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 ScholarGoogle Scholar
  32. SILDocs 2019. Swift Intermediate Language (SIL). https://github.com/apple/swift/blob/master/docs/SIL.rst . Accessed: 2019-03-22.Google ScholarGoogle Scholar
  33. SILSSA 2017. SIL SSA Updater. https://github.com/apple/swift/blob/master/lib/SILOptimizer/Utils/SILSSAUpdater.cpp . Accessed: 2019-01-29.Google ScholarGoogle Scholar
  34. SILVerifier 2017. SILVerifier. https://github.com/apple/swift/blob/master/lib/SIL/SILVerifier.cpp . Accessed: 2019-01-29.Google ScholarGoogle Scholar
  35. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  36. 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 ScholarGoogle ScholarDigital LibraryDigital Library
  37. SwiftBench 2019. Swift Benchmark Suite. https://github.com/apple/swift/tree/master/benchmark . Accessed: 2019-01-29.Google ScholarGoogle Scholar
  38. SwiftLang 2019. Swift Programming Language. https://github.com/apple/swift/ . Accessed: 2019-01-29.Google ScholarGoogle Scholar
  39. SwiftLangDoc 2019. Swift Language Documentation. https://developer.apple.com/documentation/swift Accessed: 2019-04-01.Google ScholarGoogle Scholar
  40. UberRibs 2017. Ribs: Cross Platform Mobile Architecture. https://github.com/uber/RIBs/ . Accessed: 2019-01-29.Google ScholarGoogle Scholar
  41. WWDCSwiftPerf 2016. Understanding Swift Performance - Apple WWDC 2016. https://developer.apple.com/videos/play/ wwdc2016/416/ . Accessed: 2019-01-29.Google ScholarGoogle Scholar

Index Terms

  1. Optimization of swift protocols

    Recommendations

    Comments

    Login options

    Check if you have access through your login credentials or your institution to get full access on this article.

    Sign in

    Full Access

    PDF Format

    View or Download as a PDF file.

    PDF

    eReader

    View online with eReader.

    eReader
    About Cookies On This Site

    We use cookies to ensure that we give you the best experience on our website.

    Learn more

    Got it!