XCode 8 very slow swift compiling

2019-01-21 01:51发布

Ever since Swift 3 and XCode8 my project compiles quite slowly. Every time I add so much as an empty line to a file, recompiling takes a full minute. When I check the output, there is no particular file that takes very long. (I also used this tool to measure it: https://github.com/RobertGummesson/BuildTimeAnalyzer-for-Xcode)

It always appears to compile 4 files at once. The "rythm" is quite steady. Just very slow...

Also: Whenever I open or switch between files, it can take very long till I get autocomplete, or errors/warnings.

What things can I check? I almost feel like there is some flag I set that just drags down the build speed like crazy..

EDIT: This is not a solution to the underlying problem, but I spent some time on moving more code to frameworks. This made a difference (simply because it has to recompile less files every time). This shouldn't be necessary but it got unbearable... I'm of course still looking very much for a proper solution.

8条回答
冷血范
2楼-- · 2019-01-21 02:16

Make sure you're not combining arrays like let combinedArrays = array1 + array2. There's a known bug as well for type inference here, where Swift wastes time trying to figure out what type the combinedArrays should be. Instead, [array1, array2].joined() should work just as well, and compile far faster.

查看更多
Ridiculous、
3楼-- · 2019-01-21 02:18

One common practice that slows down compile time is using Array.append and String.append (or their + operator equivalents). For Strings, it's better to use a formatted string, so instead of

let hello = "Hello, "
let world = "World!"
let combinedString = hello + world

you should use

let combinedString = "\(hello)\(world)"

I can't remember the exact speedup, but it was on the order of 10 times for those particular lines. Odds are, thought, that this won't have a noticeable speedup for any but the tinest projects. For example, our project has hundreds of Swift files, as well as many Objective-C ones, and our compile times are often 10 minutes or more, sometimes even when the only change was to a non-Swift file.

查看更多
不美不萌又怎样
4楼-- · 2019-01-21 02:24

I've had the same issue only since upgrading to Swift 3/XCode 8 and it appears to be caused by large array literals, similar to this.

I was able to fix the issue by adding type annotations to the variables being assigned to the array literal, e.g.

let array: Array<String> = ["1", "2", "3", "4", "5", "6", "7", "8"]

instead of

let array = ["1", "2", "3", "4", "5", "6", "7", "8"]
查看更多
来,给爷笑一个
5楼-- · 2019-01-21 02:24

In my case I was using a helper function to save some data in Firebase. That function was returning a dictionary with about 20 elements and it would take about 40 mins to compile. My solution was to initialize an empty dictionary and then add the items one by one to someDict . Now it compiles in less than 30 seconds. I hope it helps.

Before

func toAnyObject() -> AnyObject {
  return
      ["BookingAmount":BookingAmount,
     "BookingNumber":BookingNumber,
     "PostCode":PostCode,
     "SelectedBathRow":SelectedBathRow,
     "SelectedBedRow":SelectedBedRow,
     "DateAndTime":DateAndTime,
     "TimeStampDateAndTime":TimeStampDateAndTime,
     "TimeStampBookingSavedInDB": TimeStampBookingSavedInDB,
     "FrequencyName":FrequencyName,
     "FrequecyAmount":FrequecyAmount,
     "insideCabinets": insideCabinets,
     "insideFridge": insideFridge,
     "insideOven": insideOven,
     "laundryWash": laundryWash,
     "interiorWindows": interiorWindows,
     "FullName":FullName,
     "SuppliesName":SuppliesName,
     "SuppliesAmount":SuppliesAmount,
     "FlatNumber":FlatNumber,
     "StreetAddress":StreetAddress,
     "PhoneNumber":PhoneNumber,
     "EmailAddress":EmailAddress] as AnyObject

}

After

  func toAnyObject() -> AnyObject {

    var someDict = [String : AnyObject]()
    someDict["BookingAmount"] = self.BookingAmount as AnyObject?
    someDict["BookingNumber"] = self.BookingNumber as AnyObject?
    someDict["PostCode"] = self.PostCode as AnyObject?
    someDict["SelectedBathRow"] = self.SelectedBathRow as AnyObject?
    someDict["SelectedBedRow"] = self.SelectedBedRow as AnyObject?
    someDict["DateAndTime"] = self.DateAndTime as AnyObject?
    someDict["TimeStampDateAndTime"] = self.TimeStampDateAndTime as AnyObject?
    someDict["TimeStampBookingSavedInDB"] = self.TimeStampBookingSavedInDB as AnyObject?
    someDict["FrequencyName"] = self.FrequencyName as AnyObject?
    someDict["FrequecyAmount"] = self.FrequecyAmount as AnyObject?
    someDict["insideCabinets"] = self.insideCabinets as AnyObject?
    someDict["insideFridge"] = self.insideFridge as AnyObject?
    someDict["insideOven"] = self.insideOven  as AnyObject?
    someDict["laundryWash"] = self.laundryWash as AnyObject?
    someDict["interiorWindows"] = self.interiorWindows as AnyObject?
    someDict["FullName"] = self.FullName as AnyObject?
    someDict["SuppliesName"] = self.SuppliesName as AnyObject?
    someDict["SuppliesAmount"] = self.SuppliesAmount as AnyObject?
    someDict["FlatNumber"] = self.FlatNumber as AnyObject?
    someDict["StreetAddress"] = self.StreetAddress as AnyObject?
    someDict["PhoneNumber"] = self.PhoneNumber as AnyObject?
    someDict["EmailAddress"] = self.EmailAddress as AnyObject?

    return someDict as AnyObject
}
查看更多
孤傲高冷的网名
6楼-- · 2019-01-21 02:24

I was able to greatly reduce my swift project compile times by avoiding the use the Nil-Coalescing Operator and string concatenation.

In otherwords where I had something like:

let x = "one" + object.nullableProperty ?? ""

I changed it to

let x = String(format: "one %@", object.nullableProperty ?? "")

My compile times have dropped drastically- from 20 minutes to 20 seconds.

查看更多
你好瞎i
7楼-- · 2019-01-21 02:27

A issue with this problem is that we don't know where is the wrong initialization/declaration . A solution that my colleague suggest is to find which function take long time to compile so:

  1. Go to Project select your target
  2. Build Settings -> Swift Compiler - Custom Flags
  3. Add to Other Swift Flags -Xfrontend -warn-long-function-bodies=50 (50 represent the time in milliseconds)

after that a warning should displayed as follow:

Getter 'frameDescription' took 108ms to type-check (limit: 50ms)

and after that you know what to do ;)

查看更多
登录 后发表回答