[Foundation] Performance improvements for IndexPath bridging and comparison (#9339)#9653
Merged
tkremenek merged 2 commits intoswiftlang:swift-4.0-branchfrom May 17, 2017
Conversation
…arison (swiftlang#9339) * [Foundation] Refactor the backing of IndexPath to favor stack allocations The previous implementation of IndexPath would cause a malloc of the underlying array buffer upon bridging from ObjectiveC. This impacts graphical APIs (such as UICollectionView or AppKit equivalents) when calling delegation patterns. Since IndexPath itself can be a tagged pointer and most often just a pair of elements it can be represented as an enum of common cases. Those common cases of empty, single, or pair can be represented respectively as no associated value, a single Int, and a tuple of Ints. These cases will be exclusively stack allocations, which is markably faster than the allocating code-path. IndexPaths that have a count greater than 2 will still fall into the array storage case. As an added performance benefit, accessing count and subscripting is now faster by aproximately 30% due to more tightly coupled inlining potential under whole module optimizations. Accessing count is also faster since it has better cache-line effeciency (lesson learned: the branch predictor is more optimized than pointer indirection chasing). Benchmarks performed on x86_64, arm and arm64 still pending results but should be applicable across the board. Resolves the following issues: https://bugs.swift.org/browse/SR-3655 https://bugs.swift.org/browse/SR-2769 Resolves the following radars: rdar://problem/28207534 rdar://problem/28209456 * [Foundation] remove temp IndexPath hashing that required bridging to ref types * [Foundation] IndexPath does not guarentee hashing to be the same as objc # Conflicts: # stdlib/public/SDK/Foundation/IndexPath.swift
…to calculate hash values (swiftlang#9476)
Contributor
Author
|
@swift-ci please test |
Contributor
Author
|
cc @parkera |
itaiferber
approved these changes
May 17, 2017
Contributor
itaiferber
left a comment
There was a problem hiding this comment.
Big diff, but a good performance improvement for IndexPath in the most common cases (small number of indexes in the path). LGTM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Explanation: IndexPath was refactored to avoid un-nessicary allocations by utilizing an enum backing store that switches upon the payload size of the path. The previous implementation of IndexPath would cause a malloc of the underlying array buffer upon bridging from ObjectiveC. This impacts graphical APIs (such as UICollectionView or AppKit equivalents) when calling delegation patterns. Since IndexPath itself can be a tagged pointer and most often just a pair of elements it can be represented as an enum of common cases. Those common cases of empty, single, or pair can be represented respectively as no associated value, a single Int, and a tuple of Ints. These cases will be exclusively stack allocations, which is markably faster than the allocating code-path. IndexPaths that have a count greater than 2 will still fall into the array storage case. As an added performance benefit, accessing count and subscripting is now faster by aproximately 30% due to more tightly coupled inlining potential under whole module optimizations. Accessing count is also faster since it has better cache-line effeciency (lesson learned: the branch predictor is more optimized than pointer indirection chasing).
Scope: This is limited to the specific functionality of IndexPath. No API changes were made in this.
Radar (and possibly SR Issue):
https://bugs.swift.org/browse/SR-3655
https://bugs.swift.org/browse/SR-2769
rdar://problem/28207534
rdar://problem/28209456
Tracked by rdar://problem/32110579
Risk: The hashing algorithm was changed for IndexPaths so they no longer match their objc counterparts. This could result in runtime assumptions failing but any heterogenous collections of objc and non objc paths will be converted into a uniform collection either via bridges or AnyHashable boxes.
Testing: This was tested against the Foundation overlay unit tests