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
[Foundation] Performance improvements for IndexPath bridging and comparison #9339
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
@swift-ci please test |
@swift-ci please test |
Build failed |
Build failed |
StringIndex should not have any interaction with IndexPath
|
@swift-ci please test |
Build failed |
Build failed |
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this have been:
return count &+ (first << lengthBits) &+ (last << (lengthBits + firstIndexBits))
i.e. if the first/last value are large, you can overflow.
…arison (apple#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
[Foundation] Performance improvements for IndexPath bridging and comparison (#9339)
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