You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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
finalclassBody{letshape:Shapevarposition=CGPoint()init(shape:Shape){self.shape = shape
shape.body =self}}finalclassShape{
unowned(unsafe) varbody:Body!//****** This line is the problem ******varvertices:[CGPoint]=[]init(){
for _ in 0..<8{self.vertices.append(CGPoint(x:CGFloat.random(in:-10...10), y:CGFloat.random(in:-10...10)))}}}varbodies:[Body]=[]
for _ in 0..<1000{
bodies.append(Body(shape:Shape()))}varpairs:[(Shape,Shape)]=[]
for i in 0..< bodies.count {leta=bodies[i]
for j in i +1..< bodies.count {letb=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. */letstartTime=CFAbsoluteTimeGetCurrent()
for (a,b) in pairs {vart: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
}lettime=CFAbsoluteTimeGetCurrent()- startTime
print(time)
The text was updated successfully, but these errors were encountered:
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
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: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.
The text was updated successfully, but these errors were encountered: