Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-7759] Swift 4.1.1 performance regression #50298

Closed
swift-ci opened this issue May 24, 2018 · 7 comments
Closed

[SR-7759] Swift 4.1.1 performance regression #50298

swift-ci opened this issue May 24, 2018 · 7 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. performance standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-7759
Radar None
Original Reporter woodcrust (JIRA User)
Type Bug
Status Closed
Resolution Done

Attachment: Download

Additional Detail from JIRA
Votes 2
Component/s Standard Library
Labels Bug, Performance
Assignee None
Priority Medium

md5: 4069bbc8d4757d9becf5310fc5d09c1f

Issue Description:

Hello, my name is Oleh. My english is very bad =(
I am seeing a decline in performance between versions like 4.0.3 and 4.1.1. Me very interested, why ? I am testing this on linux os and other computers with this code:

func shellsSort<T : Comparable>(_ arr: inout [T]) {
    let n = arr.count
    var gap = n/2
    var i = 0
    var j = 0
    var temp : T
    while gap > 0 {
        i = gap
        while i < n {
            temp = arr[i]
            j = i
            while (j >= gap && arr[j - gap] > temp) {
                arr[j] = arr[j - gap]
                j = j - gap
            }
            arr[j] = temp
            i = i + 1
        }
        gap = gap/2
    }
}

var a = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
var c = 0

while (c < 10000000) {
    shellsSort(&a)
    a = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
    c = c + 1
}

And I see what the regression of performance in 4.1.1 version equal ~74% then in versions 4.0.3, 3.0.1 etc

for example
4.0.3 - 1,66 sec
4.1.1 - 2,89 sec

Please tell me why ?

@belkadan
Copy link
Contributor

cc @moiseev, @eeckstein

@moiseev
Copy link
Mannequin

moiseev mannequin commented May 24, 2018

woodcrust (JIRA User) a few questions: did you compile with or without optimizations, as in, what were the compiler command line arguments? Also, do you mind invoking both compilers with `-emit-sil` option and attach the output to the jira? It would help diagnose the issue quicker.

@swift-ci
Copy link
Collaborator Author

Comment by Oleh (JIRA)

yes of course. Wait please a few minutes ...
p.s. I compiled with this command:
swiftc -O ./swift.swift -o ./path
because without optimization swift very slow. Slower than nodejs(~2,4) =)

@weissi
Copy link
Member

weissi commented May 24, 2018

Can confirm that, and it regressed a bit further with Swift 4.2:

(the jw-swift-VERSION is just a wrapper around xcrun that selects the right version; jw-dump-sil is just swiftc -frontend -emit-sil "$@" "$file")

for version in 4.0 4.1 latest; do rm -f test && echo running with $(jw-swift-$version swift -version) && jw-swift-$version swiftc -O test.swift && time ./test && jw-swift-$version jw-dump-sil -O test.swift > /tmp/$version.sil; done
running with Apple Swift version 4.0.3 (swift-4.0.3-RELEASE) Target: x86_64-apple-macosx10.9

real    0m1.706s
user    0m1.681s
sys 0m0.014s
running with Apple Swift version 4.1 (swift-4.1-RELEASE) Target: x86_64-apple-darwin17.6.0

real    0m2.150s
user    0m2.116s
sys 0m0.021s
running with Apple Swift version 4.2-dev (LLVM ff3ae623c3, Clang c2feda0b5c, Swift 58f7399ba5) Target: x86_64-apple-darwin17.6.0

real    0m2.214s
user    0m2.185s
sys 0m0.017s

attaching the sil files too

@swift-ci
Copy link
Collaborator Author

Comment by Oleh (JIRA)

I attached files with names:
emit-sil-4.0.3
emit-sil-4.1.1

latest test on

Linux cybertron 3.16.0-4-amd64 #1 SMP Debian 3.16.51-3 (2017-12-13) x86_64 GNU/Linux
with this simple code
https://github.com/woodcrust/puffin_bench/blob/master/source/1-sortArray/swift.swift
4.0.3 - 1,60 sec

4.1.1 - 2,27 sec

@jckarter
Copy link
Member

It's possible that the introduction of exclusivity checking for globals is impacting things here. If I change it so that the benchmark code is wrapped in a function, like this:

func shellsSort<T : Comparable>(_ arr: inout [T]) {
    let n = arr.count
    var gap = n/2
    var i = 0
    var j = 0
    var temp : T
    while gap > 0 {
        i = gap
        while i < n {
            temp = arr[i]
            j = i
            while (j >= gap && arr[j - gap] > temp) {
                arr[j] = arr[j - gap]
                j = j - gap
            }
            arr[j] = temp
            i = i + 1
        }
        gap = gap/2
    }
}

func main() {
  var a = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
  var c = 0

  while (c < 10000000) {
      shellsSort(&a)
      a = [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
      c = c + 1
  }
}

main()

Then I see a 35% improvement, putting it back in line with the Swift 4 performance:

$ time ./with_function

real    0m1.469s
user    0m1.451s
sys 0m0.011s
$ time ./with_toplevel_code

real    0m1.982s
user    0m1.960s
sys 0m0.013s

We've seen a number of issues where "toplevel" code doesn't get optimized as well as standalone functions do. A lot of this is down to the underlying design flaw that top-level variables are treated like global variables and made visible to functions in other files, meaning we have to optimize them conservatively as if they can be modified from anywhere, when they should really be treated as local to the scope of the implicit toplevel entry point.

@swift-ci
Copy link
Collaborator Author

Comment by Oleh (JIRA)

@jckarter

✔ cool thanks a lot, its work 😃

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. performance standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

4 participants