Skip to content

[Foundation] Performance improvements for IndexPath bridging and comparison (#9339)#9653

Merged
tkremenek merged 2 commits intoswiftlang:swift-4.0-branchfrom
phausler:swift-4.0-branch-IndexPath_perf
May 17, 2017
Merged

[Foundation] Performance improvements for IndexPath bridging and comparison (#9339)#9653
tkremenek merged 2 commits intoswiftlang:swift-4.0-branchfrom
phausler:swift-4.0-branch-IndexPath_perf

Conversation

@phausler
Copy link
Copy Markdown
Contributor

@phausler phausler commented May 16, 2017

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

phausler added 2 commits May 16, 2017 11:02
…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
@phausler
Copy link
Copy Markdown
Contributor Author

@swift-ci please test

@phausler
Copy link
Copy Markdown
Contributor Author

cc @parkera

Copy link
Copy Markdown
Contributor

@itaiferber itaiferber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big diff, but a good performance improvement for IndexPath in the most common cases (small number of indexes in the path). LGTM

@tkremenek tkremenek merged commit 68c3b51 into swiftlang:swift-4.0-branch May 17, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants