Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does `outlined init with copy of protocol` mean?

Tags:

swift

I wrote very simple piece of code in Swift:

protocol MultiplyByTwoProtocol {
    func multiply() -> Int
}

class MultiplyByTwoClass: MultiplyByTwoProtocol {
    private let n: Int

    init(n: Int) { self.n = n }

    func multiply() -> Int { return 2 * n }
}

class DynamicDispatchSwift {

    private let items: [MultiplyByTwoProtocol]

    init(n: Int) {
        self.items = Array<Int>.generate(size: n).map(MultiplyByTwoClass.init)
    }

    func run() {
        items.forEach { input in
            _ = input.multiply()
        }
    }
}

(btw generate method for Array just creates an array of random Ints)

Then, I run that code in Instruments and I got following results:

results from Instruments

As you can see, almost half of the time takes entry called outlined init with copy of MultiplyByTwoProtocol. Does anyone know what it is?

Thanks for your help.

like image 776
Wieczorny Avatar asked Oct 10 '18 06:10

Wieczorny


1 Answers

I just ran into outlined init with copy of ... as the top of a stack trace for crashes coming from an app built with release configuration. I found that turning off the compiler optimization settings would prevent the crash. Ultimately I also found an equivalent syntax for the code the stack trace pointed to, that the optimizer was having problems with, that did not cause crashes and was able to turn optimization back on.

In my case the line of code causing the issue was accessing a static constant inside a for loop. Assigning that same static constant to nothing just prior to the loop fixed the crash, as in this example:

let _ = Foo.fooConstant // This line fixed the crash
for index in 0..<values.count {
    let someBar = Bar(foo: .fooConstant)
    ...
}
like image 94
Charles A. Avatar answered Nov 04 '22 10:11

Charles A.