苹果开发者注册应用程序_苹果硅应用程序开发人员

苹果开发者注册应用程序

One of the more surprising announcements at WWDC2020 was the move from Intel to Apple Silicon. In this article, I want to look at how it will affect us humble developers. How will we be able to take advantage of the new hardware?

WWDC2020上最令人惊讶的公告之一就是从英特尔迁移到苹果芯片。 在本文中,我想看看它将如何影响我们谦虚的开发人员。 我们将如何利用新硬件?

During this WWDC2020 video on the subject, the presenter (Gavin Barraclough) goes over some of the differences between Intel-based and Apple Silicon-based Macs and answers my question to an extent. Let’s dig a little deeper into his recommendations.

在本主题的WWDC2020视频中 ,演示者(Gavin Barraclough)介绍了基于Intel的Mac和基于Apple Silicon的Mac之间的一些区别,并在一定程度上回答了我的问题。 让我们更深入地研究他的建议。

One of the principal points he made was the fact that the new Macs based on Apple Silicon will be using a single system on a chip or SoC. The SoC itself is an asymmetric multiprocessing unit that is different from Intel’s, as displayed in this image taken from that presentation:

他提出的主要观点之一是,基于Apple Silicon的新型Mac将在芯片或SoC上使用单个系统。 SoC本身是一个与Intel处理器不同的非对称多处理单元,如以下演示文稿中所显示的图像所示:

Image for post
Symmetric cores vs. asymmetric cores
对称核与非对称核

The emphasis is on designing hardware that will be able to run apps requiring less CPU more efficiently whilst retaining the performance required for more CPU-intensive ones. In the presentation, Gavin goes over the sort of frameworks and formats you need to focus on to take full advantage of the new SoC. Frameworks like these named specifically are designed to take advantage of multiprocessing cores:

重点在于设计硬件,这些硬件将能够更高效地运行需要较少CPU的应用程序,同时保留更多CPU密集型应用程序所需的性能。 在演示中,Gavin讨论了您需要集中精力以充分利用新SoC的各种框架和格式。 像这样专门命名的框架旨在利用多处理核心:

Image for post
Apple frameworks already AMP-tuned
Apple框架已经AMP调整

There is obviously far too much to cover in any detail here, and with that in mind, I want to focus on what you can do to manually optimise your app to run on asymmetric multiprocessing Apple SoC hardware — optimisation that I suspect you’re already doing.

显然,这里没有太多细节可以讨论,考虑到这一点,我想专注于您可以手动优化应用程序以在非对称多处理Apple SoC硬件上运行的方法-我怀疑您已经在进行优化在做。

You see, Swift has been taking advantage of this sort of architecture for some time with a system called GCD or Grand Central Dispatch.

您会看到,Swift一直在利用名为GCD或Grand Central Dispatch的系统来利用这种架构。

Focusing AMP hardware with GCD centres around using QoS. There are four levels of QoS in Swift that Apple have defined, illustrated here in the WWDC2015 slide on the subject:

将AMP硬件和GCD集中在一起使用QoS。 苹果在Swift中定义了QoS的四个级别,在WWDC2015幻灯片中对此进行了说明:

Image for post

By default, the main thread (User Interactive) is always running at top priority. Threads dispatched from it run as User Initiated. Nothing runs as Utility or Background unless you specify it to do so. Let’s do a review of what that looks like.

默认情况下,主线程(用户交互式)始终始终以最高优先级运行。 从其分派的线程以用户启动的身份运行。 除非您指定将其作为实用程序或后台运行,否则不会运行。 让我们回顾一下它的外观。

To make sure we’re not comparing apples to oranges, I am going to read in a file of 48,000 lines, noting when I start and when I complete.

为了确保我们不会将苹果与橙子进行比较,我将读取一个48,000行的文件,注意何时开始和何时完成。

I’ve provided a very simple animation to show what is happening. The GIF below illustrates the effect. When I start the demo app, the start time shows on both uploads. As the data loads, the UI is updated on the thread running the upload as a background. With the upload running on the same thread as the UI, it doesn’t give us any update until it is finished. The code to push a thread to the background looks like this:

我提供了一个非常简单的动画来显示正在发生的事情。 下面的GIF说明了这种效果。 当我启动演示应用程序时,开始时间会显示在两个上传文件中。 随着数据加载,UI在运行上传作为后台的线程上进行更新。 由于上传操作与用户界面在同一线程上运行,因此直到完成后,我们才进行任何更新。 将线程推送到后台的代码如下所示:

DispatchQueue.global(qos: .background).async { 
//background thread
}

As you can clearly see, although the load takes less time, the effect isn’t very user-friendly. The user isn’t informed of the progress of the upload until it is complete.

您可以清楚地看到,尽管加载花费的时间更少,但效果不是非常友好。 在上传完成之前,不会通知用户上传的进度。

Image for post

How will this work with Apple Silicon? In theory, it will run better on the background task (i.e. use less power, generating less heat) with the code correctly prioritised. It should look no different.

这将如何与Apple Silicon一起使用? 从理论上讲,在正确设置代码优先级的情况下,它将在后台任务上运行得更好(即,使用更少的功率,产生更少的热量)。 它看起来应该没有什么不同。

But wait, one small caveat on the background: The message I send back to the UI will run at the same priority as its parent (aka background), which won’t work. Thankfully, Xcode will warn you with one of these messages if you make this mistake:

但是,等等,关于背景的一个小警告:我发回UI的消息将以其父级(也称为背景)的优先级运行,这将不起作用。 幸运的是,如果您犯此错误,Xcode会用以下消息之一警告您:

Image for post

To correct this, you simply have to wrap the UI code in a thread like this:

要更正此问题,只需将UI代码包装在这样的线程中:

DispatchQueue.main.async {
// Update UI
}

Now as everybody knows, the key to success as a coder is being organised. And to do that, you can create labels to name your queues using code like this:

现在,众所周知,组织了成功成为编码员的关键。 为此,您可以使用以下代码创建标签来命名队列:

let background = DispatchQueue.init(label: "background", qos: .background, attributes: .concurrent, autoreleaseFrequency: .inherit, target: nil)
background.async {
// do something
}

The secret to gaining the greatest advantage on SoC laptops is to update your code making sure your threads are running at the correct QoS.

在SoC笔记本电脑上获得最大优势的秘诀是更新代码,以确保线程以正确的QoS运行。

Back to Apple Silicon and those cores you’re going to be using to run your threads. We have a speculative number for the Q4 2020 MBP (12 cores). We’re told four cores will focus on efficiency and eight cores on performance. You should keep those numbers in mind when creating threads. On that note, however, I want to mention the updated piece of code illustrated in this WWDC2015 video:

回到Apple Silicon和您将要用来运行线程的那些核心。 我们有一个关于2020年第四季度MBP(12核)的推测性数字。 我们被告知四个核心将专注于效率,八个核心将关注性能。 创建线程时,请记住这些数字。 不过,在此请注意,我要提及此WWDC2015视频中说明的更新代码:

let concurrent_backgroundTasks = 4
let semaphore = DispatchSemaphore(value: concurrent_backgroundTasks)
for i in 0...999 {
  background.async {
  Thread.sleep(forTimeInterval: 1)
  print("i \(i)")
  semaphore.signal() // semaphore count increased
}
semaphore.wait() // semaphore count decreased
print("completed")
}

Using this, the number of background tasks running (given that I have only four power-focused cores) will never exceed four. This is key if I have only four power-saving cores in my hardware.

使用此功能,正在运行的后台任务的数量(假设我只有四个专注于电源的内核)将永远不会超过四个。 如果我的硬件中只有四个节能内核,那么这是关键。

Of course, your app might run on lots of different hardware platforms over time, and in an ideal world, you’d make this piece of code more intelligent by querying how many cores it has. You can use this code to find out what the hardware you’re running on has to play with:

当然,随着时间的流逝,您的应用程序可能会在许多不同的硬件平台上运行,并且在理想情况下,您可以通过查询其内核数量来使这段代码更加智能。 您可以使用以下代码来查找运行的硬件必须使用的硬件:

let processInfo = ProcessInfo()
print(processInfo.activeProcessorCount)

Before I finish up, let’s touch on a WWDC2017 presentation that looks at another way to control code more carefully/efficiently on MP hardware. This little snippet:

在结束之前,我们先看一下WWDC2017的演示,该演示探讨了另一种在MP硬件上更仔细/更有效地控制代码的方法。 这个小片段:

DispatchQueue.concurrentPerform(iterations: 16) { ( process ) in
Thread.sleep(forTimeInterval: 1)
print("process \(process)")
}

This will execute the process on as many cores in parallel as it can, effectively launching as many processes as there are cores to execute the task. Obviously, the task at hand needs to have been carefully partitioned so that it makes sense. I recommend watching the first few minutes of the WWDC2017 talk on the subject to gain a better understanding of how it could work for you too.

这将在尽可能多的内核上并行执行该进程,从而有效地启动尽可能多的进程来执行任务。 显然,手头的任务需要经过仔细划分,以使其有意义。 我建议观看WWDC2017演讲的前几分钟,以更好地了解它也可以为您服务。

DispatchQueue continues to move ahead and may contain more primitives in 2021 to help you build the most efficient codebase yet on the different classes of processors.

DispatchQueue继续向前发展,并可能在2021年包含更多原语,以帮助您在不同类别的处理器上构建最高效的代码库。

Going back to where I started with the WWDC2020 presentation, Gavin goes on to touch on several more aspects and indeed names two more WWDC sessions you should watch. I will be looking at those in the coming weeks. Stay tuned.

回到我从WWDC2020演示文稿开始的地方,Gavin继续探讨了其他几个方面,的确命名了您应该观看的另外两个WWDC会议。 在接下来的几周中,我将研究这些内容。 敬请关注。

This brings me to the end of this short article. I learned a few things by writing this and I hope you did while reading it.

这使我结束了这篇简短的文章。 我通过写这篇文章学到了一些东西,希望您在阅读的同时也做了。

Keep calm, keep coding.

保持冷静,保持编码。

翻译自: https://medium.com/better-programming/apple-silicon-app-developer-9d725f10bee8

苹果开发者注册应用程序

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值