Linux Swift: Strange bug found in a simple arithmetic

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

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 <Issues · apple/swift · GitHub; 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)")

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.

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

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

It’s related to this: Exponential time complexity in the Swift type checker

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! :)
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

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

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"
   Exponential time complexity in the Swift type checker

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

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: