ABSTRACT
Swift is a modern multi-paradigm programming language with an extensive developer community and open source ecosystem. Swift 3's memory management strategy is based on Automatic Reference Counting (ARC) augmented with unsafe APIs for manually-managed memory. We have seen ARC consume as much as 80% of program execution time. A significant portion of ARC's direct performance cost can be attributed to its use of atomic machine instructions to protect reference count updates from data races. Consequently, we have designed and implemented dynamic atomicity, an optimization which safely replaces atomic reference-counting operations with nonatomic ones where feasible. The optimization introduces a store barrier to detect possibly intra-thread references, compiler-generated recursive reference-tracers to find all affected objects, and a bit of state in each reference count to encode its atomicity requirements.
Using a suite of 171 microbenchmarks, 9 programs from the Computer Language Benchmarks Game, and the Richards benchmark, we performed a limit study by unsafely making all reference counting operations nonatomic. We measured potential speedups of up to 220% on the microbenchmarks, 120% on the Benchmarks Game and 70% on Richards.
By automatically reducing ARC overhead, our optimization both improves Swift 3's performance and reduces the temptation for performance-oriented programmers to resort to unsafe manual memory management. Furthermore, the machinery implemented for dynamic atomicity could also be employed to obtain cheaper thread-safe Swift data structures, or to augment ARC with optional cycle detection or a backup tracing garbage collector.
- Apple. 2016. The Swift Programming Language (Swift 3.1 ed.). Apple Inc.Google Scholar
- ARC 2012. Objective-C Automatic Reference Counting (ARC). http://clang.llvm.org/docs/AutomaticReferenceCounting.html. (2012).Google Scholar
- Stephen M. Blackburn and Kathryn S. McKinley. 2003. Ulterior Reference Counting: Fast Garbage Collection Without a Long Wait. In Proceedings of the Conference on Object-oriented Programing, Systems, Languages, and Applications. Anaheim, California, 344-358. Google ScholarDigital Library
- D. C. Cann and Rod R. Oldehoeft. 1988. Reference Count and Copy Elimination for Parallel Applicative Computing. Technical Report CS-88-129. Department of Computer Science, Colorado State University, Fort Collins, CO.Google Scholar
- CLBG 2008. Computer Language Benchmarks Game. http://benchmarksgame.alioth.debian.org/. (2008).Google Scholar
- Benoit Daloze, Stefan Marr, Daniele Bonetta, and Hanspeter Mössenböck. 2016. Efficient and Thread-safe Objects for Dynamically-typed Languages. In Proceedings of the 2016 ACM SIGPLAN International Conference on Object-Oriented Programming, Systems, Languages, and Applications (OOPSLA 2016). ACM, New York, NY, USA, 642-659. Google ScholarDigital Library
- John DeTreville. 1990. Experience with concurrent garbage collectors for Modula-2+. Technical Report 64. DEC Systems Research Center.Google Scholar
- L. Peter Deutsch and Daniel G. Bobrow. 1976. An Efficient, Incremental, Automatic Garbage Collector. Commun. ACM 19, 9 (Sept. 1976), 522-526. Google ScholarDigital Library
- Tamar Domani, Gal Goldshtein, Elliot K. Kolodner, Ethan Lewis, Erez Petrank, and Dafna Sheinwald. 2002. Thread-local Heaps for Java. In Proceedings of the 3rd International Symposium on Memory Management (ISMM '02). ACM, New York, NY, USA, 76-87. Google ScholarDigital Library
- GCD 2017. Dispatch. https://developer.apple.com/reference/dispatch. (2017).Google Scholar
- Pramod G. Joisha. 2006. Compiler Optimizations for Nondeferred Reference: Counting Garbage Collection. In Proceedings of the 5th International Symposium on Memory Management (ISMM '06). ACM, New York, NY, USA, 150-161. Google ScholarDigital Library
- Richard Jones, Antony Hosking, and Eliot Moss. 2011. The Garbage Collection Handbook: The Art of Automatic Memory Management (1st ed.). Chapman & Hall/CRC. Google ScholarDigital Library
- Joseph Levanoni and Erez Petrank. 1999. A Scalable Reference Counting Garbage Collector. Technical Report.Google Scholar
- Richards 1999. Richards Benchmark. http://www.cl.cam.ac.uk/mr10/Bench.html. (1999).Google Scholar
- Swift 2017. Swift Ownership Manifesto. https://github.com/apple/swift/blob/master/docs/OwnershipManifesto.md. (2017).Google Scholar
- David Ungar. 1984. Generation Scavenging: A Non-disruptive High Performance Storage Reclamation Algorithm. In Proceedings of the First ACM SIGSOFT/SIGPLAN Software Engineering Symposium on Practical Software Development Environments (SDE 1). ACM, New York, NY, USA, 157-167. Google ScholarDigital Library
- David Ungar and David Grove. 2017a. Swift Computer Language Benchmarks Game variants. https://github.com/davidungar/benchmarkingswift/tree/Swift_3.2/ComputerLanguageBenchmarkGame. (2017).Google Scholar
- David Ungar and David Grove. 2017b. Swift Dynamic Atomicity Implementation. https://github.com/davidungar/Swift-dynamic-atomicity. (2017).Google Scholar
- David Ungar and David Grove. 2017c. Swift Richards Benchmark. https://github.com/davidungar/benchmarkingswift/tree/Swift_3.2/Richards. (2017).Google Scholar
Index Terms
- Dynamic atomicity: optimizing swift memory management
Recommendations
Biased reference counting: minimizing atomic operations in garbage collection
PACT '18: Proceedings of the 27th International Conference on Parallel Architectures and Compilation TechniquesReference counting (RC) is one of the two fundamental approaches to garbage collection. It has the desirable characteristics of low memory overhead and short pause times, which are key in today's interactive mobile platforms. However, RC has a higher ...
Dynamic atomicity: optimizing swift memory management
DLS '17Swift is a modern multi-paradigm programming language with an extensive developer community and open source ecosystem. Swift 3's memory management strategy is based on Automatic Reference Counting (ARC) augmented with unsafe APIs for manually-managed ...
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