Swift 中的 import 语句是否有相关成本? [英] Do import statements in Swift have an associated cost?

查看:28
本文介绍了Swift 中的 import 语句是否有相关成本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读字符串宣言,我看到了一段 关于在不必要时避免 Foundation 导入.

Reading the String Manifesto, I saw a paragraph about avoiding the Foundation import when not necessary.

为什么 Swift 团队会关注这个问题?除了美观和代码整洁之外,导入是否有成本?

Why is this a concern to the Swift team? Apart from aesthetics and code tidiness, do imports come with a cost?

导入不必要的框架是否会影响性能、内存使用、打包应用的大小或构建时间?

Could importing unnecessary frameworks impact performance, memory usage, packaged app size or build time?

推荐答案

page 只是说他们希望看到更多直接内置到 Swift 中的 String 方法.目前一些任务,比如区分大小写的字符串比较,需要导入 Foundation.

The page you reference is just saying that they'd like to see more String methods built directly into Swift. Currently some tasks like case-sensitive string comparison require importing Foundation.

好的,我将尝试回答您的其余问题.答案是这取决于您要导入的内容.

Ok I'm going to give the rest of your question a shot. The answer is it depends on what you are importing.

看着这个答案:

从 Xcode 5 开始,有一个引入预编译源数据库的新功能.Xcode 5 基本上只编译一次所有必需的框架,将构建保存在数据库中,并且已编译的部分在编译代码时使用

Since Xcode 5, there is a new feature introducing precompiled sources database. Xcode 5 basically compiles all the required frameworks just once, saves builds in the database and that already compiled pieces uses while compiling your code

也在看这个问题:

我们看到在每个使用它的文件中导入 UIKit 是必要的.如果上述解决方案是正确的,那么无论 UIKitFoundation 被导入多少次,它都只会编译一次.因此,多次导入标准库不会影响编译时间.

We see that importing UIKit in every file that uses it is necessary. If the above solution is correct than regardless of how many times UIKit or Foundation is imported it is only compiled once. Thus importing a standard library more than once as no affect on compile time.

但是,第一次导入某些内容会影响编译时间,因为该库现在需要在以前不需要时进行编译.

However, importing something for the first time would affect compile time because that Library now needs to be compiled when it previously was not needed.

例如,如果我必须在一个小的 Swift 程序中导入 Foundation,编译时间会变慢.对于 iOS 应用,基本上不可能不导入 UIKit,而 UIKit 也导入 Foundation,所以我认为这不值得担心,因为每个应用都必须编译这些库.

Example if I have to import Foundation in a small Swift program the compile time will be slowed down. When it comes to iOS apps it's basically impossible to not import UIKit which also imports Foundation so I don't think this is worth worrying about since every app will have to compile these libraries as well.

此外,我们需要查看由 Cocoa Pods迦太基:

Additionally, we need to look at imports that result from things like Cocoa Pods and Carthage:

查看这个仓库:

您可以通过两种方式在项目中嵌入第三方依赖项:

There are two ways you can embed third-party dependencies in your projects:

作为每次执行干净构建项目时编译的源(示例:CocoaPods、git 子模块、复制粘贴的代码、应用程序目标所依赖的子项目中的内部库)作为预构建的框架/库(例如:Carthage,由不想提供源代码的供应商分发的静态库)CocoaPods 是 iOS 上最受欢迎的依赖项管理器,这导致编译时间更长,因为在大多数情况下,每次执行干净构建时都会编译 3rd 方库的源代码.一般来说,您不应该经常这样做,但实际上,您确实这样做了(例如,因为切换分支、Xcode 错误等).

as a source that gets compiled each time you perform a clean build of your project (examples: CocoaPods, git submodules, copy-pasted code, internal libraries in subprojects that the app target depends on) as a prebuilt framework/library (examples: Carthage, static library distributed by a vendor that doesn’t want to provide the source code) CocoaPods being the most popular dependency manager for iOS by design leads to longer compile times, as the source code of 3rd-party libraries in most cases gets compiled each time you perform a clean build. In general you shouldn’t have to do that often but in reality, you do (e.g. because of switching branches, Xcode bugs, etc.).

Carthage 虽然更难使用,但如果您关心构建时间,它是更好的选择. 只有当您更改依赖项列表中的某些内容(添加新框架,更新一个新版本的框架等).这可能需要 5 或 15 分钟才能完成,但与构建嵌入 CocoaPods 的代码相比,您这样做的频率要低得多.

Carthage, even though it’s harder to use, is a better choice if you care about build times. You build external dependencies only when you change something in the dependency list (add a new framework, update a framework to a newer version, etc.). That may take 5 or 15 minutes to complete but you do it a lot less often than building code embedded with CocoaPods.

查看此博客:

我们看到,如果担心编译时间,可以使用更精确的导入.如import UIKit.UITableViewController

We see that there are more precise imports that can be used if one is concerned about compile time. Such as import UIKit.UITableViewController

就性能和二进制大小而言,任何未使用的符号都已由 Swift 编译器从最终二进制文件中优化出来.如果在编译时没有引用它,那么它将被删除,这意味着导入框架但不使用它的特定部分不应该有任何负面影响.

As far as performance and binary size goes, any unused symbols are already optimized out of the final binary by the Swift compiler. If there’s no reference to it at compile time then it’s removed, meaning that importing a framework but not using particular parts of it shouldn’t have any negative implications.

另一个博客 指出:

在这个 WWDC 2016 演讲中,Apple 建议将动态框架替换为静态存档以减轻这种情况.为了采用这种方法,我们静态地重建了尽可能多的动态框架,然后将它们合并到一个名为 AutomaticCore 的单一动态框架中.

In this WWDC 2016 talk, Apple suggests replacing dynamic frameworks with static archives to mitigate this. To take this approach, we rebuilt as many of our dynamic frameworks as possible statically and then merged them into a single monolithic dynamic framework named AutomaticCore.

差异很大:我们应用的启动时间减少了一半.

TLDR:不必担心导入 Foundation 或 UIKit 等标准内容,它们只编译一次,并且几乎每个应用程序都使用它们,因此可以分担任何性能下降.但是,如果您要导入第三方框架,您可能需要使用静态存档来帮助缩短编译时间.

TLDR: It's not worth worrying about importing standard things like Foundation or UIKit they are only compiled once and just about every app uses them so any performance decrease is shared. However, if you are importing third party Frameworks you might want to use a static archive to help with compile time.

这篇关于Swift 中的 import 语句是否有相关成本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆