Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Identify slow code to optimize build time

Tags:

xcode

ios

swift

I'm using these Compilation Swift Flag to identify codes that slow down the compilation time:

-Xfrontend -warn-long-function-bodies=100
-Xfrontend -warn-long-expression-type-checking=100

Then after building, I get warnings like these:

Instance method 'startFadePositionTitle()' took 2702ms to type-check (limit: 500ms)

for this part of the code:

    func startFadePositionTitle() -> CGFloat {
        let value: CGFloat = ((backgroundImage.frame.height/2 - contentTitle.frame.height/2) - navbarView.frame.height)/2
        return value
    }

Can someone explains me what is wrong in this method and what could I possibly improve?

like image 480
Jaythaking Avatar asked Nov 23 '19 06:11

Jaythaking


1 Answers

You should break it to smaller chunks, then Swift can do type check more easily. Also the more you tell, the less Swift has to think. So you can help compiler and tell it anything you already know:

func beginFadePositionTitle() -> CGFloat {
    let n: CGFloat = 2
    let a: CGFloat = self.backgroundImage.frame.height/n
    let b: CGFloat = self.contentTitle.frame.height/n
    let ab: CGFloat = a - b
    let c: CGFloat = self.navbarView.frame.height
    let abc: CGFloat = ab - c
    return abc/n
}

Instance method 'beginFadePositionTitle()' took 1ms to type-check (limit: 1ms)

This is the result when you tell everything to compiler. See the difference?

like image 59
Mojtaba Hosseini Avatar answered Oct 28 '22 19:10

Mojtaba Hosseini