Promisekit: How to avoid long compilation time

Created on 4 Jun 2017  路  11Comments  路  Source: mxcl/PromiseKit

  • PromiseKit version:
  • Xcode version: 8.3.2

Hi,

I love to use PromiseKit but compilation times can be very long as soon as you chain 4 then in a method. Have you any tricks in order to avoid this compilation extra times?

Regards,
Quentin

Most helpful comment

Indeed, with this function
```

func testWith5GenericFunctionsWithReturnTypes() {
_ = genericPromise("a string").then { value -> Promise in
return genericPromise(value)
}.then { value -> Promise in
return genericPromise(value)
}.then { value -> Promise in
return genericPromise(value)
}.then { value -> Promise in
return genericPromise(value)
}.then { value -> Void in
print(value)
}
}```

Compilation time goes down from 3240ms to 21ms.

Thanks.

All 11 comments

I've not personally noticed this.

I've also not seen this. Can you provide more detail? An example would be even better.

Like, a test project that proves it would be good.

I guess this is not directly a PromiseKit. Compilation times go exponential if you chain generic functions that returns Promises.

With this function
func genericPromise<T>(_ value: T) -> Promise<T> { return Promise { fulfill, _ in DispatchQueue.main.async { fulfill(value) } } }

I wrote 5 promises chains with 1, 2, 3, 4, 5 successive calls.

```func testWith1GenericFunctions() {
_ = genericPromise("a string").then {
print($0)
}
}

func testWith2GenericFunctions() {
_ = genericPromise("a string").then {
return genericPromise($0)
}.then {
print($0)
}
}

func testWith3GenericFunctions() {
_ = genericPromise("a string").then {
return genericPromise($0)
}.then {
return genericPromise($0)
}.then {
print($0)
}
}

func testWith4GenericFunctions() {
_ = genericPromise("a string").then {
return genericPromise($0)
}.then {
return genericPromise($0)
}.then {
return genericPromise($0)
}.then {
print($0)
}
}

func testWith5GenericFunctions() {
_ = genericPromise("a string").then {
return genericPromise($0)
}.then {
return genericPromise($0)
}.then {
return genericPromise($0)
}.then {
return genericPromise($0)
}.then {
print($0)
}
}
```

Compilation times are:

  • testWith1GenericFunctions: 29 ms
  • testWith2GenericFunctions: 7 ms
  • testWith3GenericFunctions: 50 ms
  • testWith4GenericFunctions: 450 ms
  • testWith5GenericFunctions: 3276 ms

So I think that there is no bugs in PromiseKit, I just would like to have advice on how to optimise this situations with a readable solution.
PromiseTestCompilationTime.zip

Possibly if you specify the return types of the closures it helps?

Indeed, with this function
```

func testWith5GenericFunctionsWithReturnTypes() {
_ = genericPromise("a string").then { value -> Promise in
return genericPromise(value)
}.then { value -> Promise in
return genericPromise(value)
}.then { value -> Promise in
return genericPromise(value)
}.then { value -> Promise in
return genericPromise(value)
}.then { value -> Void in
print(value)
}
}```

Compilation time goes down from 3240ms to 21ms.

Thanks.

Interesting, thanks for bringing this to our attention.

If you will, please report this to http://bugs.swift.org

I cannot, it's quite possible that people at Apple will not listen to me anymore.

I'll try with swift 4 if the problem is still present. But maybe it is unavoidable, genericity has a price.

Obviously I'm not the expert, but my layman's observation is this looks like an O(n) situation. Linear chain.

I find Swift 4 much improves this.

Was this page helpful?
0 / 5 - 0 ratings