SwiftAutoLayout icon indicating copy to clipboard operation
SwiftAutoLayout copied to clipboard

Big negative impact on compilation time

Open APesate opened this issue 8 years ago • 4 comments

After running a profiling script to check the compilation times of the project (because it began to be really slow) I noticed that the methods where I set up the constraints with this library are the ones that take the longest to compile and it's making the project to take between 4 - 5 min to run. The project is really small so it shouldn't be the case.

This is the method that takes the longest to compile. 56690.86ms

fileprivate func setupConstraints() {
        playerComponentView.translatesAutoresizingMaskIntoConstraints = false
        headerComponentView.translatesAutoresizingMaskIntoConstraints = false
        tabsComponentView.translatesAutoresizingMaskIntoConstraints = false
        componentsScrollView.translatesAutoresizingMaskIntoConstraints = false
        topHeaderView.translatesAutoresizingMaskIntoConstraints = false
        closeButton.translatesAutoresizingMaskIntoConstraints = false
        chromecastButton.translatesAutoresizingMaskIntoConstraints = false
        topTitleLabel.translatesAutoresizingMaskIntoConstraints = false

        if UIDevice.current.userInterfaceIdiom == .pad {
            addConstraints([componentsScrollView.width == width * 0.7,
                            componentsScrollView.top == top,
                            componentsScrollView.bottom == bottom,
                            componentsScrollView.centerX == centerX])
        } else {
            addConstraints([componentsScrollView.leading == leading,
                            componentsScrollView.top == top,
                            componentsScrollView.bottom == bottom,
                            componentsScrollView.trailing == trailing])
        }

        let playerHeight = (((UIScreen.main.bounds.height * 0.7) * 9) / 16)
        addConstraints([playerComponentView.leading == componentsScrollView.leading,
                        playerComponentView.trailing == componentsScrollView.trailing,
                        playerComponentView.top == componentsScrollView.top,
                        playerComponentView.height == playerHeight,
                        playerComponentView.width == componentsScrollView.width])

        addConstraints([headerComponentView.top == playerComponentView.bottom,
                        headerComponentView.leading == componentsScrollView.leading,
                        headerComponentView.trailing == componentsScrollView.trailing,
                        headerComponentView.width == componentsScrollView.width])

        addConstraints([tabsComponentView.leading == componentsScrollView.leading,
                        tabsComponentView.trailing == componentsScrollView.trailing,
                        tabsComponentView.top == headerComponentView.bottom,
                        tabsComponentView.bottom == componentsScrollView.bottom,
                        tabsComponentView.width == componentsScrollView.width])

        topHeaderBottomConstraint = (topHeaderView.bottom == top)

        addConstraints([topHeaderBottomConstraint,
                        topHeaderView.leading == componentsScrollView.leading,
                        topHeaderView.trailing == componentsScrollView.trailing])

        let buttonsTopMargin = (UIApplication.shared.statusBarFrame.height + 8)

        addConstraints([closeButton.trailing == topHeaderView.trailing - 8,
                        closeButton.top == top + buttonsTopMargin,
                        closeButton.width == 48,
                        closeButton.height == 48])

        addConstraints([chromecastButton.trailing == closeButton.leading - 8,
                        chromecastButton.top == closeButton.top,
                        chromecastButton.width == 48,
                        chromecastButton.height == 48])

        addConstraints([topTitleLabel.leading == topHeaderView.leading + DetailsViewDesignGuidelines.contentMargin,
                        topTitleLabel.trailing == chromecastButton.leading - 8,
                        topTitleLabel.top == topHeaderView.top + (UIApplication.shared.statusBarFrame.height + 20),
                        topTitleLabel.bottom == topHeaderView.bottom - 16])
}

By changing the constraints configuration to use NSLayoutConstraint directly it goes down to 10.56ms As you can see is quite difference.

Any ideas on what could be the problem or how to fix this? I would really like to continue using this library because it makes so much easier and faster the constraints setup, but the impact it has on the build time is just too big and it will only get worse as the project gets bigger.

APesate avatar May 12 '17 13:05 APesate

I can only confirm what @APesate said. Same problem for me.

carmelogallo avatar May 13 '17 06:05 carmelogallo

This is a known issue with the compiler taking too long to resolve complex expressions. Unfortunately I don't have a solution for this until this is improved in the compiler.

indragiek avatar May 14 '17 18:05 indragiek

@indragiek I tried this other option, https://github.com/Raizlabs/Anchorage, sorry about that.

Anyway, the same code above goes from 56690.86ms to 580.65ms and the are using also complex expression. Maybe would be nice if you take a look and see what could be wrong.

I found a case were the compilation time is increased a lot: addConstraint(someView.top == view.top + 5.0 + 10.0 + 40.0) Here just because we are using multiple operation ( + 5.0 + 10.0 + 40.0 ) the compiler needs 5 secs to compile.

carmelogallo avatar May 28 '17 07:05 carmelogallo

I found a case were the compilation time is increased a lot: addConstraint(someView.top == view.top + 5.0 + 10.0 + 40.0)

Why not make the priority of the '+' operator between 'view.top' and '5.0' a little bit lower? This will let '5.0 + 10.0 + 40.0' be calculated first into 55.0 and turn 3 constraint-relative operations into 1.

rikumi avatar Jul 12 '17 14:07 rikumi