Base types casting and hidden function calls in SWIFT


(Alex) #1

After few days playing with the profiler I have noticed some shocking swift behaviour.When casting basic types from one type to another, for example from Int to Double swift is actually creating and then deallocating something :frowning: That leads to another performance problem - imagine many casts in loops.
As a type strict language swift is forcing one to cast Int to Double to when you multiply double by Int variables for example but as the cast is so expensive what should developers do to get better performance for some bit of code. As a sound processing developer I see most of the real time sound processing moved to C++ code, but I don,t see the reason for that as it is most basic operations in loops. Swift needs to be faster to become serious language ...
After profiling here is what I see:
1. slow arrays - may be partly improved using UnsafeMuttablePointers
2. expensive cast - this is not possible to avoid at this point. I will suggest making the language not so type strict when there is no need (like adding int to double and so on)

array[i] = value Double(1)
These problems come from hidden function calls so please avoid adding more of them in future like the syntax Erica suggested for the if:
if v in (0...127)
I suppose that the range will be also created and then deallocated in background.


(Joe Groff) #2

This is probably a better discussion for swift-users. It sounds like you didn't build with optimization enabled鈥攖ry that before making any performance measurements.

-Joe

路路路

On Mar 28, 2016, at 7:05 AM, Biala via swift-evolution <swift-evolution@swift.org> wrote:

After few days playing with the profiler I have noticed some shocking swift behaviour.
When casting basic types from one type to another, for example from Int to Double swift is actually creating and then deallocating something :frowning: That leads to another performance problem - imagine many casts in loops.

As a type strict language swift is forcing one to cast Int to Double to when you multiply double by Int variables for example but as the cast is so expensive what should developers do to get better performance for some bit of code. As a sound processing developer I see most of the real time sound processing moved to C++ code, but I don,t see the reason for that as it is most basic operations in loops. Swift needs to be faster to become serious language ...

After profiling here is what I see:

1. slow arrays - may be partly improved using UnsafeMuttablePointers

2. expensive cast - this is not possible to avoid at this point. I will suggest making the language not so type strict when there is no need (like adding int to double and so on)

array[i] = value
Double(1)

These problems come from hidden function calls so please avoid adding more of them in future like the syntax Erica suggested for the if:

if v in (0...127)

I suppose that the range will be also created and then deallocated in background.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(F茅lix Cloutier) #3

How did you come to that conclusion? In Swift 2.1 (I'm late to update), with optimizations on, this function:

func test() -> Double {
聽聽return Double(arc4random())
}

compiles to:

define hidden double @_TF4test4testFT_Sd() #0 {
entry:
聽聽%0 = tail call i32 @arc4random()
聽聽%1 = uitofp i32 %0 to double
聽聽ret double %1
}

which has no allocation at all.

F茅lix


(F茅lix Cloutier) #4

How did you come to that conclusion? In Swift 2.1 (I'm late to update), with optimizations on, this function:

func test() -> Double {
聽聽return Double(arc4random())
}

compiles to this IR:

define hidden double @_TF4test4testFT_Sd() #0 {
entry:
%0 = tail call i32 @arc4random()
%1 = uitofp i32 %0 to double
ret double %1
}

which has no allocation at all.

F茅lix

路路路

Le 28 mars 2016 脿 07:05:48, Biala via swift-evolution <swift-evolution@swift.org> a 茅crit :

After few days playing with the profiler I have noticed some shocking swift behaviour.
When casting basic types from one type to another, for example from Int to Double swift is actually creating and then deallocating something :frowning: That leads to another performance problem - imagine many casts in loops.

As a type strict language swift is forcing one to cast Int to Double to when you multiply double by Int variables for example but as the cast is so expensive what should developers do to get better performance for some bit of code. As a sound processing developer I see most of the real time sound processing moved to C++ code, but I don,t see the reason for that as it is most basic operations in loops. Swift needs to be faster to become serious language ...

After profiling here is what I see:

1. slow arrays - may be partly improved using UnsafeMuttablePointers

2. expensive cast - this is not possible to avoid at this point. I will suggest making the language not so type strict when there is no need (like adding int to double and so on)

array[i] = value
Double(1)

These problems come from hidden function calls so please avoid adding more of them in future like the syntax Erica suggested for the if:

if v in (0...127)

I suppose that the range will be also created and then deallocated in background.

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution