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-4856] Swift Array copy is very slow #47433
Comments
I can't reproduce this, even in -Onone: swiftc -Onone x.swift -o /tmp/x && /tmp/x
Are you sure you didn't leave off the |
Comment by Rick M (JIRA) Copied-and-pasted directly from Xcode 8.3.2 (8E2002). Attached Playground file. |
Running this in Playground is slow because Playground need to display the results.
|
Comment by Rick M (JIRA) I don't buy that this is slow because it needs to display results. That doesn't really make any sense. Can you explain why this would be the case? If you want to more closely reporduce the situation I see running on our iOS device, you can take a slice of array1 when making the copy. That's the only difference. It didn't seem necessary for this example since performance is so bad with the example as given. |
Rintaro's is the likeliest explanation. As he says, there is basically no computation required to "copy" or to slice an array. |
You could definitely consider this an Xcode/playgrounds bug, but then I think you'd want to file a radar as that code is not part of the open source Swift project. |
Comment by Rick M (JIRA) So, this first showed up in code on an iOS app (both Simulator and device). I just tried to get a reproducible case in a Playground. But it's not a Playground bug. What else could it be? |
rmann (JIRA User) I have no clue, sorry. I don't think the code you're running in your playground is what's slowing down your iOS app though. I suggest you reduce the app until you can isolate the problem. |
Comment by Rick M (JIRA) I can promise you it was this line of code: thing = Array(self.dataBuffer![0 ..< bigSize]) |
But that line of code is a very different matter. Copying a slice of an array into an Array is O(N), because the Array has a 1-word representation (pointer-to-buffer) and can't represent a slice of some other buffer. |
Comment by Rick M (JIRA) So, in my comment a couple hours ago I said "If you want to more closely reporduce the situation I see running on our iOS device, you can take a slice of array1 when making the copy." The response from you was "there is basically no computation required to "copy" or to slice an array," so I assumed you were saying it wouldn't be making the copy. Nevertheless, it seems to be a very slow copy. I fully expect it to be O(N), but two seconds to copy a megabyte? That sounds like an unoptimized loop, rather than a call to memcpy() (or similar). |
— So, in my comment a couple hours ago I said "If you want to more closely reporduce the situation I see running on our iOS device, you can take a slice of array1 when making the copy." The response from you was "there is basically no computation required to "copy" or to slice an array," so I assumed you were saying it wouldn't be making the copy. Sorry, lots going on here; subtle hints can get lost in the noise. — Nevertheless, it seems to be a very slow copy. I fully expect it to be O(N), but two seconds to copy a megabyte? That sounds like an unoptimized loop, rather than a call to memcpy() (or similar). That does sound very slow. Is this a debug or release build? |
Comment by Rick M (JIRA) I do believe it was a debug build. Not sure what the Playground is doing. |
That doesn't surprise me too much then. With this program: import Foundation
func test() {
let hugeSize = 1 * 1024 * 1024
let array1 = [UInt8](repeating: 0, count: hugeSize)
print("Starting copy")
let startTime = CFAbsoluteTimeGetCurrent()
let array2 = Array(array1[1..<array1.endIndex])
let endTime = CFAbsoluteTimeGetCurrent()
let dur = endTime - startTime
print("Copy dur: \(dur) s")
if CommandLine.arguments.count == 1000 { print(array2) }
}
test()
If it's any consolation, when I use the current master branch of Swift in -Onone, I get:
|
Attachment: Download
Environment
Xcode 8.3.2, macOS 10.12.4, on an early 2013 15" Retina MacBook Pro, 2.8 GHz
Additional Detail from JIRA
md5: ce755e5a51e1a3472fde13818edfce75
Issue Description:
Creating an Array from a 1 MB [UInt8] array takes 3 seconds (2 MB takes 6 s, 3 MB takes 9 s). This seems very, very slow.
Output:
This is in Xcode 8.3.2, macOS 10.12.4, on an early 2013 15" Retina MacBook Pro, 2.8 GHz. I've also seen it on an iPad Pro.
The text was updated successfully, but these errors were encountered: