xcode 编译时间慢得令人难以置信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39737374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compile Time Incredibly Slow
提问by dhint4
My project consists of ~350 Swift files and ~40 cocoa pod dependencies.
我的项目包含 ~350 个 Swift 文件和 ~40 个可可豆荚依赖项。
As soon as the entire project was migrated to Swift 3
, build times have been INCREDIBLYslow and took a little over 3 minutes to completely compile.
I've noticed that if I rebuild after not changing any files, it builds within a reasonable amount of time. However, if I add a new function, it takes the 3+ minutes.
Cocoapods does not seem to be causing the problem as it delays on Compiling Swift source files
state.
一旦整个项目迁移到Swift 3
,编译时间已经令人难以置信的缓慢,花了一点时间3分钟完全编译。
我注意到,如果我在不更改任何文件后重新构建,它会在合理的时间内构建。但是,如果我添加一个新功能,则需要 3+ 分钟。
Cocoapods 似乎没有引起问题,因为它延迟了Compiling Swift source files
状态。
I followed thisto investigate:
我按照这个来调查:
Added the
-Xfrontend -debug-time-function-bodies
flag to myOther Swift Flags
in my target's build settingsBuild the project
Copied this into terminal and ran
pbpaste | egrep '\.[0-9]ms' | sort -t "." -k 1 -n | tail -100
在我的目标的构建设置中将
-Xfrontend -debug-time-function-bodies
标志添加到我Other Swift Flags
的构建项目
将此复制到终端并运行
pbpaste | egrep '\.[0-9]ms' | sort -t "." -k 1 -n | tail -100
However, I didn't see anything of concern. The file that took the longest to compile was only 250ms. The next closest was 100ms, even if all 350 files took 250ms to compile, that would only be a total of 73 seconds which is way off from the 3+ minute builds I am seeing.
但是,我没有看到任何令人担忧的事情。编译时间最长的文件只有 250 毫秒。下一个最接近的是 100 毫秒,即使所有 350 个文件都需要 250 毫秒来编译,那总共也只有 73 秒,这与我看到的 3 分钟以上的构建相差甚远。
What could be causing these long compile times?
什么可能导致这些长编译时间?
It was never as slow before updating to Xcode 8
and Swift 3
.
在更新到Xcode 8
和之前,它从未如此缓慢Swift 3
。
回答by Alexander Selling
Update 1:
更新 1:
I created a new project without running the Swift 3
conversion, imported my Swift 3
files, but the build time remains the same.
我在没有运行Swift 3
转换的情况下创建了一个新项目,导入了我的Swift 3
文件,但构建时间保持不变。
Update 2:
更新 2:
I've tried SWIFT_WHOLE_MODULE_OPTIMIZATION = YES
, but as soon as you make changes to more than 1 file, the incremental build fails and it triggers a re-build which lasts for more than 4-5 minutes.
我已经尝试过SWIFT_WHOLE_MODULE_OPTIMIZATION = YES
,但是一旦您对 1 个以上的文件进行更改,增量构建就会失败,并且会触发持续超过 4-5 分钟的重新构建。
Update 3:
更新 3:
I've now re-written my entire code base from Swift 3
to Swift 2.3
. It didn't make any difference, the problem lies with the Xcode 8
compiler.
我现在已经从Swift 3
到重写了我的整个代码库Swift 2.3
。它没有任何区别,问题出在Xcode 8
编译器上。
Update 4:
更新 4:
I can confirm that unchecking these two will alleviate the pain for a while, the
Xcode 8
bug does seem to be tied to how it checks dependencies between files.
我可以确认取消选中这两个将缓解一段时间的痛苦,该
Xcode 8
错误似乎与检查文件之间的依赖关系的方式有关。
Update 5:
更新 5:
I've converted my code base to Swift 3
from Swift 2.3
since Xcode 8.2
beta requires it, the beta should include a fix for "Xcode will not rebuild an entire target when only small changes have occurred. (28892475)". Sad to say, they haven't fixed the bug and my compile times are exactly the same with Xcode 8.2 Beta
.
我已经将我的代码库转换为Swift 3
from,Swift 2.3
因为Xcode 8.2
beta 需要它,beta 应该包括一个修复“当只有小的更改发生时,Xcode 不会重建整个目标。(28892475)”。可悲的是,他们还没有修复错误,我的编译时间与Xcode 8.2 Beta
.
Original post:
原帖:
I don't have enough reputation to comment, but I still wanted to share some resources. I've been stuck in this misery for days, upgrading to Swift 3
has been a complete disaster.
我没有足够的声誉发表评论,但我仍然想分享一些资源。我已经被困在这种痛苦中好几天了,升级到Swift 3
完全是一场灾难。
I'm using this to track slow files, even though just like you, that's not my problem. Something else in xcode is taking literally 4 minutes to complete: https://github.com/irskep/swift_compile_times_parserhttps://github.com/RobertGummesson/BuildTimeAnalyzer-for-Xcode
我正在使用它来跟踪慢速文件,即使就像你一样,这不是我的问题。xcode 中的其他内容需要 4 分钟才能完成:https: //github.com/irskep/swift_compile_times_parser https://github.com/RobertGummesson/BuildTimeAnalyzer-for-Xcode
I've also made sure I don't have any lazy vars
or closures
that swift doesn't like. Don't use the + operator
when concatenating strings, etc.
see this.
我还确保我没有任何lazy vars
或closures
swift 不喜欢的。+ operator
连接字符串等时不要使用,
请参阅此。
I'll update this answer if I find anything, it's just about impossible to be productive with Swift 3
ATM.
如果我发现任何东西,我会更新这个答案,使用Swift 3
ATM几乎不可能提高效率。
回答by Santiago Carmona González
I'm using Xcode 8.1 My issue was with Dictionary which uses Nil-Coalescing Operator
我正在使用 Xcode 8.1 我的问题是使用字典 Nil-Coalescing Operator
this is my code when it takes 10 minutes to build:
这是我需要 10 分钟构建的代码:
let params: [String:String] = [
"email": email ?? self.email,
"clave": password,
"tipo_documento": documentType?.rawValue ?? self.typeDocument.rawValue,
"documento": number ?? self.documentNumber,
"nombre": name ?? self.name,
"apellidos": lastName ?? self.lastName,
"fecha_nacimiento": birth?.parse() ?? self.birthDate.parse(),
"genero": genre?.rawValue ?? self.genre.rawValue,
"telefono_movil": cel ?? self.cel,
"direccion": address ?? self.address
]
I don't know why but it advertise me that the Dictionary take a long time to compile.
我不知道为什么,但它告诉我字典需要很长时间来编译。
Then I change it to:
然后我把它改成:
var params: [String:String] = [:]
params["email"] = email ?? self.email
params["clave"] = password
params["tipo_documento"] = documentType?.rawValue ?? self.typeDocument.rawValue
params["documento"] = number ?? self.documentNumber
params["nombre"] = name ?? self.name
params["apellidos"] = lastName ?? self.lastName
params["fecha_nacimiento"] = birth?.parse() ?? self.birthDate.parse()
params["genero"] = genre?.rawValue ?? self.genre.rawValue
params["telefono_movil"] = cel ?? self.cel
params["direccion"] = address ?? self.address
Hope it could help some of you.
希望它可以帮助你们中的一些人。
回答by metpb
SWIFT_WHOLE_MODULE_OPTIMIZATION = YES
SWIFT_WHOLE_MODULE_OPTIMIZATION = 是
Xcode version: 8.1 GM
Xcode 版本:8.1 GM
To add choose your target, then go to Editor > Add Build Setting > Add User-Defined Setting
, and add the above.
要添加,请选择您的目标,然后转到Editor > Add Build Setting > Add User-Defined Setting
,并添加上述内容。
My clean build time dropped from 35 mins (Ahem, excuse me) to 8 mins with a project file count of 800.
我的干净构建时间从 35 分钟(咳咳,对不起)下降到 8 分钟,项目文件数为 800。
Note: Tried this on Xcode 8.0 first, but didn't work.
注意:首先在 Xcode 8.0 上尝试过,但没有成功。
回答by coyer
Also string concatenation
seems to be incredible slow in Swift3/XCode8:
string concatenation
在 Swift3/XCode8 中似乎也慢得难以置信:
item.text = item.text + " " + pickerText + " " + (attribute?.Prefix ?? "") + inputText + (attribute?.Suffix ?? "")
~ took 8-10 seconds to compile
~ 编译用了 8-10 秒
item.text = "\(item.text) \(pickerText) \(attribute?.Prefix ?? "")\(inputText)\(attribute?.Suffix ?? "")"
~ took 1,6 seconds to compile
~ 编译耗时 1.6 秒
item.text = [item.text, " ", pickerText, " ", (attribute?.Prefix ?? ""), inputText, (attribute?.Suffix ?? "")].joined();
~ took 0,001 second to compile
~ 编译耗时 0,001 秒
回答by Ruud Visser
I found a couple of coding styles that take a lot of time compiling in Swift (2.3, not tested on 3):
我发现了一些需要花费大量时间在 Swift 中编译的编码风格(2.3,未在 3 上测试):
a += b
Should be
应该
a = a + b
Also adding array together:
还将数组添加在一起:
var a = [1,3,4]
var b = [5,6,7,8]
var c = [8,4,3,5]
var d = a + b + c
Should be
应该
var a = [1,3,4]
var b = [5,6,7,8]
var c = [8,4,3,5]
var d : [Int] = []
d.appendContentsOf(a)
d.appendContentsOf(b)
d.appendContentsOf(c)
The last optimization took the compilation time for 1 method from 9800ms to 5.5ms...
最后一次优化将 1 个方法的编译时间从 9800 毫秒缩短到 5.5 毫秒……
回答by obelisk
I migrated a Swift 2x project of 17 files to Swift 3 and had 28 warnings and 55 errors across all files. Compile time was 4-5 minutes.
我将一个包含 17 个文件的 Swift 2x 项目迁移到 Swift 3,并且在所有文件中出现了 28 个警告和 55 个错误。编译时间为 4-5 分钟。
Disabling
禁用
Find Implicit Dependancies
in scheme and
在计划和
SWIFT_WHOLE_MODULE_OPTIMIZATION = YES
made only minor improvements.
只做了很小的改进。
As I eventually cleared the warnings and errors in each file, the compile time reduced and is now in the seconds. The IDE is back behaving as it should - detecting errors in near real time and compiling quickly.
当我最终清除每个文件中的警告和错误时,编译时间减少了,现在以秒为单位。IDE 恢复正常运行 - 近乎实时地检测错误并快速编译。
Firstly, it looks like the compiler is recompiling (or at least cross checking) every file with any error or warning - even if you haven't edited that file since the last compile.
首先,看起来编译器正在重新编译(或至少交叉检查)每个带有任何错误或警告的文件——即使您自上次编译以来没有编辑过该文件。
Secondly, if there are too many dependant errors/warnings across files, the compiler bottlenecks and slows right down.
其次,如果跨文件存在太多相关错误/警告,编译器就会出现瓶颈并减慢速度。
回答by Vaibhav Saran
Whenever you face slow compilation issue, closly looks the the third party SDKs you included in your app and also try to find your coding techniques.
每当您遇到编译缓慢的问题时,请仔细查看您的应用程序中包含的第三方 SDK,并尝试找到您的编码技术。
I face this issue twice in my app and I felt like harrassed by Swift 3 . Both the times reasons were different.
我在我的应用程序中两次遇到这个问题,我觉得被 Swift 3 骚扰了。两个时代的原因不同。
First time I found that I added a library in my app named AASignatureView. Since this lib was added, my compilation time increased like hell. It started taking near 12-15 Mins in running the app on simulator. The issue was resolved after I removed this lib from my code and added VMSignatureViewand my compilation time went into normal state.
我第一次发现我在我的应用程序中添加了一个名为AASignatureView的库。由于添加了这个库,我的编译时间像地狱一样增加。在模拟器上运行应用程序开始需要近 12-15 分钟。在我从我的代码中删除这个库并添加VMSignatureView并且我的编译时间进入正常状态后,问题得到了解决。
Second time I faced this issue after making a code of appending several Strings. Both of these below methods turned app's compilation time to hell
在编写了附加几个字符串的代码后,我第二次遇到这个问题。以下这两种方法都将应用程序的编译时间变成了地狱
a = a + b
and
和
a += b
Then I changed my code to below way and the problem was solved.
然后我将代码更改为以下方式,问题解决了。
a = "a\(strSometihng),\(strAnother)"
回答by Mario Jaramillo
Concatenating several Strings also can cause increasing compiling times, for example in my case, my compilation times where very high because of this line:
连接多个字符串也会导致编译时间增加,例如在我的情况下,由于这一行,我的编译时间非常长:
let fecha = post.dia + " " + post.num_dia + " " + post.mes + " - " + post.hora
When i changed the code to this, it satart compiling in seconds:
当我将代码更改为此时,它会在几秒钟内开始编译:
var fecha = post.dia!
fecha = fecha + " "
fecha = fecha + post.num_dia!
fecha = fecha + " "
fecha = fecha + post.mes!
fecha = fecha + " - "
fecha = fecha + post.hora!