Thursday, April 27, 2023

Swift Foundation Preview

Tony Parker (tweet):

This preview provides a unified implementation of Foundation, written in Swift, that is faster, safer, and more approachable to new contributors.

A new Foundation Workgroup will run reviews of proposed Foundation API and coordinate the needs of the Swift community with Apple engineering.


The following types are available in the preview package, with more to come later. Many types, including JSONEncoder, Calendar, TimeZone, and Locale are all-new Swift implementations. FormatStyle and ParseStyle available as open source for the first time.

Here’s the repo.


Update (2023-05-22): See also: Hacker News.


Update (2023-06-09): Ben Cohen:

In What’s new in Swift I mention the performance improvements from the new Swift implementation of Foundation. Performance came up a lot when we first put the open source package live, but wasn’t easy to talk about until the new OS betas were available.

One common trope at the time was “it isn’t faster than using Objective-C, this is just to reduce Swift bridging costs” and while that’s true, it’s important to note Swift is just plain faster, as seen even when calling into it from ObjC.


3 Comments RSS · Twitter · Mastodon

"[…] that is faster […]"

So from what I'm reading, it's just faster for Swift executables because this avoids some bridging. Which is good for Swift developers.

But, when Apple announced Swift, one of the supposedly advantages of Swift was that it would provide better performances than Objective-C. It looks like this will only happen the day it is not possible to run an Objective-C executable on an Apple device.

It's also nice to have (some of?) the Foundation code open sourced, though.

I don't think Swift is faster than Objective-C today in most circumstances.

So Swift will be faster from a pure Swift world than Swift used to be without having to to bridge to ObjC....but is this pure Swift world still slower than a pure ObjC/C world?

@ObjC4Life With some of the pure Swift code I’ve been writing, it’s much easier/cleaner to make it go fast than with Objective-C. However, I also run into some cases with unexpectedly very poor performance due to bridging (which can usually be worked around, but it’s annoying). And I think Apple and others often compare Swift against Objective-C using ARC, whereas I’m used to the manual Obj-C days where you could walk a data structure or sort without temporarily retaining a zillion objects.

Leave a Comment