Performance regression in +[NSObject respondsToSelector:] in iOS 7
| Originator: | ben | ||
| Number: | rdar://15599098 | Date Originated: | 12/5/2013 |
| Status: | Closed | Resolved: | Resolved in 7.1b3 (11D5127c) |
| Product: | iOS SDK | Product Version: | 7.0 |
| Classification: | Performance | Reproducible: | Always |
There is a performance regression when calling +respondsToSelector: (not -respondsToSelector:) on a class that does not implement that selector in iOS 7. For our app, we see a regression in total time spent in +respondsToSelector: on launch from ~40 ms in iOS 6 to ~400 ms on iOS 7. As far as we can tell, the reason for this is because the implementation of _class_resolveClassMethod changed in iOS 7. In this function, the runtime needs to find the class associated with the metaclass passed into the function. In iOS 6, this was done by looking up the class by name in a hash table. In iOS 7, this is done by calling _class_getNonMetaClass, which looks for the metaclass => class mapping in a hash table that is almost never populated with anything, then devolves into a linear scan of all classes in all loaded libraries. See the attached text file for relevant code snippets. It seems like the perf regression would go away if: 1. the runtime switched back to using objc_getClass to find the class associated with the metaclass, or 2. the runtime could early exit without calling +resolveClassMethod: if it detected that the +resolveClassMethod: that would be called would be NSObject's (which does nothing and just returns NO anyway). Steps to Reproduce: Run the attached test app on an iPhone 4. Notice that in the attached instruments traces, we spend 60 ms in the CallRespondsToSelector function in iOS 6, and 523 ms in the CallRespondsToSelector function in iOS 7. Expected Results: There shouldn't be a 10x perf regression in this function. Actual Results: There is a 10x perf regression in this function. Version: 10A403 and 11B554a Sample app code to test for the regression is here: http://pastebin.com/7T2Yqfbg
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!