BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Apple Announces Full Swift Rewrite of the Foundation Framework

Apple Announces Full Swift Rewrite of the Foundation Framework

The Swift team has started to work on a new open-source implementation of the Foundation framework. To be written entirely in Swift, the new Foundation aims to improve performance by getting rid of conversion costs between Objective-C and Swift as well as to provide the opportunity for modularizing and clean it up.

With a native Swift implementation of Foundation, the framework no longer pays conversion costs between C and Swift, resulting in faster performance.

As Apple engineer Tony Parker explained on Swift forum, the performance benefit can be significant:

A reimplementation of Calendar in Swift is 1.5x to 18x as fast as the C one (calling from Swift in various synthetic benchmarks like creation, date calculation).

The Foundation framework is a cornerstone of most macOS and iOS apps, providing a large number of ubiquitous abstractions, including NSObject, NSString, NSArray and NSDictionary, and many more.

Originally written in Objective-C, and largely drawing on its dynamic properties, Foundation can be currently used in Swift apps that run on Apple OSes thanks to Swift interoperability with the Objective-C runtime. This is not an option though for other platforms where Swift is supported but only the original GNUstep Objective-C runtime is available, such as Linux. To circumvent this limitation, when Apple open sourced Swift they built swift-corelibs-foundation, a Swift wrapper around Core Foundation, a lower-level C API lying at the heart of Foundation and providing "toll-free bridged" abstractions mapping to Foundation, including CFString, CFDictionary and so on.

As mentioned, the Swift rewrite will be the occasion for rearchitecturing the Foundation framework. At the moment, five distinct packages are devised, including FoundationEssentials, FoundationInternationalization, FoundationNetworking, FoundationXML, and FoundationObjCCompatibility. This will allow developers to bring in only the dependencies they actually need for a specific app. For example, a developer starting from scratch and wanting to cut all ties to the Darwin Foundation layer could skip the Obj-C compatibility layer altogether.

With the decision to rewrite Foundation, Apple is swapping the ordering between C, Objective-C, and Swift layers and making the Swift layer the underpinning to Foundation across all platforms. This will bring the opportunity to remove a number of features that are currently provided directly from the Swift language and standard library such as NSCoding and NSKeyedArchiver, replaced by Codable, or Lock, OperationQueue, and other concurrency primitives that are replaced by Swift structured concurrency. Those features will be provided by a separate package available on the Darwin platform to ensure compatibility with existing code, though.

The announcement prompted many comments on the Swift Forums, most of them hinting at the convenience of giving several existing APIs a rethink as well as of ensuring Swift-types, such as Codable, or structured concurrency are made powerful enough to fully replace the Obj-C types that are slated for removal from the core Foundation layer.

The Foundation rewrite is foreseeably a medium-term effort for which no schedule has been announced yet and Apple is inviting the Swift community to join the discussion to define its future.

About the Author

Rate this Article

Adoption
Style

BT