[Foundation] Performance improvements for IndexPath bridging and comparison#9339
Merged
phausler merged 4 commits intoswiftlang:masterfrom May 6, 2017
Merged
Conversation
…ions 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
Contributor
Author
|
@swift-ci please test |
Contributor
Author
|
@swift-ci please test |
Contributor
|
Build failed |
Contributor
|
Build failed |
Contributor
Author
|
StringIndex should not have any interaction with IndexPath |
Contributor
Author
|
@swift-ci please test |
Contributor
|
Build failed |
Contributor
|
Build failed |
thomasvl
reviewed
May 10, 2017
| let totalBits = MemoryLayout<Int>.size * 8 | ||
| let lengthBits = 8 | ||
| let firstIndexBits = (totalBits - lengthBits) / 2 | ||
| return count + (first << lengthBits) + (last << (lengthBits + firstIndexBits)) |
There was a problem hiding this comment.
Shouldn't this have been:
return count &+ (first << lengthBits) &+ (last << (lengthBits + firstIndexBits))i.e. if the first/last value are large, you can overflow.
phausler
added a commit
to phausler/swift
that referenced
this pull request
May 16, 2017
…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
tkremenek
added a commit
that referenced
this pull request
May 17, 2017
[Foundation] Performance improvements for IndexPath bridging and comparison (#9339)
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.
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
This was re-opened since the CI server decided it didn't like the previous PR