Recent Apple Silicon like A13 Bionic has both high-performance cores (P cores) and high-efficiency cores (E cores). These different core types allow you to deliver apps that have both great performance and great battery life. To take full advantage of their performance and efficiency, you can provide the operating system (OS) with information about how to execute your app in the most optimal way. From there, the OS uses semantic information to make better scheduling and performance control decisions.
Let’s explore some best practices to help you get the most out of Apple Silicon and create faster, more efficient apps. Discover how to adapt your code for asymmetric multiprocessing, adopt Quality of Service classes, and find out more about Grand Central Dispatch APIs.
Adapt your code for asymmetric multiprocessing
Unlike traditional symmetric multiprocessing (SMP) systems which use many identical cores, asymmetric multiprocessing (AMP) systems have cores that are not all equal.
Apple Silicon Macs are AMP systems, having both performance cores (P cores) and efficiency cores (E cores). Even though E cores are optimized for high efficiency, they offer significant compute resources for apps to take advantage of.
An app may execute threads on both P and E cores over a period of time. The OS places threads on P or E cores based on the following criteria:
- Information your app provides
- Observation of the app’s workload
- Observation of the system as whole
On Apple Silicon Macs, the system observes applications and daemons separately from each other. This allows the system to execute them with individual efficiency and performance characteristics. As an example, an app running in the background may have its threads placed on E cores to optimize battery life while the foreground app is taking advantage of P cores.
Use Quality of Service classes to categorize work
Quality of Service (QoS) classes are the primary way for you to categorize work performed by your app and provide the OS with semantic information on the nature of that work and how it affects someone using your app.
On AMP systems, the operating system uses the energy-efficiency information conveyed by QoS classes to influence placement of threads on P or E cores. You can use the following QoS classes on Apple platforms:
For instance, you can use the QoS class Background to categorize all of your app’s background processes in order to maximize battery life.
For more details on Quality of Service, watch “Building Responsive and Efficient Apps with GCD” from WWDC15 and explore the energy efficiency guide.
Manage parallel workloads
Your app can take advantage of both P and E cores to execute parallel worker threads and get tasks done faster and more efficiently.
Statically pre-assigning pieces of a parallel workload to worker threads will leave threads idle before the end of the execution. This is because not all cores are equal and so worker threads will not make identical progress. Instead, subdivide parallel problems into a large number of pieces and use a work-stealing algorithm to balance these pieces across threads to keep all workers busy.
GCD is the recommended API for expressing concurrent and parallel workloads in your application. Parallel workloads should use the
dispatch_apply API to execute parallel instances of a block on multiple cores simultaneously. Set the number of iterations to at least three times the total number of cores on the system. This enables the work-stealing algorithm inside GCD to appropriately balance iterations.
If you have an existing codebase that cannot adopt GCD and uses a custom pthread worker pool, you may benefit from implementing a work-stealing algorithm to achieve optimal performance. For more information, see “Tuning Your Code’s Performance for Apple Silicon.”
Further AMP exploration
When you adopt GCD and QoS in your application, you can unlock greater processing power and ensure that your application performs well across all Apple platforms. If you need more information on testing your adoption, we’ve also provided resources to help you there.
Working on something we haven’t mentioned above? Check out the Developer website for more information on other situations like daemons and agents working on behalf of applications and realtime audio applications and plugins.