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
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:
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
return genericPromise(value)
}.then { value -> Promise
return genericPromise(value)
}.then { value -> Promise
return genericPromise(value)
}.then { value -> Promise
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.
Most helpful comment
Indeed, with this function
```
func testWith5GenericFunctionsWithReturnTypes() { in in in in
_ = genericPromise("a string").then { value -> Promise
return genericPromise(value)
}.then { value -> Promise
return genericPromise(value)
}.then { value -> Promise
return genericPromise(value)
}.then { value -> Promise
return genericPromise(value)
}.then { value -> Void in
print(value)
}
}```
Compilation time goes down from 3240ms to 21ms.
Thanks.