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-13074] unowned(unsafe) performance bug #55520

Open
swift-ci opened this issue Jun 25, 2020 · 0 comments
Open

[SR-13074] unowned(unsafe) performance bug #55520

swift-ci opened this issue Jun 25, 2020 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-13074
Radar None
Original Reporter rswift (JIRA User)
Type Bug
Environment

Code targets macOS 10.15 with release mode enabled (-O). Haven't tried testing on iOS yet.

Simply run the code in Xcode 11.5 or Xcode 12 beta, and compare the time to Xcode 11.1. You will see unowned(unsafe) has become much slower in Xcode 11.5/12 beta compared to Xcode 11.1.

Additional Detail from JIRA
Votes 4
Component/s
Labels Bug
Assignee None
Priority Medium

md5: 48db539b4b6ddb3f4451a345483eb84e

Issue Description:

Follow the discussion here

And a related stack overflow post with a benchmark

In Swift, weak/unowned references have significant overhead. I have managed to overcome this overhead by using unowned(unsafe). This is explained by the documentation in the following quote:

Swift also provides unsafe unowned references for cases where you need to disable runtime safety checks—for example, for performance reasons.

However, in Xcode 12 beta/Swift 5.3 and Xcode 11.5/Swift 5.2.4 unowned(unsafe) has much worse performance (about 5X Slower in a simple benchmark) compared to earlier versions of Swift such as Xcode 11.1/Swift 5.1. This has also been reproduced by others on stack overflow and on the Swift discussion forums linked above.

Below is the code, it is the same from the stack overflow link. Simply run it in Xcode 11.5 or Xcode 12 beta, and compare the time to Xcode 11.1. You will see unowned(unsafe) has become much slower in Xcode 11.5/12 beta compared to Xcode 11.1.

import Foundation

final class Body {
    let shape: Shape
    var position = CGPoint()
    init(shape: Shape) {
        self.shape = shape
        shape.body = self
        
    }
}

final class Shape {
    unowned(unsafe) var body: Body! //****** This line is the problem ******
    var vertices: [CGPoint] = []
    init() {
        for _ in 0 ..< 8 {
            self.vertices.append( CGPoint(x:CGFloat.random(in: -10...10), y:CGFloat.random(in: -10...10) ))
        }
    }
}

var bodies: [Body] = []
for _ in 0 ..< 1000 {
    bodies.append(Body(shape: Shape()))
}

var pairs: [(Shape,Shape)] = []
for i in 0 ..< bodies.count {
    let a = bodies[i]
    for j in i + 1 ..< bodies.count {
        let b = bodies[j]
        pairs.append((a.shape,b.shape))
    }
}

/*
 Benchmarking some random computation performed on the pairs.
 Normally this would be collision detection, impulse resolution, etc.
 */
let startTime = CFAbsoluteTimeGetCurrent()
for (a,b) in pairs {
    var t: CGFloat = 0
    for v in a.vertices {
        t += v.x*v.x + v.y*v.y
    }
    for v in b.vertices {
        t += v.x*v.x + v.y*v.y
    }
    a.body.position.x += t
    a.body.position.y += t
    b.body.position.x -= t
    b.body.position.y -= t
}
let time = CFAbsoluteTimeGetCurrent() - startTime

print(time)
@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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.
Projects
None yet
Development

No branches or pull requests

1 participant