Swift 5 ABI Stability

Apple Inc. introduced Swift language in 2014. It has become one of the fastest growing languages although it was launched such a short time back.

So, what makes Swift so popular?

Swift makes it easy to develop software incredibly fast for phones, desktops, servers or anything else that runs code. The Swift compiler is optimized for performance and language is optimized for development, without compromises on either of the factors.

Journey of Swift language began with the release of version 1.0 and the latest version to release is 5.0. The Swift community is constantly evolving and bringing about improvements with each new version that is released.

Swift 5 language version of Apple can be considered a huge milestone for the developer community with the feature of ABI stability.

Let’s tell you the basics of ABI stability to help you understand its importance better!

What is ABI Stability?


The main focus of Swift 5 is the ABI stability for Swift Standard Library.

ABI stability means locking ABI to the point where future compiler versions can produce binaries conforming to the stable ABI. Once an ABI is stable it tends to prevail for the rest of the platform’s lifetime.

ABI stability will only affect the invariants of externally visible public interfaces and symbols. For instance, future compilers are free to change calling functions for internal function calls as long as the public interfaces are preserved. As a result of ABI stability, the Swift libraries are now included in every macOS, iOS, tvOS and watchOS release.

Now, the apps will be easier to develop and smaller too as those libraries won’t be included.

Swift 5 ABI Stability

Why does ABI stability matter?


  • Reduced bundle size - Size of your application reduces as you don’t have to include the Swift standard library in your frameworks folder.
  • Source compatibility - Newer compilers can compile the code written in older versions of Swift. The aim is to reduce migration hassle that Swift developers face while migrating to a newer Swift version.
  • Lesser language changes - Less changes lead to lesser efforts in migration.
  • Binary framework and runtime compatibility - It enables distribution of frameworks in a binary form which works across multiple swift versions. Binary framework includes both swift module file and shared library. Developers can create pre-compiled frameworks in Swift because they don’t have to bundle the Swift standard library into their framework.

However, there are some prerequisites to ABI stability. We have listed them for you here!

  • Types, like structs and classes, should have a defined in-memory layout for instances of that type and share the same layout conventions.
  • Type metadata is used majorly by Swift programs. This metadata should either have a defined memory layout or a set of defined APIs for querying metadata of a type.
  • Every exported or external symbol in the library needs a unique name on which binary entities can agree. Swift provides function overloading and contextual namespaces, which means any name in source code may not be globally unique. Unique name is created through a technique called name mangling.
  • Functions must stick to the calling conventions that requires such things as layout of the call stack, what registers are preserved and ownership conventions.
  • Swift ships with a runtime library that handles things like dynamic casting, reflection, reference counting etc. The compiled Swift programs make external calls to this runtime. Thus, Swift runtime API is Swift ABI.
  • Swift also ships with a standard library that defines many common types, structures and operations on these. A shipped standard library will work with applications written in different versions of Swift only if it is exposed to stable API. thus, Swift standard library API is Swift ABI as well as layout of many of the types it defines.

Now, that you are familiar with the basics of Swift and ABI stability let’s tell you some advantages of ABI stability!

  • Once Swift is declared ABI stable, the code written after that will be compatible with newer versions of the language and developers don’t need to update all external dependencies of the project while migrating to newer version of Swift.
  • Library author can supply his framework as a binary framework once the module format stability is achieved.
  • Application bundle size will decrease as the stable Swift runtime could then be incorporated within the operating system.
  • Language would keep evolving but changes to ABI from that point would be additive. These changes can be taken advantage of when the minimum targeted Swift version supports them, since ABI stability locks only externally visible public interfaces and symbols.

Let’s Conclude!


Thus, it can be said that in Swift 5 ABI stability has brought about two major improvements.

The first is that it is possible to build and distribute binary libraries using the Swift language. This is important while building closed source libraries. Developers are currently stuck with Objective-C.

The second major improvement is of convenience. It is no longer required to incorporate Swift itself in your applications and the bundle size will drop significantly. ABI stability means less drastic changes to the language and its standard APIs.

Swift language was already considered the future of ios app development and now that the latest version has ABI stability, the language is free of its greatest disadvantage. It can now be considered a more mature programming environment.


ABOUT THE AUTHOR
blog CMO - kodytechnolab

Jatin Patel

iOS Developer