Linux Swift: Strange bug found in a simple arithmetic


(louislepegue) #1

Hi

This simple instruction can take more than 20 seconds to execute 

and near 100% CPU on different Linux Swift version I tested.

Also tested in compiled mode with the same result.

      let simpleTest = Float(abs(11 - -3) + abs(4.5 -

-4)) //!

  print ("simpleTest:  \(Simpletest)")

  I know that a many variations can cause more or less results I

can’t determine if Float or abs is the culprit.

  Do someone have an idea on this ?

Thank you

···

Louis Plouffe


(Alex Blewitt) #2

Aside from the fact that the case-sensitivity of simpleTest is important, the delay is in the compilation of this script rather than the runtime performance of the script. You can compile it with swiftc (which takes around 20s) but then when running it the performance is fine.

It's certainly an interesting compiler issue, probably to do with the type analysis rather than the complexity of the abs function or the Float cast directly. It's still worth raising a bug at https://bugs.swift.org <https://bugs.swift.org/> with the snippet though.

Alex

···

On 7 Sep 2016, at 15:11, louislepegue via swift-users <swift-users@swift.org> wrote:

let simpleTest = Float(abs(11 - -3) + abs(4.5 - -4)) //!
print ("simpleTest: \(Simpletest)")


(Lou Zell) #3

It's certainly an interesting compiler issue, probably to do with the type
analysis

I've for sure hit this before and now see with a little help to the
compiler the compile time drops dramatically:​

Float(abs(Double(0) - Double(0)))
repl.swift:50:1: error: expression was too complex to be solved in
reasonable time; consider breaking up the expression into distinct
sub-expressions

Float(abs(Double(0 as Int) - Double(0 as Int)))
// Compiles instantly!

It makes the code ugly though, so we're probably better-off busting it into
subexpressions like the error above states.


(Lou Zell) #4

Also hoping someone more knowledgeable can explain why these seemingly
simple cases do in fact take so long! :slight_smile:


(louislepegue) #5

oups! typo!

the real test:

    let

simpleTest = Float(abs(11 - -3) + abs(4.5 - -4)) //!

    print ("simpleTest:  \(simpleTest)")
···

Le 07/09/2016 à 10:33, Alex Blewitt a écrit :

On 7 Sep 2016, at 15:11, louislepegue via swift-users swift-users@swift.org wrote:

          let simpleTest =

Float(abs(11 - -3) + abs(4.5 - -4)) //!

          print ("simpleTest:  \(Simpletest)")
      Aside from the fact that the case-sensitivity of

simpleTest is important, the delay is in the compilation of
this script rather than the runtime performance of the script.
You can compile it with swiftc (which takes around 20s) but
then when running it the performance is fine.

      It's certainly an interesting compiler issue,

probably to do with the type analysis rather than the
complexity of the abs function or the Float cast directly.
It’s still worth raising a bug at https://bugs.swift.org with
the snippet though.

Alex


(Jon Shier) #6

It’s related to this: http://www.cocoawithlove.com/blog/2016/07/12/type-checker-issues.html

Jon

···

On Sep 7, 2016, at 11:32 AM, Lou Zell via swift-users <swift-users@swift.org> wrote:

Also hoping someone more knowledgeable can explain why these seemingly simple cases do in fact take so long! :slight_smile:
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Ole Begemann) #7

Also hoping someone more knowledgeable can explain why these seemingly
simple cases do in fact take so long! :slight_smile:

Matt Gallagher wrote an excellent article where he explained how seemingly simple expressions can sometimes cause an enormous amount of work for the type checker because it has so many overloads to consider:

   "Exponential time complexity in the Swift type checker"
   http://www.cocoawithlove.com/blog/2016/07/12/type-checker-issues.html

It's a great read.

For what it's worth, on my Mac with Xcode 8b6 the snippet takes about 3 seconds to compile (which is still a long time).

Ole


(louislepegue) #8

Thanks

  A must reading, at least I understand that is a compiler bug that

could happen with type conversion. Also, a workaround can be
easily done.

  On this September 7, I hope that we will have a new release :-)
···
  Le 07/09/2016 à 11:47, Ole Begemann via swift-users a écrit :
  Matt Gallagher wrote an excellent article where he

explained how seemingly simple expressions can sometimes cause an
enormous amount of work for the type checker because it has so
many overloads to consider: