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-3334] for loop over array.reversed() takes much longer than over array (seems to still be in linear time with large constant factor) #45922
Comments
Starting with the stdlib, although it may turn out to be a missed optimization the compiler should be doing. |
Comment by Alexis Beingessner (JIRA) 60x overhead sounds about right for all the generic abstractions that ReversedRandomAccessCollection churns through. That's truly awful, but I have no idea how to get around that given the index-based programming model that the standard library has. Note that about 25% of this time is spent in swift_bridgeObjectRetain, so maybe this could be fixed by better ARC optimizations? |
I think it's reasonable to say the compiler should be able to get through the abstraction of ReversedRandomAccessCollection all the way down to a for-loop, but whether that means simplifying the type or changing the compiler or both I don't know. |
Comment by Dylan Brown (JIRA) I was able to reproduce this in Swift version 3.1-dev. The -O flag solves it, but it seems like it would be a surprisingly severe penalty for the average user, if compiled without the optimization flag. |
Comment by Dylan Brown (JIRA) I added some code to SwiftOnoneSupport.swift which is now generating symbols for several prespecialized IndexingIterator.next() on a ReversedRandomAccessCollection of Int, Int8, Int16, etc. I believe plain Swift Int type is shown below: I will keep working on it, but does anyone have a suggestion? |
Comment by Dylan Brown (JIRA) With Swift version 3.1, the provided test program outputs the following.
Note that forward and reversed iteration take approximately 2 seconds each. Also, happy 1_500_000_000 seconds! |
Attachment: Download
Additional Detail from JIRA
md5: 3ae4c12e2c889a3ec086b41d13e1f466
Issue Description:
I have this little test program
and the output of
time swift test4.swift
is:so going forwards over 400M things in the array takes about 3s on my machine, going over it backwards takes about 188s. It seems to be a linear cost of iterating over it forwards and backwards with a huge constant factor for backwards 🙁. Doesn't seem to happen with optimised builds.
Swift version is
The text was updated successfully, but these errors were encountered: